WordPress and Nginx

Apache is like Microsoft Word, it has a million options but you only need six. Nginx does those six things, and it does five of them 50 times faster than Apache.

Chris Lea on nginx and WordPress.

Nginx is a lightweight Web server with a small memory footprint and exceptional flexibility, along with this features the Nginx design is focused to ensure the maximum security. The configuration may look non-intuitive and not so straightforward, but a strong community and an excellent documentation will clear all your doubts. In this how-to I’m going to share my experience on moving a few WordPress blogs from Apache to Nginx, one of the blog is served inside the root directory and the other one is served as WordPress sub directory.

Please note, this how to applies to Ubuntu 10.04 LTS.

php5-fpm installation and configuration.

In order to serve PHP dynamic content we need to install php5-fpm.

[sourcecode language="bash"]

deb http://ppa.launchpad.net/brianmercer/php/ubuntu lucid main
deb-src http://ppa.launchpad.net/brianmercer/php/ubuntu lucid main

apt-key adv –keyserver keyserver.ubuntu.com –recv-keys 8D0DC64F
apt-get update
apt-get install php5-fpm

[/sourcecode]

Once php5-fpm is installed we need to create a configuration file www.conf ,
located into /etc/php5/fpm/pool.d/ . Please copy the following values into it.

[sourcecode language="bash"]

[www]

user = www-data
group = www-data
listen = 127.0.0.1:9000
pm = dynamic
pm.max_children = 10
pm.start_servers = 4
pm.min_spare_servers = 2
pm.max_spare_servers = 6
chdir = /

[/sourcecode]

Restart the php5-fpm service.

[sourcecode language="bash"]
/etc/init.d/php5-fpm restart
[/sourcecode]

Nginx installation and configuration.

[sourcecode language="bash"]
apt-get install nginx
[/sourcecode]

It wise to disable the default site configuration.

unlink /etc/nginx/sites-enabled/default

Now, let’s begin to create our first site configuration, the one
installed inside the root directory and served as www.example1.com .

In /etc/nginx/sites-available/example1.com paste, wich example1.com
is your domain name.

Example NR.1

[sourcecode language="bash"]

server {
listen 80;
server_name example1.com;

access_log /var/log/nginx/example1.com.access.log;
error_log /var/log/nginx/example1.com.error.log;

location / {
root /var/www/example1.com;
index index.php index.htm;
}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

}

[/sourcecode]

To enable example1.com please create a soft link to:

[sourcecode language="bash"]
sudo ln -s /etc/nginx/sites-available/example1.com /etc/nginx/sites-enabled/example1.com
[/sourcecode]

Restart Nginx with # /etc/init.d/nginx restart and point the browser to your domain name.

Example NR.2, with wordpress served as www.example2.com/blog

In /etc/nginx/sites-available/example2.com paste, wich example2.com
is your domain.

[sourcecode language="bash"]

server {
server_name example2.com;
index index.php;
root /var/www/example2.com;

access_log /var/log/nginx/example2.com.access.log;
error_log /var/log/nginx/example2.com.error.log;

if (!-e $request_filename) {
rewrite ^.+/?(/ms-.*) $1 last;
rewrite ^/files/(.+) /wp-includes/ms-files.php?file=$1 last;
rewrite ^.+/?(/wp-.*) $1 last;
rewrite ^.+/?(/.*.php)$ $1 last;
rewrite ^(.+)$ /index.php?q=$1 last;
break;
}

location ~ .php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}

[/sourcecode]

To enable example1.com please create a soft link to:

[sourcecode language="bash"]
sudo ln -s /etc/nginx/sites-available/example2.com /etc/nginx/sites-enabled/example2.com
[/sourcecode]

Please restart the Nginx server and test your new blog # /etc/init.d/nginx and point the browser to www.example2.com/blog

Comments

Leave a Reply

Your email address will not be published / Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>