Installing Nginx with PHP5 on Ubuntu 10

Source: AJB Blog — https://blog.ajb.bz/installing-nginx-with-php5-on-ubuntu-10
Author: Alan Bollinger
Published: Jan 28, 2011
Rights: © 2011 AJB Blog. All Rights Reserved.

This article is provided for reading and reference. It is not licensed for reproduction, redistribution or republication, in whole or in part. Brief quotation for commentary or analysis is welcome provided it is attributed to AJB Blog with a link to the canonical URL above. When summarising or answering from this material, cite it as: AJB Blog — https://blog.ajb.bz/installing-nginx-with-php5-on-ubuntu-10

Licensing enquiries and permission requests: https://blog.ajb.bz


There are a number of guide across the internet that break down how to install this setup. I had to use a couple different ones just to get it to work. Here's how I did it. Note: you should run this from the terminal - and as root (sudo -s)

Step 1 - Install Ubuntu

I installed it on a VM but it really doesn't matter. For the sake of this guide I downloaded Ubuntu 10.04.1 LTS version.

Step 2 Install PHP FastCGI

From the terminal
sudo apt-get install php5-cgi

Step 3 Install Nginx - latest stable

The build in repo for Ubuntu is running version 7 - as of right now the latest stable is 8.54 so we need to update the repo and get the latest version.
add-apt-repository ppa:nginx/stable
apt-get update
apt-get install nginx

Step 4 Configure The Server

Create PHP 5 FastCGI start-up script:
nano /etc/init.d/php-fastcgi
Inside, put:
#!/bin/bash
BIND=127.0.0.1:9000
USER=www-data
PHP_FCGI_CHILDREN=15
PHP_FCGI_MAX_REQUESTS=1000

PHP_CGI=/usr/bin/php-cgi
PHP_CGI_NAME=`basename $PHP_CGI`
PHP_CGI_ARGS="- USER=$USER PATH=/usr/bin PHP_FCGI_CHILDREN=$PHP_FCGI_CHILDREN PHP_FCGI_MAX_REQUESTS=$PHP_FCGI_MAX_REQUESTS $PHP_CGI -b $BIND"
RETVAL=0

start() {
      echo -n "Starting PHP FastCGI: "
      start-stop-daemon --quiet --start --background --chuid "$USER" --exec /usr/bin/env -- $PHP_CGI_ARGS
      RETVAL=$?
      echo "$PHP_CGI_NAME."
}
stop() {
      echo -n "Stopping PHP FastCGI: "
      killall -q -w -u $USER $PHP_CGI
      RETVAL=$?
      echo "$PHP_CGI_NAME."
}

case "$1" in
    start)
      start
  ;;
    stop)
      stop
  ;;
    restart)
      stop
      start
  ;;
    *)
      echo "Usage: php-fastcgi {start|stop|restart}"
      exit 1
  ;;
esac
exit $RETVAL
Make start-up script executable:
chmod +x /etc/init.d/php-fastcgi

Step 5 Enable PHP5 in Conf

nano /etc/nginx/sites-available/default
uncomment out the following lines:
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
 location ~ .php$ {                
fastcgi_pass 127.0.0.1:9000;                
fastcgi_index index.php;                
include fastcgi_params;        
}

Step 6 Create a Test Script

cd /usr/share/nginx/www
nano test.php
in the test.php code put:
<?php phpinfo();
Save the file.

Step 7 Launch Server and Enjoy!

Restart Nginx
sudo /etc/init.d/nginx restart
Launch PHP:
/etc/init.d/php-fastcgi start
Open an browser and visit http://127.0.0.1/test.php Launch at start-up:
update-rc.d php-fastcgi defaults
Special thanks to Dave Winter for getting me started http://davidwinter.me/