Nov 19 2008

Prerequisites: apache with mod_rewrite and mod_fcgi, shell access (maybe root/sudo)

UBUNTU USERS – READ THIS FIRST!!

You might think compiling PHP, a scripted language, is a bit daft, but there are a few really good reasons you might want to compile your source code. In fact, compiled web code has been around for a while now, what with ASP.NET and all that. The main two reasons are (1) protecting your intellectual property if your project is closed-source and (2) potential speed increases. I say potential because that’s just a theory really – I’ve not investigated speed increases (or even decreases) yet, all I’ve done is compiled a test app and got it working.

I used Ubuntu as my compilation OS, because… well it’s awesome really*. I used this and this as the main guide, so read them and I’ll skim over the process I did here.

Firstly, you need to install and compile Bigloo (this will take 10 mins or so):

cd /usr/local/src
sudo wget ftp://ftp-sop.inria.fr/mimosa/fp/Bigloo/bigloo3.0c-4.tar.gz
sudo tar -xvzf bigloo3.0c-4.tar.gz
sudo rm bigloo3.0c-4.tar.gz
sudo chown -R root bigloo3.0c && sudo chmod -R u=rwx,go=rx bigloo3.0c
cd bigloo3.0c
sudo ./configure --prefix=/usr --enable-sqlite
sudo make && sudo make install

Then, install Roadsend (this will take 5 mins or so), ensuring FastCGI support is compiled in:

cd /usr/local/src
sudo wget http://code.roadsend.com/snaps/roadsend-php-2.9.7.tar.bz2
sudo tar -xvjf roadsend-php-2.9.7.tar.bz2
sudo rm roadsend-php-2.9.7.tar.bz2
sudo chown -R root roadsend-php-* && sudo chmod -R u=rwx,go=rx roadsend-php-*
cd roadsend-php-*
sudo ./configure --prefix=/usr/local
sudo make && sudo make install

Add this line into /etc/profile (or ~/.profile):

export LD_LIBRARY_PATH=/usr/local/lib

Go to your “source code” directory (e.g. /var/www/src), and create a script with this in:

#!/bin/sh
 
# Change this value to the name of your binary
APPNAME='testapp'
 
# Change this to your cgi-bin directory
CGIDIR='/usr/lib/cgi-bin'
 
# Compile using PCC
pcc -v --static -O --fastcgi $APPNAME `find ./ -name '*.php'`
 
# Copy fastcgi to
sudo rm $CGIDIR/$APPNAME.fcgi
sudo cp -v $APPNAME.fcgi $CGIDIR

You’ll need to modify the script – replace “testapp” with whatever you want the final binary to be called, and replace “/usr/lib/cgi-bin” with the location of your cgi-bin (you can find this by doing “grep -r ScriptAlias /etc/apache2″ on an Ubuntu system). If you don’t need to sudo to modify files in your cgi-bin directory (i.e. you have permissions in that directory), then you can remove the sudo’s from the beginning of the rm/cp commands. I need this as my cgi-bin is owned by root.

Now all you need to do, in the live directory, create a .htaccess with this in:

RewriteEngine On
RewriteRule ^(.+\.php)$ /cgi-bin/testapp.fcgi/$1
RewriteRule ^$ /cgi-bin/testapp.fcgi/index.php

This forwards requests like http://host/blah.php to http://host/cgi-bin/testapp.fcgi/blah.php, and the FastCGI module magically deals with it just as you would any other normal website… but it’s compiled!

The great thing about this is that all you need to provide in your release packages is:

  • The FastCGI module (e.g. testapp.fcgi)
  • The data files (images, javascript etc. that go in your web root)
  • If you’re nice, some instructions on how to set it up.

In theory, as it’s a FastCGI it should run on a multitude of web servers, and should be cross-platform compatible… but I’ve not tried it, so I can’t vouch for it yet!

* but not awesome at compiling FastCGI statically linked binaries.

Leave a Reply