Wordpress with Apache and NginX

I recently decided to make the move back to Wordpress from a smaller Rails application called Simplelog. I'll save my reasons for another post, but needless to say, I had to decide what type of application stack I would use to host Wordpress. After a bit of research, I decided to go with NginX in the front end to serve static files, and put Apache behind it to serve the dynamic PHP content through the use of mod_php. I had originally thought I might use NginX only, but Wordpress is fairly dependent on mod_rewrite for a lot of things, and NginX doesn't really have an equivalent yet, so I wanted to have Wordpress itself running on Apache. The reason I put NginX in front is that it is much faster at serving static files, and has a very low memory footprint.This tutorial makes the assumption that you're using Ubuntu Gutsy for the OS, and have already installed NginX and Apache using Aptitude. For great guides on how to do that, check out articles.slicehost.com. So let's start off with the NginX configuration.

NginX Configuration

Go ahead and open (or create) a virtual host config file for the domain you want to use.

sudo nano /etc/nginx/sites-available/domain1.com

You should then fill it with the following configuration, courtesy of Alexey Kovyrin. Of course, you'll need to change a few things to fit your specific setup, such as the domain name, and location of your Wordpress install for serving static files.

server {
  listen      80;
  server_name www.domain1.com;

rewrite ^/(.*) http://domain1.com permanent; }

server { listen 80; server_name domain1.com;

access_log /home/user/public_html/domain1.com/log/nginx.access.log;
error_log /home/user/public_html/domain1.com/log/nginx.error.log;

# Main location
location / {
proxy_pass http://127.0.0.1:8080/; proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}

# Static files location
location ~* ^.+.(jpg|jpeg|gif|png|ico|css|txt|js)$ {
root /home/user/public_html/domain1.com/public;
} }

You'll then need to enable to virtual host by creating a symlink to the sites-enabled directory using the following command.

sudo ln -s /etc/nginx/sites-available/domain1.com /etc/nginx/sites-enabled/domain1.com

Next up is Apache...

Apache Configuration

Let's start by creating the virtual host file. If you've installed via aptitude, you can create the file by running the following command, replacing domain1.com with your own domain name.

sudo nano /etc/apache2/sites-available/domain1.com

Next, you'll need to fill the virtual host file with a bit of configuration.

<VirtualHost *:8080>

# Admin email, Server Name (domain name) and any aliases ServerAdmin webmaster@domain1.com ServerName domain1.com

# Index file and Document Root (where the public files are located) DirectoryIndex index.html, index.php DocumentRoot /home/user/public_html/domain1.com/public

# Custom log file locations LogLevel warn ErrorLog /home/user/public_html/domain1.com/log/apache.error.log CustomLog /home/user/public_html/domain1.com/log/apache.access.log combined

</VirtualHost>

Since NginX is going to be sitting out front on port 80, you'll need to change to default port that Apache runs on. In the NginX configuration above, requests will be sent to an Apache instance running on port 8080. If you've installed Apache according to the Slicehost articles I linked to, then you will need to edit a couple configuration files to get Apache running on the proper port.

sudo nano /etc/apache2/apache2.conf

Find the following section and replace port 80 with port 8080.

...
NameVirtualHost *:8080
...

For the second file...

sudo nano /etc/apache2/ports.conf

And change port 80 to 8080.

...
Listen 8080
...

That's pretty much it. All you need to do now is run the following commands to enable the virtual host on Apache, and restart the web servers for the changes to take effect.

sudo a2ensite domain1.com
sudo /etc/init.d/apache2 restart
sudo /etc/init.d/nginx stop
sudo /etc/init.d/nginx start

You should now be able to visit your site! If it doesn't work, then either you screwed something up, or my directions are wrong, and my money's on wrong directions. So if you have problems leave a comment and I'll try and help.

08:00 AM | 3 Comments

Comments

  1. I'm just curious -- is it worthwhile to do all this work for static files when you could just set the expiration date way in the future and try to only serve those files once?

    Jamie Thingelstad on
  2. Thanks for the comment Jamie!

    Setting a far in the future expiration date for existing static files would keep them from being downloaded a second time, but what about newly added static file? For example, a site that is frequently updated with new images would benefit from those images being served from NginX.

    Also, setting the expiration date ahead is good for users who have previously visited your site, but what about first time users? Or users who have cleared out their temporary internet files? What if the site was dugg? You would be receiving a large spike in traffic from users who need to download all that static content. In those cases, your performance will benefit greatly from NginX's ability to quickly serve static files.

    Jamie on
  3. I've put nginx in front of apache2 and it all works just fine. The only thing is that I can't really tell if nginx is actually serving up the static files ? Thanks for the article by the way !

    Warren on