Using PHP with fcgid & suexec on Apache 2.2

In short

This page summaries a working setup of PHP on Apache, using fcgid and suexec. It has been tested with Apache 2.2 on a Debian Linux system.

Installation procedure

Customisation

You need to adapt the following values to your own setup:

  • user_account: the user account under which your PHP scripts will run.

  • user_group: the group that will be used to run your PHP scripts.

  • www-data: the user account used by Apache.

  • php_site: the directory under which your web site will be installed.

  • /usr/bin/php-cgi: the full path to your PHP interpreter1. It will need to support the FastCGI2 interface.

Apache 2 setup

Enable the fcgid et suexec Apache modules. If you’re using a Debian, this can be done with the following commands:

a2enmod suexec
a2enmod fcgid

Modifiy your virtuals hosts as follows:

<VirtualHost *>

DocumentRoot /var/www/user_account/php_site

[...]

Options None

SuexecUserGroup user_account user_group

<Directory /var/www/user_account/php_site>

  # The PHP scripts in this directory
  # are run through the php-cgi script.

  AddHandler fcgid-script .php
  FCGIWrapper /var/www/user_account/php-cgi .php
  Options ExecCGI
</Directory>

# We run a single PHP FastCGI server
# which will launch as many children
# as necessary.

DefaultMaxClassProcessCount 1
DefaultMinClassProcessCount 1

</VirtualHost>

You now need to check that you Apache is correctly set up:

apache2ctl -t

Directory structure creation

Unless you use the mod_userdir Apache module, your will need to work in a subdirectory of /var/www.

First, create the directories that will contain your site:

mkdir -p /var/www/user_account/php_site

Then copy your files in the php_site directory.

Wrapper script

Place the following script in a file called “php-cgi”, in the /var/www/user_account directory:

#!/bin/sh
export PHP_FCGI_CHILDREN=4
export PHP_FCGI_MAX_REQUESTS=500
exec /usr/bin/php-cgi $@

When this script is started by Apache, it will automatically be replaced by the php-cgi process. This process will start 4 chilren, to process the incoming requests. Once a child will have processed 500 requests, it will automatically shutdown and be restarted by the initial process. This feature is here to prevent a possible memory leak.

Your should adapt the number of children (PHP_FCGI_CHILDREN) to suit your needs. A lower value (e.g. 4) will be less demanding on your server. A higher value (e.g. 8) will enable your server to handle more connexions.

Permissions update

Apply the following permissions:

# User account directory

chown user_account:user_group /var/www/user_account
chmod u=rwx,g=rx,o=rx /var/www/user_account

# Wrapper script

chown user_account:user_group /var/www/user_account/php-cgi
chmod u=rwx,ug= /var/www/user_account/php-cgi

# Site

chown user_account:www-data /var/www/user_account/php_site
chmod -R u=rwX,g=rX,o= /var/www/user_account/php_site

Restart Apache

Everything should be OK now, you just need to restart Apache:

apache2ctl graceful

See also

  • The SUEXEC documentation of the Apache project.
  • The README.FastCGI file, which is included in the PHP sources.

for “(cgi-fcgi)” in its output.

License : CC-BY


  1. It will usually be called php4-cgi or php5-cgi. [return]
  2. You can check this by running the php-cgi -v command and looking [return]