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

13 thoughts on “MySQL, nginx and PHP on Ubuntu 8.04

  1. Hi,

    I’m running into some trouble following your tutorial:

    A83 ~/sources: ./configure
    -bash: ./configure: No such file or directory

    I’ve followed the tutorial exactly up to that point. Any idea what I should do?

    Thanks in advance!

    Like

  2. Ahh, I figured out that I had to enter the lighttpd directory, so now ./configure and make worked!

    But I ran into another problem, when I try to start nginx I get this error:

    A83 /: sudo /etc/init.d/nginx start
    Starting nginx: 2008/06/08 14:01:27 [emerg] 22231#0: unknown directive “includefastcgi_params” in /etc/nginx/sites-enabled/default:40

    Like

  3. Ok, this is starting to become silly. Saw that my nginx config file and yours didn’t looked exactly the same. Copied yours and made nginx start!

    But it seams like there are more to this:

    When I goto my IP the welcome to nginx text is showing. But if I turn my browser to test.php I get this text:

    The page you are looking for is temporarily unavailable.
    Please try again later.

    Like

  4. Yes you need to change to the correct directory – I’ve edited the tutorial to make that clearer.

    Line 40 in my default file reads:

    include /etc/nginx/fastcgi_params;

    Like

  5. Ok this could be several things –

    Check first that php cgi process is running correctly.

    ps aux

    You should see several processes /usr/bin/php5-cgi under COMMAND.

    If not then check again that these scripts are correct:

    /usr/bin/php-fastcgi
    /etc/init.d/init-fastcgi

    If you do have the php5-cgi processes then check the nginx config files again to make sure that it is routing requests for .php files correctly.

    Finally check again test.php has correct syntax and is in the correct directory – also that the user permissions are correct if you aren’t running nginx as root.

    I’m not sure right now what else could be the problem.

    Like

  6. Thanks for the assistance!

    I ran ps aux but didn’t found any processes so I redid that part of the tutorial so now they’re there.

    I’ve set up a vhost to my domain (following this tutorial: http://articles.slicehost.com/2008/5/16/ubuntu-hardy-nginx-virtual-hosts). And my index.html works fine, but if I try to run the test.php I’ve uploaded in the same directory I get dialog to download the file instead of opening it. Can I state that somewhere in the nginx config what file extentions it shall run?

    The file is here: http://a83.se/test.php

    Thanks a lot in advance!

    Like

  7. I would check again this part of the nginx config for your domain that begins:

    location ~ \.php$ {

    This is what matches urls with .php extension and passes them to fastcgi – in your case this is not happening for some reason.

    Like

  8. This error means php is not getting the file for some reason.

    Check the SCRIPT_FILENAME param in your nginx configuration file has the correct path for your install:

    /var/www/nginx-default$fastcgi_script_name;

    Failing that it could be a permissions problem – the spawn-fcgi process runs as user ‘www-data’ in my example, so check that the files are readable.

    Like

  9. Hi,
    I tried and tried, notyhing:
    1) I set permission on spawn-fcgi by “sudo chmod 755 /usr/bin/php-fastcgi”
    2) I found that post http://forum.slicehost.com/comments.php?DiscussionID=1259 that has solved by add “-g www-data” into /usr/bin/php-fastcgi … from “/usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -u www-data -f /usr/bin/php5-cgi” to “/usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -u www-data -g www-data -f /usr/bin/php5-cgi”
    3) I reboot slice …

    Nothing

    I think I have to change web server ….

    Like

  10. When i want to start fastcgi by using “sudo /etc/init.d/init-fastcgi start” then i get “/usr/bin/php-fastcgi: 2: /usr/bin/spawn-fcgi: Permission denied” what is wrong , pls Hlp.

    Like

Leave a comment