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:

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:
- Add the following two lines into your /etc/apt/sources.list file (example for debian squeeze, see lenny instructions here:
- fetch and apply GnuPG key:
- refresh your sources
- Install packages including php5-fpm and nginx:
- First of all i did some modifications (gzip compression and tcp tweaks) to the nginx.conf located in /etc/nginx/nginx.conf
- 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:
#/etc/nginx/nginx.conf
server {
listen :80;
server_name andreas-lehr.com;
server_tokens off;
root /var/www/virtual/andreas-lehr.com/htdocs;
index index.php index.html index.htm;
access_log /var/log/nginx/andreas-lehr.com/access_log;
error_log /var/log/nginx/andreas-lehr.com/error_log;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 127.0.0.1:9000;
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.
- 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:
- Start php-fpm
- Start nginx
deb http://packages.dotdeb.org stable all
deb-src http://packages.dotdeb.org stable all
wget http://www.dotdeb.org/dotdeb.gpg
cat dotdeb.gpg | sudo apt-key add -
apt-get update
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
#/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;
#/etc/php5/fpm/pool.d/www.conf
;listen = 127.0.0.1:9000
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.
/etc/init.d/php5-fpm start
/etc/init.d/nginx start
Conclusion:
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.

Thanks, great tutorial here.
Just wanted to point out that php-apc caused issues with Debian Lenny, I had to remove it from the apt install (I really didn’t need it anyway). Also listen :80 was throwing an error, I needed to make it listen 80; (without the colon).
Additionally, for anyone who might be running into port 80 bind() issues on nginx startup, you need to stop apache2 first. I also removed apache2 from the startup list by running:
sudo update-rc.d -f apache2 remove
other than that everything worked great!
[...] http://andreas-lehr.com/blog/archives/491-nginx-wordpress-php-fpm-on-debian-squeeze.html [...]