Allowing PHP to write files in a Plesk server environment

As you may have found out, a web server that houses a bunch of web sites can cause some issues with file permissions when it comes to allowing PHP to write files that a web site needs, whether it is a data file, a document or an image. This is usually not an issue if PHP is installed as a cgi module because cgi programs are usually setup to run as the user that owns them, but Plesk uses PHP installed as an Apache module, and thus it runs as the same user that Apache runs as, which is usually not the same user that owns the website files.

How to solve this? It is really easy actually. There are 2 possible solutions, the first is the easiest, but really shouldn’t be considered and that is to make the folders that PHP needs to write to 0777, which opens a possible security hole and that is why you shouldn’t really consider it.

The second is to simply add the user apache uses, which under Plesk is usually “apache” to the group “psacln”. You can edit the /etc/group and /etc/group- files if you know what you are doing, or use usermod.

If you want to edit the files directly, locate the line that looks like this:

psacln:x:2524:

and just tack on “apache” to the end of it like so…

psacln:x:2524:apache

Do this for both files and restart apache and you are done.

Or, simpler

/usr/sbin/usermod -a -G psacln apache

Then simply make the folders and/or files that php needs access to 0775, which makes it more secure.

You can do similar with any server enviroment, we just happen to use Plesk here.

Rob

Bookmark and Share

This entry was posted on Thursday, March 27th, 2008 at 8:36 am and is filed under Code, Tips and Tricks, Web Development. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

 

Leave a Reply

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word