Ubuntu Hardy Nginx PHP5 FastCGI

Setup nginx, with php5 via FastCGI, and MySQL. The instructions below assume you have SU access and nginx already setup and running.

Install MySQL:

aptitude install mysql-server mysql-client libmysqlclient15-dev

Install PHP5

aptitude install php5-common php5-cgi php5-mysql php5-cli

To get PHP as FastCGI working with Nginx, we first have to spawn the fcgi process. There are a couple of different ways to do that. I used the spawn-fcgi app included with lighttpd. So we’re going to compile lighthttpd, but not install it.

wget http://www.lighttpd.net/download/lighttpd-1.4.19.tar.gz
tar xvzf lighttpd-1.4.19.tar.gz
cd lighttpd-1.4.18.tar.gz
./configure
make
cp src/spawn-fcgi /usr/bin/spawn-fcgi

Now we’ll create a script to launch the FastCGI process and have it listen on 127.0.0.1 port 9000, located at /usr/bin/php5-fastcgi

#!/bin/sh
/usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -u www-data -g www-data -C 2 -f /usr/bin/php5-cgi

Then we give it permissions:

chmod +x /usr/bin/php5-fastcgi

SymLink it to php-fastcgi:

ln -s /usr/bin/php5-fastcgi /usr/bin/php-fastcgi

Now we create a init.d script /etc/init.d/fastcgi

#!/bin/bash
PHP_SCRIPT=/usr/bin/php-fastcgi
RETVAL=0
case "$1" in
	start)
		echo "Starting fastcgi"
		$PHP_SCRIPT
		RETVAL=$?
  ;;
	stop)
		echo "Stopping fastcgi"
		killall -9 php5-cgi
		RETVAL=$?
  ;;
	restart)
		echo "Restarting fastcgi"
		killall -9 php5-cgi
		$PHP_SCRIPT
		RETVAL=$?
  ;;
	*)
		echo "Usage: php-fastcgi {start|stop|restart}"
		exit 1
  ;;
esac
exit $RETVAL

Give it permissions:

chmod 755 /etc/init.d/fastcgi

Start it up:

/etc/init.d/fastcgi start

And you should see something like:

Starting fastcgi
spawn-fcgi.c.197: child spawned successfully: PID: 19026

Now lets make it run on system boot:

update-rc.d fastcgi defaults

Create the FastCGI configuration file for nginx /usr/local/nginx/fastcgi.conf

fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx;
fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;
fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;

Make nginx pass the .php files to our FastCGI socket:

location ~ \.php$ {
 
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  /var/www/example.com/public_html$fastcgi_script_name;
    include        /usr/local/nginx/fastcgi.conf;
 
        }

Bling, you should be good to go. I’ll discuss MySQL tuning in another article.

1 comment so far ↓

#1 Not So Wise on 08.30.08 at 3:31 pm

[...] of below LEMP stack install steps are this, this and that.Follow solely anyone of them didn’t get me to where I want so below is a blend of [...]

Leave a Comment