I am interested in knowing the proper, yet security-conscious settings for a directory. Here's my scenario:

  1. I have a username for FTP access to my server called "user".
  2. For the purpose of the scenario, PHP runs as "nobody" on my server.
  3. I have a directory off the document root called "sample".
  4. The "sample" directory is chmod'd at 0755 (drwxr-xr-x)
  5. "Sample" is owned by "user" and the group is set to "user"

The above is all very straight forward and standard.

So I want to have a script be able to create (mkdir) and delete (rmdir) directories under "sample". Yet, I don't want to obviously overly expose my server by opening up the permissions (I could easily chmod sample to 0777 and make it world write-able).

What is the best combination of permissions, owner settings and/or group settings to allow my script to create and delete directories under "sample" while retaining the ability for "user" to continue to FTP into the directory?


  • Here's what I do have (and I've tested it so I know it works). drwxrwxr-x user nobody sample So the sample directory is chmod'd at 0775 with owner=user and group=nobody. Just curious if that poses any extraordinary security risks, or is there a better set of permissions to use? – H. Ferrence Dec 15 '09 at 16:44

The best would be to run the script as user "user". If you are using Apache, this can be done using the suEXEC module: http://httpd.apache.org/docs/2.2/suexec.html.

To use PHP, you have to run PHP as CGI. Usually, this means disabling mod_php, but eventually there is a way to switch between mod_php and php-cgi with different extensions (e.g. .cgi or .phpcgi). Haven't tried it, so it's just an idea.

  • Interesting concept, although it's out of my comfort zone of experience. Will think about it. Thanks andunix. – H. Ferrence Dec 15 '09 at 16:38

In situations like this I've used PHP's FTP client support to do the actual file work. So basically I let PHP do the uploading normally to a directory that it can write to. Then from there I use FTP locally to move the file to the appropriate location (after validating the file). This works best if you aren't exposing FTP to the outside world.

  • That would certainly work. I need something on a real time basis because the subsequent actions by the user are based on the presence of the directory. Plus I don't want to be on the hook manually each time a directory gets created in order to "review" so the user can continue on with using the system. – H. Ferrence Dec 15 '09 at 16:41

This might already be what your doing but, here is a thought. If the contents of sample are only to be accessed via FTP then you can move that directory outside the web server's document root and use your 0775 with owner=user and group=nobody plan. The php script will be able to write, user will be able to use FTP and the outside world will not be able to get at sample through the web server.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.