NGINX, WordPress and php-fpm on Debian Squeeze

A while ago i published a howto on running nginx+serendipiy+php-fpm with apache style rewriting as implemented in s9y. The described installation is obsolete now – php-fpm made the jump into the regular php versions beginning with php 5.3.3 (see here). In addition to that i made s9y obsolete on my blog and migrated it to wordpress.
The netcraft webserver survey from April 2011 shows that nginx is still increasing slightly and is currently providing access to 8,68% of all monitored domains:

Netcraft Webserver survey april 2011
Time for me to switch to nginx again :)

Manual compiling is cool, but also a bit old-school, that’s why we use packages from Dotdeb in this example:

  1. Add the following two lines into your /etc/apt/sources.list file (example for debian squeeze, see lenny instructions here:

  2. deb stable all
    deb-src stable all

  3. fetch and apply GnuPG key:

  4. wget
    cat dotdeb.gpg | sudo apt-key add -

  5. refresh your sources

  6. apt-get update

  7. Install packages including php5-fpm and nginx:

  8. apt-get install php-apc php-auth php-net-smtp php-net-socket php-pear php5 php5-cgi php5-cli php5-common php5-curl php5-dev php5-gd php5-imagick php5-imap php5-mcrypt php5-mysql php5-pspell php5-sqlite php5-suhosin php5-xmlrpc php5-xsl php5-fpm nginx

  9. First of all i did some modifications (gzip compression and tcp tweaks) to the nginx.conf located in /etc/nginx/nginx.conf

  10. #/etc/nginx/nginx.conf
    worker_processes 2;
    tcp_nopush on;
    tcp_nodelay on;
    # Gzip Settings
    gzip on;
    gzip_disable "msie6";
    gzip_vary on;
    gzip_proxied any;
    gzip_comp_level 1;
    gzip_buffers 16 8k;
    gzip_http_version 1.1;
    gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

  11. Thanks to the new try_files directive in nginx the vhost configuration (found in /etc/nginx/sites-available) for wordpress is fairly straight forward. (In addition Igor often mentioned that using the if directive in nginx equal to apache usage is evil.). Example vhost configuration code for my blog:

    server {
    listen :80;
    server_tokens off;
    root /var/www/virtual/;
    index index.php index.html index.htm;
    access_log /var/log/nginx/;
    error_log /var/log/nginx/;

    location / {
    index blog/index.php ;

    location /blog/ {
    try_files $uri $uri/ /blog/index.php?q=$uri;

    location ~ \.php$ {
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_pass unix:/dev/shm/php-fastcgi.socket;

    location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
    expires max;
    log_not_found off;
    gzip on;

    please be aware that the blog in the example is running from the sublocation /blog.

  12. php-fpm configuration is located in /etc/php5/fpm. The file php-fpm.conf is the main configuration file which includes everything in the subdirectory pool.d, where i made some modifications to the default www.conf pool file:

  13. #/etc/php5/fpm/pool.d/www.conf
    ;listen =
    listen = /dev/shm/php-fastcgi.socket
    php_flag[display_errors] = off
    php_admin_value[error_log] = /var/log/fpm-php.www.log
    php_admin_flag[log_errors] = on
    php_admin_value[error_reporting] = 0
    php_admin_value[memory_limit] = 128M
    php_admin_value[date.timezone] = Europe/Berlin

    As you see i configured only one pool which is running through the unix socket in /dev/shm/php-fastcgi.socket, which should be a bit faster than via local TCP socket. In addition i didn’t change any other performance values in first place (Blog is running with pm = dynamic, pm.max_children = 50, pm.start_servers = 20, pm.min_spare_servers = 5, pm.max_spare_servers = 35, pm.max_requests = 0 which is a lot more than ever needed).
    Finally i changed some php_admin_flags and values accordingly.

  14. Start php-fpm

  15. /etc/init.d/php5-fpm start

  16. Start nginx

  17. /etc/init.d/nginx start

Now thats really a easy and fast way to install nginx with php-fpm on debian squeeze. This configuration is now running for several weeks on this domain and the pingdom monitoring shows an average response time improvement of about 300ms. In addition the machine now has a lot more reserves considering RAM and CPU.
Coming up next:
Adapting this changes to the piwik and gallery3 installations running here, integration of varnish-cache in this setup.

Related Posts: