nginx / unit

NGINX Unit - universal web app server - a lightweight and versatile open source server that simplifies the application stack by natively executing application code across eight different programming language runtimes.
https://unit.nginx.org
Apache License 2.0
5.26k stars 322 forks source link

Issue with creating files and folders from PHP #1167

Closed elpado closed 2 months ago

elpado commented 4 months ago

Hello dear developers,

I would like to test Unit for a small PHP project. I use the Docker Image with PHP 8.3 installed.

The code of the application is stored within the container under "/mnt/code/". But I have a, let's call it, data exchange directory. This directory lives outside of my application root.

-/
 -- /mnt
     -- /code
-/data-store  <-- exchange directory

My application need to create new folders and files in that exchange directory.

My config.json looks like this:

{
  "listeners": {
    "*:80": {
      "pass": "routes"
    }
  },

  "routes": [
    {
      "match": {
        "uri": "!/index.php"
      },
      "action": {
        "share": "/mnt/code/",
        "fallback": {
          "pass": "applications/exchange-data"
        }
      }
    }
  ],

  "applications": {
    "exchange-data": {
      "type": "php",
      "root": "/mnt/code/public/",
      "script": "index.php",
    }
  }
}

But when I try to create a new directory or file in the exchange folder, I receive a permission denied :(

    mkdir ('/data-store/data-set/2024-03-01', 0777, true);

    mkdir(): Permission denied in /mnt/code/src......

It's the very first time I'm trying to use Unit for one of my applications. I've also tried changing my configuration and adding a user/group - but to no success.

Is there something I have overlooked?

ac000 commented 4 months ago

You will need to ensure that the user that Unit is running as has permissions to create the directory. mkdir(2) needs write permission on the parent directory.

elpado commented 4 months ago

Thanks for the quick reply :)

I have now simply set up a small test container and tested it with the following configuration - and it now works. I am now trying to use the same configuration for the actual project and hope that there are no more problems.

Dockerfile

FROM unit:php8.3

WORKDIR /opt/app

RUN mkdir /data-store && chown -R unit:unit /data-store

COPY ./config.json /docker-entrypoint.d/config.json
COPY ./index.php /opt/app/public/index.php

config.json

{
  "listeners": {
    "*:80": {
      "pass": "routes"
    }
  },

  "routes": [
    {
      "match": {
        "uri": "!/index.php"
      },
      "action": {
        "share": "/opt/app/public$uri",
        "fallback": {
          "pass": "applications/data-exchange"
        }
      }
    }
  ],

  "applications": {
    "data-exchange": {
      "type": "php",
      "root": "/opt/app/public/",
      "script": "index.php",
      "user": "unit",
      "group": "unit"
    }
  }
}

index.php

<?php

$exchangeDirectory = '/data-store/' . date('Y-m-d');

echo sprintf('Check directory exists %s ... ', $exchangeDirectory);
if (!is_dir($exchangeDirectory)) {
    echo sprintf('not found - will create %s ... ', $exchangeDirectory);
    mkdir($exchangeDirectory, 0777, true);
}
echo 'done';
ac000 commented 2 months ago

Looks like you've got it sussed. Closing this now. Feel free re-open if required.