How to run PHP files as another user with Apache and 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 1:

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 safe_mode)

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).

Solution 2:

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.