Nginx, PHP 5.3.10 and PHP-FastCGI (PHP5-FPM) on Ubuntu 10.04 (lucid)

The nginx is an open source Web server and a reverse proxy server for HTTP, SMTP, POP3 and IMAP protocols, with a strong focus on high concurrency, performance and low memory usage. Although commonly used to serve static content, it's quite capable of handling dynamic pages as well. This guide will help you get nginx up and running with PHP via FastCGI on your Ubuntu 10.04 (lucid) Linux VPS. To check the Ubuntu version type: lsb_release -a and press Enter.

Set the Hostname

Issue the following commands to make sure it is set properly:

hostname -f

The first command should show your short hostname, and the second should show your fully qualified domain name (FQDN).

Install PHP-FPM: PHP FastCGI Process Manager

PHP-FPM (PHP FastCGI Process Management) is a patch for PHP to improve PHP’s FastCGI capabilities and administration. It is bundled with the official PHP distribution since version 5.3.3.

cd /tmp 
aptitude install python-software-properties 
add-apt-repository ppa:brianmercer/php5
apt-get clean; apt-get update; apt-get upgrade; apt-get dist-upgrade

Install/Update PHP5

Issue the following commands to update your system and install the nginx web server, PHP, and compiler tools:

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

pear channel-update 
pear upgrade-all

Optional installation:

 apt-get install pdo pdo_sqlite sqlite 

Start PHP5-FPM

service php5-fpm start 

To check that PHP is listening on port 9000, run the following command:

netstat -ant | grep :9000


tcp        0      0*               LISTEN

Tweak the php-fpm configuration:

nano /etc/php5/fpm/php5-fpm.conf

The following tweaks have been customized for a 512MB-1GB VPS. The line "request_terminate_timeout = 30s" is optional but I highly suggest you use it. It's saying that if a php-fpm process hangs it will terminate it if it continues to hang for 30 seconds. This will add stability and reliability to php application in the event there is a problem.

pm.max_children = 25
pm.start_servers = 4
pm.min_spare_servers = 2
pm.max_spare_servers = 10
pm.max_requests = 500
request_terminate_timeout = 30s

Restart php5-fpm:

service php5-fpm restart

Install NGINX

Append the appropriate stanza to /etc/apt/sources.list. The Pgp page explains the signing of the released packaging. Ubuntu 10.04:

deb lucid nginx
deb-src lucid nginx     

Install Nginx:

apt-get clean; apt-get update; apt-get upgrade; apt-get dist-upgrade
sudo apt-get install nginx   

Installing new version of config file /etc/nginx/fastcgi_params ...
Installing new version of config file /etc/nginx/nginx.conf ...
Installing new version of config file /etc/nginx/mime.types ...
Installing new version of config file /etc/logrotate.d/nginx ...
Installing new version of config file /etc/init.d/nginx ...

Check the version of Nginx: nginx -v or dpkg –print-avail nginx

To start Nginx, run the following command:

service nginx start

To check that Nginx is listening on port 80, run the following command:

netstat -ant | grep :80


tcp        0      0*               LISTEN

Issue with starting Nginx.


nginx: [emerg] bind() to failed (98: Address already in use)

Check runung PID for port 80:

lsof -i tcp:80 or fuser -n tcp 80

Kill processes and run Nginx:

fuser -k 80/tcp ; service nginx restart

Tweak the nginx configuration:

nano /etc/nginx/nginx.conf
user www-data;
worker_processes  1;
error_log  /var/log/nginx/error.log warn;
pid        /var/run/;

events {use epoll; worker_connections  1024; multi_accept on;}

http {
    # Basic Settings
    client_max_body_size 20M;
    client_body_buffer_size 128k;

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

    sendfile    on;
    tcp_nopush  on;
    tcp_nodelay on;
    keepalive_timeout  65;
    types_hash_max_size 2048;
        # server_tokens off;
        # access_log off;
        # client_max_body_size 32m;
        # client_body_timeout 60;
        # client_header_timeout 60;
        # send_timeout 60;
        # reset_timedout_connection on;
    # Logging Settings error_log=[debug | info | notice | warn | error | crit]
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;
    error_log   /var/log/nginx/error.log info;

    # Gzip Settings
        gzip on;
        gzip_min_length 5000;
        gzip_buffers 16 8k;
        gzip_types text/plain text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript application/json;
        gzip_proxied any;
        gzip_comp_level 9;
        gzip_disable "msie6";
        gzip_vary on;
        gzip_http_version 1.1;

        gzip_static on;
        ignore_invalid_headers  on;

    # Virtual Host Configs
        upstream php {
       include /etc/nginx/sites-enabled/*;











This entry was posted in Technologies. Bookmark the permalink.