Ubuntu Server 10.04 LTS + Nginx + PHP + MySQL

In a pursuit to optimise a customers server, we looked at the initial stack of software which runs the application. As it runs on a VPS and has limited resources, we decided on using Nginx – an extremely lightweight web server, instead of Apache which can be a resource hog. Below you will find my guide for installing Nginx 0.7.76, PHP 5.3, MySQL 5.1 and APC caching. Enjoy!

Let’s install Nginx

 sudo apt-get install nginx

Start your server and test:

/etc/init.d/nginx start

Update your nginx configuration (see later section!) /etc/nginx/sites-available/default (make a backup of the old one):

server {
listen   80 default;
server_name  localhost;

access_log  /var/log/nginx/localhost.access.log;

## Default location
location / {
root   /var/www;
index  index.php index.htm index.html;
}
## 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;
}

Visit the IP number in your web browser to make sure it’s installed ok “Welcome to nginx!“. Next, add to /etc/apt/sources.list:

 deb http://ppa.launchpad.net/brianmercer/php/ubuntu lucid main
deb-src http://ppa.launchpad.net/brianmercer/php/ubuntu lucid main

then run

sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 8D0DC64F
sudo apt-get update

Now let’s install PHP 5.3, and the things you need. Here’s my stack:

sudo apt-get install php5-cgi php5-mysql php5-curl php5-gd php5-idn php-pear php5-imagick php5-imap php5-mcrypt php5-memcache php5-mhash php5-ming php5-ps php5-pspell php5-recode php5-snmp php5-sqlite php5-tidy php5-xmlrpc php5-xsl php5-json php5-suhosin php5-common php-apc php5-dev

A few of these are replaced by php5-common – this is ok and I should update the stack at some point but it’s automatic!

Now we grab the PHP FPM + CGI Module:

sudo apt-get install php5-fpm php5-cgi

Restart Nginx + Fast CGI

sudo /etc/init.d/nginx restart

and you should get something like this:

ryan@localhost:~# sudo /etc/init.d/nginx restart
Restarting nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
configuration file /etc/nginx/nginx.conf test is successful
nginx.

Get CGI up:

/etc/init.d/php5-fpm start

Create an index.php in /var/www with and test your configuration. This was all working for me on Ubuntu Server amd64. Now it’s time to try and deploy an application to see how it can work :)

My nginx.conf file:

user www-data www-data;
worker_processes  4;

error_log  /var/log/nginx/error.log;
pid        /var/run/nginx.pid;

events {
worker_connections  1024;
# multi_accept on;
}

http {
include       /etc/nginx/mime.types;
default_type  application/octet-stream;

access_log  /var/log/nginx/access.log;

sendfile        on;
#tcp_nopush     on;

#keepalive_timeout  0;
keepalive_timeout  65;
tcp_nodelay        on;

gzip  on;
gzip_disable "MSIE [1-6]\.(?!.*SV1)";
gzip_comp_level 2;
gzip_proxied any;
gzip_types      text/plain text/html text/css application/x-javascript text/xml
application/xml application/xml+rss text/javascript;

include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}

I installed memcache and APC (beta works with 10.04 lucid but you need libpcre installed ). Select the [no] defaults:

sudo pecl install memcache
sudo apt-get install libpcre3-dev
sudo pecl install apc-beta

Didn’t need to modify any php.ini so far, but it is in 3 locations so I’ll see how this goes :-)

Update: Don’t forget to install postfix and configure it as an Internet Server if it was not part of your default installation. Then you can send emails…. Also, just to make sure… “ufw” the default firewall needs your attention ;)

sudo apt-get install postfix

Leave a Reply