Install And Configure Nginx, PHP, MySQL On Ubuntu

Nginx is a simple yet powerful web server. It is widely known for being a stable web server. If it is well configured, Nginx rarely has spikes in CPU load and has low memory consumption. Nginx is known for its stability, rich feature set, simple configuration, and low resource consumption.

In this article I want to share how to install and configure your Ubuntu server to use Nginx as web server, PHP5 support (using FastCGI) and MySQL support.

Install MySQL server

Open your SSH terminal.

Install MySQL:

sudo apt-get install mysql-server mysql-client

Install and Configure Nginx

Still in your SSH terminal.

Update your repositories list:

sudo apt-get update

Install Nginx:

sudo apt-get install nginx

Nginx configuration file is under /etc/nginx/sites-available/default, and it should be changed to:

server {
    listen   80;
    server_name  localhost;
    access_log  /var/log/nginx/localhost.access.log;

## Default location
    location / {
        root   /var/www;
        index  index.php;
    }

## Images and static content is treated different
    location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|xml)$ {
      access_log        off;
      expires           30d;
      root /var/www;
    }

## Parse all .php file in the /var/www directory
    location ~ .php$ {
        fastcgi_split_path_info ^(.+.php)(.*)$;
        fastcgi_pass   backend;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /var/www$fastcgi_script_name;
        include fastcgi_params;
        fastcgi_param  QUERY_STRING     $query_string;
        fastcgi_param  REQUEST_METHOD   $request_method;
        fastcgi_param  CONTENT_TYPE     $content_type;
        fastcgi_param  CONTENT_LENGTH   $content_length;
        fastcgi_intercept_errors        on;
        fastcgi_ignore_client_abort     off;
        fastcgi_connect_timeout 60;
        fastcgi_send_timeout 180;
        fastcgi_read_timeout 180;
        fastcgi_buffer_size 128k;
        fastcgi_buffers 4 256k;
        fastcgi_busy_buffers_size 256k;
        fastcgi_temp_file_write_size 256k;
    }

## Disable viewing .htaccess & .htpassword
    location ~ /.ht {
        deny  all;
    }
}
upstream backend {
        server 127.0.0.1:9000;
}

Install and Configure PHP5 with FastCGI

Install spawn-fcgi with the following command:

sudo apt-get install spawn-fcgi

Install PHP:

sudo apt-get install php5 php5-cli php5-common php5-suhosin php5-cgi php-pear php5-mysql

Now let’s write a script which will spawn FastCGI PHP processes on a Unix domain socket. We create the file /usr/sbin/fastcgi-php.

sudo touch /usr/sbin/fastcgi-php

Then write this into the file:

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

To make it work at startup we need to create an init script:

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

Then write the following:

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

Give the script execute permission:

sudo  chmod 755 /etc/init.d/init-fastcgi
#then run it
/etc/init.d/init-fastcgi start

Now let’s start it on boot:

sudo  update-rc.d init-fastcgi defaults

Restart Nginx:

sudo /etc/init.d/nginx restart

Test it with phpinfo(). If you see the result now everything is okay, but if you don’t please revise the steps you have done.

PS: these steps work for me on my machine with Ubuntu 10.04 server edition. I’m not guaranteeing that this will work for you too, but that’s the basics to install Nginx, PHP, MySQL on Ubuntu. If you are not successful maybe you just need to check the configuration to get it working. I couldn’t get it running on the first try. So keep trying, it’s a learning process.

Related article:
[HowTo] Install Apache, PHP, MySQL, and PHPMyAdmin on Ubuntu