One of the key parts of speeding up the web application development is automating the…
As well as hosting sites on the Windows/IIS/.Net platform, we also have quite a few sites that run on the LAMP (Linux/Apache/MySQL/PHP) stack. Although Apache had been tweaked and tuned, we had periodic issues with unexpected load hitting Apache and causing it to slow down or (more often) grind to a halt.
Enter Nginx – the high concurrency webserver that now runs 8.5% of the world’s websites. For running static websites, Nginx seems a no-brainer – it’s dead simple to set up with fewer lines of configuration than Apache and performs admirably under load. Of course, most of the websites we were looking to move had dynamic content to some extent (some WordPress sites; some built with Yii; at least one vanilla, hand-rolled PHP site).
There’s multiple ways to spin this one – from using Nginx as a proxy through to running PHP-FPM (FastCGI Process Manager). In the end, we’ve settled on a set-up using Nginx, spawn-fcgi and PHP. Running on CentOS 5.7, it was remarkably simple to get Nginx up and running – here’s what we did:
yum install nginx
The Nginx config files are located in /etc/nginx
yum install spawn-fcgi
OPTIONS="-u apache -g apache -p 9000 -C 5 -P /var/run/spawn-fcgi.pid -- /usr/bin/php-cgi"
-u is the user to run as, -g the group to run as, -p is the port to listen on, -C is the number of child processes to spawn for PHP and -P is the pidfile.
We are running as apache:apache, just to avoid changing any existing file permissions when migrating from Apache. If this is a fresh setup, you might as well run as the nginx user.
Once that’s done, you can add spawn-fcgi to be started on boot and start it up:
chkconfig spawn-fcgi on service spawn-fcgi start
location / { try_files $uri $uri/ /index.php?q=$uri&$args; }
listen 80 default_server;
map $http_host $name { hostnames; .singaporepropertyforrent.com www.iproperty.com.sg; [snip ....] } server { server_name _; listen 80 default_server; rewrite ^ http://$name/ permanent; }
service nginx configtest
You also will be doing all of this on a test environment, of course, and doing some full testing, right?
service httpd stop
service nginx start
UPDATE (2013): Since this article was written, the FPM patches have become more widely adopted as part of the official PHP 5.4 series. We now use php-fpm on CentOS 6 using the PHP rpms from Remi instead of spawn-fcgi.
Leave a Reply