Setting Up nginx and Apache in 30 minutes
The angbox blog and all of my other web sites are now running on a lethal combination of nginx and Apache. That’s a huge improvement from a week ago, when my sites were barely running. If you’re looking for an alternative to running Apache by its bloated lonesome, check out this guide. If you’re looking for the latest saga of my Japan trip, or another post that has absolutely nothing to do with web development, you might want to skip it.
My Situation
On my VPS, I host five sites: two blogs (including this one), and three other sites. Each site is heavily PHP and MySQL driven, and in total serves a couple of thousand visitors a day. Not a huge amount of traffic, but enough traffic that my lone Apache server was getting killed, literally, due to out of memory errors. Experimenting with some of the apache.conf settings yielded some improvement, but the server almost always ended up stalling after some period of time. It was time for a change.
I’d heard lots of good things about nginx (pronounced engine-x), an up-and-coming webserver known for running high-traffic static web sites with ease. So I decided to do the up-and-coming hybrid: use nginx for serving up static content (HTML, images, Javascript, css, etc), and then use Apache to process the PHP files.
My Setup
I run a barebones LAMP (Linux/Apache/MySQL/PHP) setup on a VPS (Virtual Private Server) with 512 megabytes of RAM. The current OS is Ubuntu 9.10 (Karmic), and the only applications installed are webservers and mail/FTP servers.
This guide is going to assume that Apache is already installed.
1. Install nginx.
Well, this one’s easy. If you’re using Ubuntu, one line will suffice:
> sudo aptitude install nginx
2. Edit the nginx.conf settings file to your liking.
I have worker_processes set to 2, worker connections set to 1024, and gzip on. There’s probably not too much that you need to change here, but make sure you have an include statement for the proxy.conf file:
include /etc/nginx/proxy.conf;
3. Add a proxy.conf file, or create files for all of your domains.
The right way, I guess, is to create a config file for each domain in the sites-available directory. Then create a symlink to the sites-enabled directory to finish things off. However, I don’t like having to go through a bunch of different config files for all of my domains, so I just stuck everything into the proxy.conf file. Make sure you include proxy.conf in the nginx config file – it’s not included by default.
The proxy.conf file should have the following for each web site you are hosting (and assuming your site is called blahblahblah.com – just change the values to match your own site):
server {
listen 80;
server_name blahblahblah.com www.blahblahblah.com;
access_log /var/www/log/nginx.blahblahblah.log;
# serve static files
location ~ ^/(images|javascript|js|css|flash|media|static)/ {
root /var/www/blahblahblah.com/public;
expires 30d;
}
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;
proxy_connect_timeout 300;
proxy_send_timeout 300;
proxy_read_timeout 300;
}
}
Copy and paste the lines from “server {” to the closing “}” for each domain that is being hosted.
Some important points here:
- The server should be listening on port 80, and forwarding all non-static requests to Apache which is running on localhost port 8080.
- Depending on how your directory layout is, you might have to change the access_log variable so that it matches yours.
- If you do go the “sites-enabled” route, you might find this command useful:
> sudo ln -s /usr/local/nginx/sites-available/default /usr/local/nginx/sites-enabled/default
4. Edit Apache’s ports.conf file.
You need to instruct Apache to listen on port 8080, instead of port 80. Otherwise you’ll have a conflict with nginx, and conflicts are bad. Open up the ports.conf file (on Ubuntu, it’s located in /etc/apache2/) and change the ports accordingly:
NameVirtualHost *:8080 Listen 8080
5. Edit the apache config files for your domains.
Unlike my nginx install, my Apache domain configs are in separate files in the sites-available directory. Your setup may be like this, or it might not. Either way, the goal here is to change the listening port to 8080. Also, if you’re using gzip compression, remove or comment out those lines – since Nginx will be handling the major html/txt/xml/js/css files and will take care of the gzipping itself.
6. Start Apache, and start nginx.
Time for the moment of truth. Start up Apache and nginx:
/etc/init.d/apache2 start /etc/init.d/nginx start
Hopefully nothing broke, and your webserver headaches are a thing of the past!





















One Comment to Setting Up nginx and Apache in 30 minutes:
2/5/2011
11:25 am
[...] that I run a LANMP setup (Linux, Apache, Nginx, MySQL, PHP). The combo of Apache + nginx is great for performance and reliability, but technically adds an extra layer of “things that could possibly go [...]