How to run PHP files as another user with Apache and FastCGI?

Posted on

How to run PHP files as another user with Apache and FastCGI? – Managing your servers can streamline the performance of your team by allowing them to complete complex tasks faster. Plus, it can enable them to detect problems early on before they get out of hand and compromise your business. As a result, the risk of experiencing operational setbacks is drastically lower.

But the only way to make the most of your server management is to perform it correctly. And to help you do so, this article will share nine tips on improving your server management and fix some problem about apache-2.2, php, fastcgi, , .

As per older questions, such as using mod_fcgid instead of mod_php and PHP as CGI or Apache Module? (and tangentially some other questions) I came to an understanding that running PHP as CGI or FastCGI would make my .php files be ran as the owner instead of the apache user (user that runs the Apache).

The phpinfo(); function states that Server API = CGI/FastCGI

However, when I test the running user it is still apache ie. <?php echo exec('whoami'); ?> returns apache.

I’ve also tried what Jeremy Bouse suggested in a question I’ve mentioned, but the PHP whoami still returns apache.

Should the fact that phpinfo’s “Server API” equals “CGI/FastCGI” be adequate for regular users to run the php-files as the owner?

(Does it even indicate that PHP is not running with mod_php?)

Or are there some admin tasks still to be done or something that is probably misconfigured?

Solution :

IMHO the (more recent and) best way to run PHP FastCGI is using PHP-FPM, which among the many options also permits to use different users for different websites.

From the official website:

Ability to start workers with
different uid/gid/chroot/environment
and different php.ini (replaces

But using FastCGI by itself is not enough to use the owner of the file (and there’s no such thing as “automatically using the owner of the file”: you have to actually configure it virtual host by virtual host (or as you wish).

Suexec can run cgi as users other than the apache user. If you want to run PHP like this you might also be interested in suPHP.

Leave a Reply

Your email address will not be published.