Install nginx as Reverse Proxy With Apache

Installing nginx as a reverse proxy is one way to improve your server performance and so your website performance too. This is the serial post of “Improve Your Server Performance With Nginx” from my previous post. You can read why you should install nginx alongside Apache to improve your server performance in that post. But here we will talk about how we technically do it.

Install nginx as reverse proxy with Apache

Download and install the requirements

1. Download mod_rpaf

cd /usr/src #our working directory, can be anywhere you like
wget http://stderr.net/apache/rpaf/download/mod_rpaf-0.6.tar.gz

2. Download PCRE (Perl Compatible Regular Expressions)

wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-7.9.tar.gz

3. Install mod_rpaf for Apache 2.x:

tar xvzf mod_rpaf-0.6.tar.gz
cd mod_rpaf-0.6
/usr/local/apache/bin/apxs -i -c -n mod_rpaf-2.0.so mod_rpaf-2.0.c

4. Install PCRE:

cd /usr/src
tar xvzf pcre-7.9.tar.gz
cd pcre-7.9
./configure
make
make install

Download and install nginx

1. Download the latest stable nginx. When I wrote this post the latest stable nginx was version 0.8.53.

cd /usr/src  #working directory
wget http://nginx.org/download/nginx-0.8.53.tar.gz

2. Install nginx:

cd /usr/src
tar xvzf nginx-0.8.53.tar.gz
cd nginx-0.8.53
./configure --with-http_ssl_module --with-http_realip_module --with-http_dav_module --with-http_flv_module --with-http_gzip_static_module
make
make install

3. Now nginx is installed. But don’t run it yet. We’re only halfway there.

Configure Apache

1. If you use WHM then you can log in to your WHM Panel with root access, and go to Main >> Service Configuration >> Apache Setup > Include Editor > Pre Main Include and include this configuration:

LoadModule rpaf_module modules/mod_rpaf-2.0.so
RPAFenable On
# Enable reverse proxy add forward
RPAFproxy_ips 127.0.0.1 LIST_OF_YOUR_IPS
# which ips are forwarding requests to us
# change LIST_OF_YOUR_IPS to all ip you have with space separated
RPAFsethostname On
# let rpaf update vhost settings
# allows to have the same hostnames as in the "real"
# configuration for the forwarding Apache
RPAFheader X-Real-IP
# Allows you to change which header mod_rpaf looks
# for when trying to find the ip that is forwarding
# our requests

Note: If you don’t use WHM/Cpanel then just open your httpd.conf (usually in /usr/local/apache/conf/) and add those lines in there.

2. Change Apache listener port from 80 to 8080. Open your WHM panel under the Tweak Settings menu, and find the Apache listener port and change it from 80 to 8080. If you don’t use WHM/Cpanel then browse into your httpd.conf and find the line “Listen 80” and change it to “Listen 8080”.

3. For WHM/Cpanel users, update your WHM setting and restart Apache from SSH:

/usr/local/cpanel/whostmgr/bin/whostmgr2 --updatetweaksettings
/scripts/rebuildhttpdconf
httpd restart

For non WHM/Cpanel users, restart your Apache:

/etc/init.d/httpd restart #For CentOS OR
/etc/init.d/apache2 restart #For Ubuntu

Configure nginx with automated nginx virtual host creator

1. Download the automated nginx virtual host creator:

cd /usr/src  #working directory
wget http://www.ivankristianto.com/downloads/nginx.sh

2. Give execute permission

chmod +x nginx.sh

3. Run it

./nginx.sh

Note: I got this script from the net, and I actually don’t know who created it. But I did a couple of tweaks with all the static files for nginx to serve, but exclude the html file, to prevent domains that use .html in their permalink from causing 404 errors. And it still has a bug with addon domain root directories — it won’t point to the right directory, so if you have an addon domain, you need to edit this manually. If you have a better script please let me know and I will update it. Thanks.

Test and Run nginx

1. Test nginx configuration

/usr/local/nginx/sbin/nginx -t

2. If no errors then you are ready to start the engine:

/usr/local/nginx/sbin/nginx

Now check all your websites and feel the better performance of your server. And monitor your server load and memory used — it should be lower and response time should increase.