10 February 2011

How to Install MySQL, Nginx and PHP on Ubuntu 10.10

It took several attempts with information cobbled together from different blogs and tutorials, but here's how I managed to get MySQL, Nginx and PHP up and running on Ubuntu 10.10.


Step One: Install MySQL

sudo apt-get install mysql-server mysql-client

Step Two: Install PHP FastCGI

sudo apt-get install php5-cgi


Step Three: Install Nginx – (version 8.54 is the current stable)

add-apt-repository ppa:nginx/stable
apt-get update
apt-get install nginx


Step Four: Configure The Server

Create PHP 5 FastCGI start-up script:

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

Place the following in the file and save:

#!/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

Step Six: Make start-up script executable

sudo chmod +x /etc/init.d/php-fastcgi

Step Seven: Enable PHP5

sudo 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 Eight: Create a Test Script

cd /usr/share/nginx/www

sudo nano test.php

In the test.php code put:

Save the file.

Step Nine: Launch Server

Launch PHP:

sudo /etc/init.d/php-fastcgi start

Restart nginx

sudo /etc/init.d/nginx restart

Open an browser and visit http://127.0.0.1/test.php

You should be able to see something similar to the image included below.

Launch at start-up:
sudo update-rc.d php-fastcgi defaults