Nginx HTTP Server + PHP5 (With fast-cgi And xcache) On Ubuntu Feisty Fawn 

This HowTo describes the implementation of Nginx with php5 support (through FastCGI). The fast-cgi process will be initiated via spawn-fcgi.

What for? Nginx is a great replacement of Apache with very low memory footprint and contrary to Lighttpd, doesn't suffer from memory leak over time. You can then use all the memory left to unleash the power of mysql for instance by increasing the default query cache.

We have first to install php5 and, because we will need it later, build-essential.

 apt-get install php5-cli php5-cgi php5-xcache build-essential

Note that xcache has to be implemented manually by adding the following lines in the php.ini located in /etc/php5/cgi/ (please tune this config according to your system)

extension =
xcache.admin.user = "mOo"
; xcache.admin.pass = md5($your_password)
xcache.admin.pass = ""
; ini only settings, all the values here is default unless explained
; select low level shm/allocator scheme implemenation
xcache.shm_scheme =        "mmap"
; to disable: xcache.size=0
; to enable : xcache.size=64M etc (any size > 0) and your system mmap allows
xcache.size  =                64M
; set to cpu count (cat /proc/cpuinfo |grep -c processor)
xcache.count =                 1
; just a hash hints, you can always store count(items) > slots
xcache.slots =                8K
; ttl of the cache item, 0=forever
xcache.ttl   =                 0
; interval of gc scanning expired items, 0=no scan, other values is in seconds
xcache.gc_interval =           0
; same as aboves but for variable cache
xcache.var_size  =            64M
xcache.var_count =             1
xcache.var_slots =            8K
; default ttl
xcache.var_ttl   =             0
xcache.var_maxttl   =          0
xcache.var_gc_interval =     300
xcache.test =                Off
; N/A for /dev/zero
xcache.readonly_protection = Off
; for *nix, xcache.mmap_path is a file path, not directory.
; Use something like "/tmp/xcache" if you want to turn on ReadonlyProtection
; 2 group of php won't share the same /tmp/xcache
; for win32, xcache.mmap_path=anonymous map name, not file path
xcache.mmap_path =    "/dev/zero"

; leave it blank(disabled) or "/tmp/phpcore/"
; make sure it's writable by php (without checking open_basedir)
xcache.coredump_directory =   ""
; per request settings
xcache.cacher =               On
xcache.stat   =               On
xcache.optimizer =            On
; per request settings
; enable coverage data collecting for xcache.coveragedump_directory and xcache_coverager_start/stop/get/clean() functions (will hurt executing performance)
xcache.coverager =          Off
; ini only settings
; make sure it's readable (care open_basedir) by coverage viewer script
; requires xcache.coverager=On
xcache.coveragedump_directory = ""

You can do that right now even if your php configuration isn't loaded yet so everything will be in good order when Nginx and fcgi process will be started.

Next: we get the latest stable version of Nginx. The one proposed by Feisty is prehistoric one. Fortunately, there's a place you can get the latest stable version, or if you are adventurous, the latest dev version. So here we go:


(Note that if you are running on AMD replace i386 by amd64.)


 sudo dpkg -i nginx_0.5.32~grrr-1_i386.deb

Nginx is now up and running on default port 8000 (just in case you already have Apache or anything else on port 80).

The default root folder is Nginx-default and is located in /var/www/.

To change that and to start to listen to the fast-cgi we will launch next, you have to open /etc/nginx/sites-available/default.

 vi /etc/nginx/sites-available/default

You can find there all the obvious options to change and add (or uncomment the original php paragraph):

        location ~ \.php$ {
        include /etc/nginx/fastcgi_params;
        fastcgi_index index.php;
        fastcgi_param  SCRIPT_FILENAME  /var/www/nginx-default$fastcgi_script_name;

We've just asked Nginx to listen to fcgi on port 9000. So we have to start now the fcgi process. I've chosen to use spawn-fcgi and to make my own init script of it (so the process will start after reboot). To have spawn-fcgi you have to get lighttpd configured but without the need to install it. Let's grab the latest version:

 tar -xvjf lighttpd-1.4.18.tar.bz2 
 cd lighttpd-1.4.18
 cp src/spawn-fcgi /usr/bin/spawn-fcgi

Note that we did not type make install so lighttpd is not running!

Then we create a shell script we can call php-fastcgi or whatever you want and place that file in /usr/bin/ to make it simple (as php5-cgi and spawn-fcgi are already there...).

 touch /usr/bin/php-fastcgi

Then edit it:

 vi /usr/bin/php-fastcgi

and add the following:

/usr/bin/spawn-fcgi -a -p 9000 -u www-data -f /usr/bin/php5-cgi

That means every time this script will be called, fcgi will be spawned on port 9000 for user www-data (default user).

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

 touch /etc/init.d/init-fastcgi

Edit and add:

 vi /etc/init.d/init-fastcgi
case "$1" in
      killall -9 php
      killall -9 php
      echo "Usage: php-fastcgi {start|stop|restart}"
      exit 1
exit $RETVAL

You may have to change the permissions there by typing:

 chmod 755 /etc/init.d/init-fastcgi

Check then if it works by typing:

 /etc/init.d/init-fastcgi start

You should have an answer from spawn-fcgi attributing a PID process. To make now everything working after reboot type:

 update-rc.d init-fastcgi defaults

And we are done. To check if php is working as fast-cgi you can first type:

 ps ax | grep php

To check then if Nginx is listening to php, create an echo command in an empty php file:

<? echo phpinfo(); ?>

and place it in your Nginx directory. You should see then all your php conf along with the xcache module.

Share this page:

Suggested articles

7 Comment(s)

Add comment



Nice writeup guys.

 It would be great if you could add how to add eAccelorator to this setup.  That or create a new article and mash it up with the previous one:

Keep up the great work. 


it's my first howto. wrote it because i couldn't find any real and simple solution for having nginx + fcgi running automatically at startup. eaccelerator runs the same way on ubuntu than on etch.  i'm curious to compare the benchmarks between xcache and eaccelerator for nginx. 


Great Thanks!!!!!


Many thanks for this great article. I've found it very helpful.

I've installed nginx + FastCGI + PHP in Debian Etch. My PHP application is WordPress. Just in case someone needs it,  a rewrite rule is necessary in nginx.conf if we want WordPress to work properly:


        location / {
            root   /var/www/;
            index  index.php;
            if (-e $request_filename) {
            rewrite ^/(.+)$ /index.php?q=$1 last;

More information:

By: Denis

spawn-fcgi is definitely a bad choice for running php as fastcgi, look at the php-fpm instead.

By: Kuba


in newest Ubuntu versions you can install spawn-fcgi package without need of downloading lighttpd.


By: Initcron

I have automated nginx + php5 + php5-fpm setup on ubuntu 10.4 lucid with my scripted install. It is available freely under GNU GPL 3.0 at This takes care of most of the steps above.