Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a PHP-script in which I'm trying to unlink files from a directory. The files in this particular directory have been uploaded using FTP under a different account than the one Apache is running under.

PHP runs on the server using mod_php, so I assume PHP scripts are executed as the same user Apache is running as. Probably because of this, the unlink fails.

I am running Apache 2.2.3 with PHP 5.1.6 on CentOS. Most of this is administered by Plesk 8. I do have full access to the shell though.

I have tried to create a new group in which I put the FTP user and the Apache user. I then changed the GID of the files to that new group so that PHP should be able to unlink the files. No success.

I have read about options like suPHP en suEXEC, but my knowledge of those products is limited.

What would be the best way to be able to remove those files using a PHP script?

share|improve this question
up vote 1 down vote accepted

When I have to things from PHP that require root privileges on a system that has mod_php I usually write a small helper script to actually do the task. Then I use system() or proc_open() to call the script via sudo. You will need to configure your apache user account to be able to run that particular script with no password.

If the system has the cli version of php installed then you can write your helper script in PHP since it sounds like that is familiar to you.

/etc/sudoers looks like this:

Cmnd_Alias PHPHELPER=/path/helper-script 

The advantage of doing this is that you can write a very tight and specific helper script that only performs one task, and you can add lots of good error checking to prevent badness. This minimizes the potential for a system compromise.

share|improve this answer

It's possible that if you grant the user apache runs as write-permissions to the directory the files are located in, php/apache will be able to unlink them.

Permissions to unlink/delete a file are (usually) not governed by the ownership(s) and permissons of the actual file, but rather the permissions/ownerships of the directory the files are in. The exception to this is if the directory has the sticky bit set, then only the file owner or root can delete files.

share|improve this answer

Though I don't like the way that this sounds as a prospective user/administrator, I think the best solution (in terms of security) would be to use php-cgi to execute/run PHP scripts/pages in conjunction with the mpm_itk module. mpm_itk lets apache drop privileges into a specific user account (and therefore would be able to delete the files you need it to delete). The problem is that mod_php won't run in this situation, and you have to run php as CGI.

This is really the only way to run PHP in a sane manaer where PHP executes as a specific user rather than the apache-user. While mod_php is easy and it works quite well, situations like the one you raise are an issue, particularly in multi-user and shared environments.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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