How to Change Owner of Files Created by Apache

By | July 18, 2008

If you manage any script that creates files on the server, you might meet the prob;em that you cannot edit files created by this script. This is especially related to PHP scripts, that, for example, create some text files in a folder. When you try to open these files via your FTP client, you will most probably receive “Permission Denied” error when you try to save your changes.

When you run your PHP scripts, that are creating files, they almost in all cases are started by user Apache. This user has all the permissions on the created files. So we have Apache as an owner and Apache as the Group. That’s why we don’t have access to these files – we simply don’t own them.

I know two different solutions for this problem. The first one is quite intelligent – to use suphp. This is an Apache module, that allows to run php by the user that initially owns the executed file. That’s a great solution as it is done for entire server and all users’ scripts will be affected.

Another solution is not so beautiful, It’s a “patch” for scripts that are actually running. If you don’t have suphp installed or don’t want to install it for any reason, you can use root crontab to chown files created by your scripts. For example, we will recursively chown a folder where script generated files are located. Just add the following line to your root crontab:

*/10 * * * * chown user:user /folder_to_chown

This is not quite a good idea but I don’t know any other solutions at the moment.