Best Served Cold


Posts Tagged ‘php5’

Using PHP CGI on a managed server

Recently we have had to change hardware for some of our sites due to high traffic rates, so we opted for a managed solution to cut down on the administration and responsibility of managing our own systems.

Whilst having a managed solution has many key advantages, it also provides a series of problems. The most prominent issue for us was how configurable the software was, both MySQL and PHP. The server in question was on MySQL version 4.0 which doesn’t allow sub queries, so we had to change the code to use joins instead, but more importantly, the PHP version was only 4 where we really needed version 5! The solution: a PHP CGI.

After creating a php.cgi and php.ini file in a /cgi-bin/ directory at a root level, we then needed to create an override in the apache configuration to utilise them. Not as easy as you might think when mixed with an MVC system with it’s own mod rewrite capability.

Here’s what we came up with:
.htaccess:

Options +FollowSymlinks
RewriteEngine on
RewriteBase /

RewriteCond %{HTTP_HOST} ^www\.(.*)
RewriteRule ^(.*) http://%1/$1 [R,L]

RewriteCond $1 !^(cgi-bin|index\.php|public|favicon\.ico|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]

AddHandler php-cgi .php
Action php-cgi /cgi-bin/php5.cgi

In a nutshell:

  • Turn on symbolic links, the rewrite engine and set the base directory.
  • Get rid of the www. extension (not really needed on any site any more in our opinion!)
  • Rewrite everything back to the index.php file apart from stuff that needs access remotely, including the all important “cgi-bin” directory.
  • Now we’ve got our file ready to process, push it through the cgi and there you have it, everything in PHP5!

If you needed better performance, you could consider using fastCGI, or ask your service provider to upgrade their PHP apache module.

Any questions, don’t hesitate to contact us.

Tags: , , , , , , ,