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


 
 
 
  • http://a83.se A83

    Hi,

    I’m running into some trouble following your tutorial:

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

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

    Thanks in advance!

  • http://a83.se A83

    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

  • http://a83.se A83

    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.

  • matt

    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;

  • matt

    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.

  • http://a83.se A83

    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!

  • matt

    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.

  • Carlo

    Hi,
    I followed yours fantastic guide but when I call test.php file the reponse is:
    No input file specified.

    Why ? Can you help me ?

    Test yourself
    http://209.20.69.80/test.php

  • http://bc-dev.net matt

    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.

  • Carlo

    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 ….

  • Pingback: trivialview » Blog Archive » Level 1: Webserver & PHP

  • Herman

    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.

  • http://bc-dev.net matt

    Hi Herman, sorry I don’t know what could be causing that, maybe your web server is running a different user to spawn-fcgi?

  • Pingback: Install wordpress pada JeOS server menggunakan Nginx, PHP, MySQL « Just [invaleed]

  • yahbluez

    There ist a bug on your init script:

    wrong two lines with:

    killall -9 php

    right:

    killall -9 /usr/bin/php5-cgi

  • Pingback: Light weight alternatives to Apache | Web Development | PHP Development | Zend Development | Flex Development | Air Development | AJAX Development | JavaScript Development | ActionScript Development

  • Alex

    How to install Nginx with PHP as FastCGI you can find here
    http://www.linuxspace.org/archives/1576 on the Ubuntu 9.04
    I used it for my blog

  • Pingback: Light Weight Alternatives to Apache - Buy-Host forum

  • Jwxie

    You can follow this one exactly the same – no need for any changes someone mentioned above.

    One thing: keep in mind if you are working with Ubuntu Server with command line by default, you should install the followings:

    sudo apt-get install make
    sudo apt-get install build-essential

    nano is not a good editor IMO. Use VIM if possible.
    sudo apt-get install vim-full

    Press I key to insert, press Esc to exit to normal mode, then :wq to save and quit or :w just to save, or :q just to quit

    search online :)

  • Pingback: Getting a Website is Easy » Blog Archive » Light weight alternatives to Apache

  • Pritesh Loke

    Nice article i’ll put on my blog too with your site reference.

  • Pingback: MySQL, nginx and PHP on Ubuntu 8.04 « Em44y’s Blog

  • http://bit.ly/nginx-ubuntu Kuba

    Hi, in newest Ubuntu versions you no longer need to download lighttpd as there’s a spawn-fcgi package available in apt repositories.

  • Pingback: Level 1: Webserver & PHP | trivialview

  • http://boleh.net.id ruhan

    after you follow this guide try to run this :

    #ps ax | grep php

    2108 ? Ss 0:00 /usr/bin/php5-cgi
    2163 ? S 0:00 /usr/bin/php5-cgi
    2164 ? S 0:00 /usr/bin/php5-cgi
    2165 ? S 0:00 /usr/bin/php5-cgi
    2166 ? S 0:00 /usr/bin/php5-cgi
    2167 ? S 0:00 /usr/bin/php5-cgi
    2201 pts/0 S+ 0:00 grep php