Archive for December, 2009

Migrating WordPressMU set up from Apache to Ngnix server

Wednesday, December 2nd, 2009

This post covers 2 topics – Ngnix performance compared to Apache server and steps for migrating WordPressMU from Apache to Ngnix.

We developed a social community product on Ruby on Rails platform. We used WordpressMU to manage many article pages on the website. Initially, we used Apache HTTP webserver. During the effort of optimizing performance of the product, we realized that Ngnix (http://nginx.net/) server performed better in comparison with Apache.
Nginx is a free, open-source, high-performance HTTP server and reverse proxy, as well as an IMAP/POP3 proxy server. Nginx is known for its high performance, stability, rich feature set, simple configuration, and low resource consumption. Nginx doesn’t rely on threads to handle requests. Instead it uses a much more scalable event-driven (asynchronous) architecture. This architecture uses small, but most importantly, predictable amounts of memory under load.

Nginx powers several high-visibility sites, such as WordPress, Hulu, Github, Ohloh, SourceForge and TorrentReactor. The main advantage of the asynchronous approach on Ngnix is scalability. In a process-based server like Apache, each simultaneous connection requires a thread which incurs significant overhead. Nginx is faster at serving static files. So for all the good reasons, we decided to migrate our site to Nginx.

There were quite a few things needed to integrate WordpressMu with Nginx server. Here are the steps
1. Install Nginx: [root@localhost]# yum install nginx
2. create the system sartup links for Nginx:
[root@localhost]# /sbin/chkconfig –level 35 nginx on
3. shut down the apache web service:
[root@localhost]# /sbin/service httpd stop
4. start the Nginx web service:
[root@localhost]# /sbin/service nginx start
5. Make PHP5 work in Nginx through FastCGI.
Install all these packages:
[root@localhost]# yum install lighttpd-fastcgi php-cli php-mysql php-gd php-imap php-ldap php-odbc php-pear php-xml php-xmlrpc php-eaccelerator php-magickwand php-magpierss php-mapserver php-mbstring php-mcrypt php-mhash php-mssql php-shout php-snmp php-soap php-tidy
6. Open /etc/php.ini and add the line cgi.fix_pathinfo = 1 right at the end of the file:
7. Start a PHP FastCGI daemon listening on port 9000, run it as the user ngnix and group nginx
root@localhost]# /usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -u nginx -g nginx -f /usr/bin/php-cgi -P /var/run/fastcgi-php.pid
8. Restart PHP FastCGI daemon
root@localhost]# ps -ef|grep php-cgi
root@localhost]# kill PROCESS_ID
9. To have the system execute the command automatically at boot time, open /etc/rc.local…
root@localhost]# vi /etc/rc.local
and Add the command at the end of the file:
/usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -u nginx -g nginx -f /usr/bin/php-cgi -P /var/run/fastcgi-php.pid
10. Stop the Apache server
root@localhost]# /sbin/init.d/httpd stop
To disable apache on startup:
root@localhost]# /sbin/chkconfig –level 35 httpd off

It is advisable to keep a backup of your Nginx configuration file:
root@localhost]# cp /etc/nginx/nginx.conf nginx.conf_cp
Add following code to the Ngnix.conf
server {
listen 80;
server_name localhost.localdomain;
#charset koi8-r;
location / {
root /usr/share/nginx/html;
index index.php index.html index.htm;
}
error_page 404 /404.html;
location = /404.html {
root /usr/share/nginx/html;
}

# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}

location ~ ^/(wordpress)/(.*)$ {
root /usr/share/nginx/html;
index index.php;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;
include fastcgi_params;
}
location / {
}

11. Now, to deal with the pretty URLs, add the following code for fixing the WordpressMU’s Pretty URLs. In location parenthesis above you can paste following code:
rewrite ^.*/.*/files/(.*) /wordpress/wp-content/blogs.php?file=$1;
if (!-e $request_filename) {
rewrite ^.+?(/wp-.*) /wordpress$1 last;
rewrite ^.+?(/.*\.php)$ /wordpress$1 last;
rewrite ^(.+)$ /wordpress/index.php?q=$1 last;
break;
}
12. For fixing the images issue with Nginx server use following code, paste it before location parenthesis,
#images for WordPress site
location ~* ^.+\.(html|jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js)$
{
root /usr/share/nginx/html/wordpress;
rewrite ^/.*(/wp-admin/.*\.(html|jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js))$ $1 last;
rewrite ^/.*(/wp-includes/.*\.(html|jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js))$ $1 last;
rewrite ^/.*(/wp-content/themes/.*\.(html|jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js))$ $1 last;
rewrite ^/.*(/wp-content/.*\.(html|jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js))$ $1 last;
rewrite ^.*/files/(.*(html|jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js))$ /wordpress/wp-content/blogs.php?file=$1 last;
expires 10d;
break;
}
Following articles we found helpful with making the changes.
URL : http://www.wikivs.com/wiki/Lighttpd_vs_nginx
URL : http://www.wikivs.com/wiki/Apache_vs_nginx

Contributors – Rohit Jain, Amol Nirmala Waman