Previously I discussed getting Apache + PHP up and running, which provides the foundation for our new web server. Next we’ll install Nginx as a front-end cache which will speed up the delivery of your website considerably – and use fewer resources while doing it!
Visitors who access your website will be served content via Nginx, where a cached version of your website will be served. Only dynamic requests or content which the Nginx cache does not currently hold will be passed back to Apache to serve. This keeps the system resources usage down, and the result is a fast and efficient serving process which can handle far more requests than Apache on it’s own.
Part 2 : Install Nginx as a cache & reverse proxy
Firstly, lets reconfigure Apache to act as the receiver for requests from Nginx, rather than the general public on port 80. To do this make sure you are elevated to root permissions and run the following to open the Apache config with a text editor (vi):
If you aren’t familiar with vi you may have a hard time, but bear with me. You need to search for ‘Listen 80′ which is the web servers default port, do this by typing:
Then press Shift + A (begin editing at end of line) and type 80, so the full line reads:
Next, press ‘Escape’ followed by
That’ll save the file. It means “Write & Quit”. Restart the web server with with following command:
2) Install Nginx with Yum & Configure
Yum makes software management a breeze, simply issue the following command to install:
yum install nginx
Seriously that’s it. Now lets edit the default nginx config using our friend ‘vi’ again. Do this with:
Below is my config, you can change to suit. This will be fine for most serving environments, however:
We’ll need to create the Nginx cache directory with the following:
mkdir -p /var/tmp/nginx
All going well you should be able to start Nginx with the following command:
Now test by firing a web-browser at your VPS IP address once more, and you should be greeted with whatever your Apache is serving from /var/www/html. The key difference is this will now be coming via a cached Nginx front-end.
You can confirm with the following command:
wget -qO- --save-headers YOURVPSIPADDRESS | headYou should see something like this if everything is working correctly
HTTP/1.1 200 OK Server: nginx/0.8.54 Date: Tue, 30 Aug 2011 03:49:39 GMT Content-Type: text/html Content-Length: 28405 Last-Modified: Tue, 30 Aug 2011 03:45:02 GMT Connection: keep-alive Vary: Accept-Encoding X-Served-By: YOUR SERVER IP HEREThat’s it! We’ll focus on tightening down the service and adding some other important features, next time around.