Home Home
Get nginx and apache set up in 30 minutes.

Setting Up nginx and Apache in 30 minutes

September 16, 2010 in Development

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!

***

Submit to reddit!  Digg!  Like this on Facebook!  Tweet this!

Tags

,

angbox: Things to Remember When Migrating a Website
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 [...]

Got Something To Say?


angbox: version 5

Subscribe!

Subscribe!


Subscribe to this blog: RSS, Atom
 
angbox

About angbox


Angbox is the personal blog of Cary Ang, a web developer from New Jersey, and an attempt to write about the random things that move him.
 
php.angbox: now open

php.angbox


A laudable effort to teach practical PHP to programmers and non-programmers alike.

The Blog

6

Development


29

Featured


8

Gaming


3

Music


5

Other


6

Personal Finance


19

Travel


Featured Posts

This is Carly Rae Jepsen's EP. LISTEN TO IT NOW.

Curiosity (2012) - Carly Rae Jepsen: Catchy at its Finest


So, there's this new girl in town. Her name's Carly Rae Jepsen. She's Canadian, and she just joined Justin Bieber's new label....
 
AT&T's data plans. FUUUUUUUUUUUUUUUU...

Getting Around AT&T's Tiered Smartphone Data Plans


You've probably heard that AT&T's $30 unlimited* data plans have been dead and gone since the middle of last year, replaced wi...
 
Yet another gate in Togakushi.

22 Days in Japan, Day 18: Exploring Togakushi


It's a dark, cloudy day. The sun is nowhere to be found in Nagano. Since the ryokan (Japanese inn) that I'm currently stayi...
 
Nagano

22 Days in Japan, Day 17: Nagano, Rain, and a Scary Bridge


Kyoto is now miles away, and I'm on the train to my next destination: Nagano. Yep, the same Nagano that hosted the 1998 Winter...
 

Other Fun Angbox Sites

Clear Note Widget

Clear Note Widget


Clear Note Widget is a sticky note and Post-it app for Android phones. Get it now!
 
PennyToSave

PennyToSave


Hundreds of ways to save money on practically everything.
 
randomlygifted

randomlygifted


Let randomlygifted find you the perfect (random) gift ideas.
 
The Nokia Phone Blog

The Nokia Phone Blog


My blog covering the latest developments in the wireless industry, Symbian/MeeGo, and Nokia phones.
 
MyDealSpy

MyDealSpy


The greatest deal aggregator that has ever existed. Okay, I'm a little biased here.
 
PhoneMatchup

PhoneMatchup


An eBay cell phone comparison shopping/price monitoring project. Buy your next cell phone through PhoneMatchup and save!