How to Install Nginx on a Wordpress Blog!

by admin on Wednesday, December 16th, 2009

Nginx is another super-fast, web server built for handling massive amounts of web traffic. Compared to Lighttpd, and based on real-world testing, Nginx offers better performance as it doesn’t leak memory as Lighttpd.

Recently I had the pleasure of moving all my web servers from Lighttpd to Nginx, here’s how you can install Nginx on your Wordpress blog too:
(Note: This guide includes how to install Nginx on your Wordpress blog with pretty-permalinks, WP super cache workings, multiple domains, and also MediaWiki settings too.)

You can compile Nginx by downloading the source here OR you can simply download it using Yum for most Fedora/CentOS based servers.

First, set the repository to either this:

rpm -Uvh$(uname -m)/epel-release-5-3.noarch.rpm

Or this if above didn’t work:

rpm -Uvh

Next, you will use yum command to install nginx, php-cgi, and spawn-fcgi.

yum install nginx

yum install php-cgi

yum install spawn-fcgi

After installing these 3 things, you can make a script to spawn php-cgi processes. Since Nginx doesn’t come with a fastcgi, which is required, you need to spawn your own php-cgi (fastcgi) processes.

Copy and paste the following as under /usr/bin/php-fastcgi using your favorite editor:

/usr/bin/spawn-fcgi -f /usr/bin/php-cgi -a -p 9000 -P /var/run/

*Note, you can change the and -p 9000 to a different IP or port if you want to use a different web server or port number.
Also note that you will have to change configuration files under /etc/nginx/nginx.conf if you do change the values.

Next, make sure the new file executable by running the following command from shell:

chmod 755 /usr/bin/php-fastcgi

Next, we will make an easy init.d script file so we can start and top the fastcgi with ease.

Copy and paste the following as under /etc/init.d/fastcgi:

# chkconfig:   - 85 15
# description: FAST CGI!
case "$1" in
      killall -9 php-cgi
      killall -9 php-cgi
      echo "Usage: php-fastcgi {start|stop|restart}"
      exit 1
exit $RETVAL

Next, make sure the new file executable by running the following command from shell:

chmod 755 /etc/init.d/fastcgi

All right, we are pretty much done except for setting up the Nginx configuration files. After that, you should be able to switch on/off between any other web server you have been using already such as Apache or Lighttpd. That way, we can easily revert back to the old web server if it’s a live server.

Let’s now edit the Nginx config file at /etc/nginx/nginx.conf:

Here’s a sample changes I used for my website, I will explain the details in the code:

#Set the user to "root" or "apache", otherwise you will get permissions error.
user              root;
#Set the worker_processes to number of CPUs you have, in my case, my quad-core web server = 4.
worker_processes  4;

server {
        listen       80;
        #set this to your domain name
        location / {
            #these gzip settings will compress your static files,
            #you need these On so static Wordpress cache files are compressed
            gzip  on;
            gzip_http_version 1.0;
            gzip_vary on;
            gzip_comp_level 9;
            gzip_proxied any;
            gzip_types text/plain text/html text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
            gzip_buffers 16 8k;
            #this is the html root of your webserver for
            root   /var/www/vhosts/;
            #note we need to add index.php to default config!!!-------------------------
            index  index.php index.html index.htm;
            # if the requested file exists, return it immediately---------------------------
               if (-f $request_filename) {

            #this is all for super cache supercache -------------------------------------

               set $supercache_file '';
               set $supercache_uri $request_uri;

               if ($request_method = POST) {
                       set $supercache_uri '';

            # Using pretty permalinks, so bypass the cache for any query string----------
               if ($query_string) {
                       set $supercache_uri '';

               if ($http_cookie ~* "comment_author_|wordpress|wp-postpass_" ) {
                       set $supercache_uri '';

            # if we haven't bypassed the cache, specify our supercache file--------------
               if ($supercache_uri ~ ^(.+)$) {
                       set $supercache_file /wp-content/cache/supercache/$http_host/$1index.html;
                       #use the one below if your blog lies on /blog directory instead of root
                       #set $supercache_file /blog/wp-content/cache/supercache/$http_host/$1index.html;
            # only rewrite to the supercache file if it actually exists----------------------
               if (-f $document_root$supercache_file) {
                       rewrite ^(.*)$ $supercache_file break;

            # all other requests go to Wordpress----------------------------------
               if (!-e $request_filename) {
                        rewrite ^/(.*)$ /index.php?q=$1 last;
                        #use the one below if your blog lies on /blog directory instead of root
                        #rewrite ^/(.*)$ /blog/index.php?q=$1 last;

        location ~ \.php$ {
        #    root           html;
            #if you are using a different IP/port settings as set earlier for fastcgi, make the changes here too!------
            fastcgi_index  index.php;
            #also set this to the root directory of your webserver plus $fastcgi_script_name
            fastcgi_param  SCRIPT_FILENAME  /var/www/vhosts/$fastcgi_script_name;
            include        fastcgi_params;

*Note – If you have multiple domains, simply make another nested server with different domain name and make sure to change the directory settings.
**Note2 – I left out some parts such as error pages and the others, those are up to you to set them according to how you want it.

Now you can test out if your new Nginx web server is working by turning off your current web server and turning on Nginx and Fastcgi.

First, turn off your current web server.

For Apache:

/sbin/service httpd off

For Lighttpd:

/sbin/service lighttpd off

Then start Nginx and Fastcgi w/ following commands:

/sbin/service nginx on
/sbin/service fastcgi on

*Note – If you are on a live server, you should really test your Nginx config file by setting Nginx.conf’s port setting from 80 to 79 then try turning it on. If you don’t get errors, then you can try it in live mode, just a tip for anyone doing this on a live website.

If you make changes to Nginx.conf file and you want to restart the server, try this command instead of restart:
(I found restart doesn’t work all that well but reload does well.)

/sbin/service nginx reload

Also, if you are getting error with above settings, try turning off PHP safe mode in /etc/php.ini:

set safe_mode = Off

Remember to re-start the spawn-fcgi processes otherwise the changes won’t reflect!

Now, to set your Nginx and FastCgi to run when your server reboots:

For apache:

chkconfig httpd off

For Lighttpd:

chkconfig lighttpd off

Turn your Nginx and FastCgi on:

chkconfig nginx on
chkconfig fastcgi on

Check to see the settings:

chkconfig –list

Reboot and see if your web server now runs on Nginx and enjoy the new found speeds of Nginx!

Extra – Running Nginx with MediaWiki Wikis

I also found an easy way for running my MediaWiki wiki along with my blog on the same server.  I found that there’s other ways but my way is simpler and works flawlessly with pretty permalinks on MediaWiki wikis.  I found that the wiki’s skin directory was the only thing that was messing things up so I just made the Nginx.conf to ignore anything under skins directory and voila!, everything else works easily.

My wiki lies in the /wiki directory, so I added these to my LocalSettings for MediaWiki:

$wgScriptPath       = "/wiki";
$wgArticlePath      = "$wgScript/$1";
$wgUsePathInfo = true;

And these to my Nginx.conf file under server bracket:

#for MEDIAWIKI-------------
if ($request_filename ~ /wiki/skins/) {
if ($request_filename ~ /wiki/) {
rewrite ^/wiki/(.*)$ /wiki/index.php?title=$1 last;


Here’s some resources I used in order to get my Nginx working with my Worpress Blog. (and thanks everyone!)

I hope my install guide helps you as it took me about 10 hours to get everything running right including my blog, multiple domains, and my MediaWiki.


Permalink Help:

Multiple Domains Configuration:

Redirect www. to host:


Categories: Web Server Hacks.

Related News and Resources