Author Attribution

Last week I was searching for a bit of a performance boost for my site running on a Linode package. It's pretty OK, but not 'zippy' at all. During my search I came across a lot of interesting articles about Squid, lighttpd, Varnish and Nginx. EVO techblog has an article about using Nginx in combination with TYPO3 running a complex dynamic site. There are also articles about using Nginx as a load balancer that show pretty impressive improvement in system performance since the switch to Nginx. An article on drupal.org notes 300M of memory being freed after switching from Apache to Nginx.

After reading all that information, I was interested enough to give Nginx a go (Mental note to self; experiment more with emerging software and technologies). Nginx is a free, open-source, high-performance HTTP server and reverse proxy, as well as an IMAP/POP3 proxy server. Written by Igor Sysoev in 2005, Nginx now hosts between 1% and 4% of all domains worldwide (1, 2). Nginx is known for its high performance, stability, rich feature set, simple configuration, and low resource consumption.

This article will describe how to set up Nginx to run TYPO3 natively.

I run Debian on my system but the same setup should work for Ubuntu or any other platform or distro. First we get nginx:

apt-get install nginx

That was easy. Now configure nginx and start it. I just edited the /etc/nginx/sites-available/default file and changed the port number so that Nginx listens on port 8080. Then I started the server:

/etc/init.d/nginx start

That was easy too. Now you can admire the default nginx site on your server running on port 8080.

Start a php-cgi process

There is no nginx_php module so we will use php-cgi to serve php files. There are several ways to do this. The most elegant way I have found was adapted and made public by Till Klampaeckel. The original script can be found here: http://unix.derkeiler.com/Mailing-Lists/FreeBSD/questions/2007-09/msg00468.html. The adaption lives on here: http://till.klampaeckel.de/blog/archives/51-Ubuntu-nginx+php-cgi-on-a-socket.html. And there is even a git repository that stores the latest version.

Install php5-cgi:

apt-get install php5-cgi

Don't forget to check your /etc/php5/cgi/php.ini. It should be about the same as your /etc/php5/apache2/php.ini.

Get the php-fcgi super-duper-control thing (yes, that's what it is called, just check the source) and put the startup script in /etc/init.d/, 'chmod +x' it and don't forget it to use 'update-rc.d php-fcgid defaults' in case you want to start the processes when the server boots up.

Configure the script, set up the number of children and the user and group the children should run as. If you start up the service, a socket will be created in the directory you specified. You will need to note down the socket name because we will need it in the configuration of Nginx.

Create a TYPO3 Nginx configuration that uses php

We can use an existing document root for now, we're just running on another port.

server {
  listen 8080;
  server_name www.typofree.org;

  location / {
    root   /var/www/typofree.org;
    index  index.php;

    # serve exising files directly
    if (-f $request_filename) {
      break;
    }

    # if the file does not exist, pass it on to TYPO3
    if (!-f $request_filename) {
      rewrite .* /index.php last;
      return 200;
    }
  }

  # pass the PHP scripts to FastCGI server
  location ~ \.php$ {
    fastcgi_pass   unix:/var/run/php-fcgid/.fastcgi.www-data/socket;
    fastcgi_index  index.php;
    include        fastcgi_params;
    fastcgi_param  SCRIPT_FILENAME  /var/www/typofree.org/$fastcgi_script_name;
  }
} 

This configuration should do the trick. Adjust the paths where needed. Also change the location of the socket which may be different in your setup.

Restart Nginx

Now restart Nginx and you should be able to reach your site on port :8080. The site will be able to handle a realurl setup fine.

Next

  1. Rejoice
  2. Do some performance test (I'll write about that later)