This is an init script written in a Debian like way for running PHP FastCGI securely and efficiently.
Contrary to an widespread Urban Legend there’s no need for the
spawn-fcgi
program from Lighttpd
for the PHP FastCGI process to
spawn new instances when necessary. This script is all you need to
run the PHP FastCGI daemon.
- Use of an
/etc/default/php-cgi
file that specifies whether to use UNIX or Internet sockets. The default is UNIX. - PHP CGI process runs as the unprivileged user and group
www-data
. - start, stop, restart, force-reload and status actions.
- Install the
default/php-cgi
in/etc/default
. Note that I’m assuming a Debian like layout of the configuration files. - Install the
php-cgi
file in/etc/init.d
. - Enable the
php-cgi
init script in the system run level configuration by issuing:update-rc.d php-cgi defaults 99
.This means that
php-cgi
will be started in run levels2, 3, 4
and5
with a priority of 99 when launching all the processes for the specified run level. - Start the PHP FastCGI daemon with the command:
/etc/init.d/php-cgi start
Now if you invoke
/etc/init.d/php-cgi status
the script will tell if the process is running or not. If it is, then a/tmp/php-cgi
directory with permissions700
will appear. This is where the UNIX sockets are created. - Now run your web server or whatever process you want to relay requests to the PHP FastCGI daemon. Every thing should work.
- You can tune the
FastCGI
parametersPHP_FCGI_CHILDREN
andPHP_FCGI_MAX_REQUESTS
to whatever values work best for you. They’re defined in the/etc/default/php-cgi
file.
I’ve published a Monit configuration file for php-cgi on github.
If you’re using the PHP FPM (FastCGI Process Manager) the script
can be used almost verbatim. Just change the value of DAEMON
to
php-fpm
and you’re done. Note that a lot of niceties like
reloading of the configuration are missing.
The script is signed with my GPG key. Just do gpg --keyserver
keys.gpg.net --recv-keys 4D722217
. Verify by issuing gpg --verify
php-cgi.sig
.
This script steals heavily from the Nginx Wiki PHP FastCGI example.