PHP Hints

January 10, 2009

Improve php and apache2 performance with apache2-mpm-worker + mod_fcgid + php5-cgi

Filed under: Apache2, Ubuntu, fastcgi — kkruecke @ 9:00 pm
Tags: , , , , ,

Installing Apache2 and PHP5 using mod_fcgid and Apache2/workerMPM/FastCGI/PHP5 shows how to run php5 with apache2-mpm-worker, the multi-thread version of Apache, using the FastCGI module mod_fcgid.

Step 1. Install the Packages

 # apt-get install apache2-mpm-worker  libapache2-mod-fcgid
 # aptitude install php5-cgi php5-cli

Step 2. Enable fcgid

  # sudo a2enmod fcgid

If not needed, disable cgid

  # sudo a2dismod cgid

Step 3. Create a Basic Configuration File

Add the following to /etc/apache2/httpd.conf .

<Directory /var/www>
AddHandler fcgid-script .php
FCGIWrapper /usr/lib/cgi-bin/php5 .php
Options +ExecCGI
</Directory>
Alias /aptitude        /usr/share/doc/aptitude/html/en
Alias /apt                /usr/share/doc/apt-doc
Alias /phpgedview   /usr/share/phpgedview

# Provide php support for the Aliases (any php scripts in /usr/share subdirectories)
<Directory /usr/share>
AddHandler fcgid-script .php
FCGIWrapper /usr/lib/cgi-bin/php5 .php
Options ExecCGI FollowSymlinks Indexes
</Directory>

Step 4. Add ExecCGI to each Virtual Host

For each virtual host you have in /etc/apache2/sites-available, add the ExecCGI option. Prefixing ExecCGUI with a plus sign will add it to the current Options enforce:

<VirtualHost *:80>
ServerName     sampledomain.com
ServerAdmin     webmaster@sampledomain.com
ServerAlias       www.sampledomain.com *.sampledomain.com
DocumentRoot   /var/www/sampledomain.com
ErrorLog           /var/log/apache2/sampledomain.com-error.log
CustomLog        /var/log/apache2/sampledomain.com-access.log combined
<Directory     /var/www/sampledomain.com>
# Add additional options you need, but be sure you have ExecCGI. For example:
# Options   ExecCGI   Indexes  FollowSymlinks  MultiViews
Options     +ExecCGI
</Directory>
LogLevel   warn
ServerSignature   On
</VirtualHost>

Step 5. Enable your Virtual Host(s)

 # suod a2ensite sampledomain.com

Step 6. Reload the Apache Configuration

 # sudo /etc/init.d/apache2 force-reload

Step 7. A simpler way

The article Using PHP with mod_fcgid on the Typo3 website, shows how to use a <Location> block to set the ExecCGI option. This eliminates the need of putting

AddHandler fcgid-script .php
FCGIWrapper /usr/lib/cgi-bin/php5 .php
Options +ExecCGI
# (other Options can, of course, be specified)

in every <Directory> block. But you will need to enable the Actions module.

 # a2enmod actions

As Using PHP with mod_fcgid shows, create /etc/apache2/conf.d/php-fcgid.conf

<IfModule mod_actions.c>
<IfModule mod_alias.c>
<IfModule mod_mime.c>
# Path to php.ini – defaults to /etc/phpX/cgi
DefaultInitEnv PHPRC=/etc/php5/cgi
# Number of PHP childs that will be launched. Leave undefined to let PHP decide.
# DefaultInitEnv PHP_FCGI_CHILDREN 3
# Maximum requests before a process is stopped and a new one is launched
 DefaultInitEnv PHP_FCGI_MAX_REQUESTS 5000
# Define a new handler "php-fcgi" for ".php" files, plus the action that must follow
AddHandler php-fcgi .php
Action php-fcgi /fcgi-bin/php-fcgi-wrapper
# Define the MIME-Type for ".php" files
AddType application/x-httpd-php .php
# Define alias "/fcgi-bin/". The action above is using this value, which means that
# you could run another "php5-cgi" command by just changing this alias
Alias /fcgi-bin/ /var/www/fcgi-bin.d/php5-default/
# Turn on the fcgid-script handler for all files within the alias "/fcgi-bin/"
<Location /fcgi-bin/>
SetHandler fcgid-script
Options +ExecCGI
</Location>
</IfModule>
</IfModule>
</IfModule>

Then create the directory which is chosen by the Alias, and a symbolic link to the php5-cgi binary.

 # sudo mkdir -p /var/www/fcgi-bin.d/php5-default
 # sudo ln -s /usr/bin/php5-cgi /var/www/fcgi-bin.d/php5-default/php-fcgi-wrapper

We can remove the ExecCGI, AddHandler and FCGIWrapper entries from /etc/apache2/httpd.conf

<Directory /var/www>
Options +FollowSymlinks
DirectoryIndex index.php index.html index.htm
Order allow,deny
Allow from all
</Directory>
Alias /aptitude        /usr/share/doc/aptitude/html/en
Alias /apt                /usr/share/doc/apt-doc
Alias /phpgedview   /usr/share/phpgedview
<Directory /usr/share>
Options FollowSymlinks Indexes
</Directory>

And restart Apache2.

 # sudo /etc/init.d/apache2 restart

Considerations with an php opcode cacher

If you are using a PHP opcode cacher it is desireable to run a single PHP FastCGI Server which will lauch as many children as necessary (see linode.com discussion thread). This aticle Using PHP with fcgid & suexec on Apache 2.2 shows how you do this: set DefaultMaxClassProcessCount and DefaultMinClassProcessCount to 1. So /etc/apache2/conf.d/php-fcgi.d

<IfModule mod_actions.c>
<IfModule mod_alias.c>
<IfModule mod_mime.c>
# We run a single PHP FastCGI server which will launch as many children
# as necessary.
DefaultMaxClassProcessCount 1
DefaultMinClassProcessCount 1

# Path to php.ini – defaults to /etc/phpX/cgi
DefaultInitEnv PHPRC=/etc/php5/cgi
# Number of PHP childs that will be launched. Leave undefined to let PHP decide.
# DefaultInitEnv PHP_FCGI_CHILDREN 3
# Maximum requests before a process is stopped and a new one is launched
 DefaultInitEnv PHP_FCGI_MAX_REQUESTS 5000
# Define a new handler "php-fcgi" for ".php" files, plus the action that must follow
AddHandler php-fcgi .php
Action php-fcgi /fcgi-bin/php-fcgi-wrapper
# Define the MIME-Type for ".php" files
AddType application/x-httpd-php .php
# Define alias "/fcgi-bin/". The action above is using this value, which means that
# you could run another "php5-cgi" command by just changing this alias
Alias /fcgi-bin/ /var/www/fcgi-bin.d/php5-default/
# Turn on the fcgid-script handler for all files within the alias "/fcgi-bin/"
<Location /fcgi-bin/>
SetHandler fcgid-script
Options +ExecCGI
</Location>
</IfModule>
</IfModule>
</IfModule>

No Comments Yet »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

Blog at WordPress.com.