Using PHP with FastCGI

Making PHP faster for busy sites

If you expect very heavy traffic for your site, you can improve the performance of your site by using FastCGI. FastCGI is faster because it reuses a single CGI process to complete multiple PHP requests, as opposed to starting a new CGI process for each page load. To find out why we use CGI and not mod_php, read this page.

Setup

First, you want to create the file /home/user/www/cgi/www.example.com/php.fcgi (replace user with your server username and www.example.com with your domain), and it should contain:

#!/bin/sh

exec /usr/bin/php5-cgi
Then, set the permissions for this file to 700 - instructions for this vary depending on how you access the server but if using an FTP client, these should be on the client's website. Then, create the file /home/user/www/www.example.com/.htaccess, or, if you already have a .htaccess file, edit it. Add the following lines to it:
AddHandler fcgid-script .php  
FcgidWrapper /home/username/www/cgi/www.example.com/php.fcgi .php

Testing

To verify that you have correctly configured your site, create a PHP test page somewhere on your site. The contents must be:

<?php
	print getmypid();
?>

Then, navigate to the page you created. When you refresh, the number shown should not change. If it does, FastCGI has not been configured correctly. If you have checked that you have followed the tutorial correctly but it still is not working, get it touch at support@mythic-beasts.com.