MySQL, nginx and PHP on Ubuntu 8.04

The nginx webserver is a lightweight alternative to apache and is perfect for running on a vps where resources are scarce. First we need to install our packages.

sudo apt-get install mysql-server mysql-client libmysqlclient15-dev

I am using apt-get instead of aptitude here so we don’t get a lot of unwanted dependencies installed. The MySQL install will prompt you for a password for the root user. Remember this is not the same as the root login for your server and is just for master access to the MySQL databases.

sudo aptitude install php5-cli php5-cgi php5-mysql php5-xcache
sudo aptitude install nginx

Configure fastcgi

To enable php with nginx we need to edit the nginx configuration file to use fastcgi.

sudo nano /etc/nginx/sites-available/default

On my install the necessary lines were already present and just needed to be uncommented.

location ~ .php$ {
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME /var/www/nginx-default$fastcgi_script_name;
    include /etc/nginx/fastcgi_params;
}

While you are editing the config file you should change the server name to match your domain. The file fastcgi_params was installed as part of nginx.

Spawn-fcgi

Next we need a way to spawn our fastcgi processes as needed. The webserver Lighttpd comes with a script to do that which we can use. Download the source and build it to get the binary that we need.

mkdir ~/sources
cd ~/sources
wget http://www.lighttpd.net/download/lighttpd-1.4.19.tar.bz2
tar jxvf lighttpd-1.4.19.tar.bz2

I found that the configure script complained about some missing dependencies so install those first.

sudo aptitude install libpcre3-dev libbz2-dev

You can probably make it without them but having development headers is useful if you plan to do much building from source. Change to the directory you just unpacked and build the binary with configure and then make.

lighttpd-1.4.19
./configure
make

All we need is the spawn-fcgi binary.

sudo cp src/spawn-fcgi /usr/bin/spawn-fcgi

Now we need a simple script to run it.

sudo nano /usr/bin/php-fastcgi

Copy the following into that file.

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

We can also use another script to control the startup.

sudo nano /etc/init.d/init-fastcgi

Copy the following into that file.

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

We need to change the permissions on these two scripts so they are executable.

sudo chmod 755 /usr/bin/php-fastcgi
sudo chmod 755 /etc/init.d/init-fastcgi

Startup our scripts

Now we can start our fastcgi script and add it to our init script so it starts automatically on boot.

sudo /etc/init.d/init-fastcgi start
sudo update-rc.d init-fastcgi defaults

Don’t forget to start the webserver.

sudo /etc/init.d/nginx start

If you want to test php is working, just create a simple php file.

sudo nano /var/www/nginx-default/test.php

Just one line should do it.

<?php echo phpinfo(); ?>

Open this page in your browser and check that php is working as it should.

Now that we have a working ‘LEMP’ stack we can put up some content. My next post will describe how I installed WordPress.

References:
http://jit.nuance9.com/2008/01/serving-php5-with-nginx-on-ubuntu-710.html

http://articles.slicehost.com/2007/9/10/ubuntu-lts-mysql-and-ror


28 comments on “MySQL, nginx and PHP on Ubuntu 8.04


Leave a Reply