Install nginx as Reverse Proxy With Apache

Install nginx as reverse proxy is one way to improve your sever 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 aside with apache to improve your server performance in that post. But here we will talk how technically we do it.

Install nginx as reverse proxy with Apache

Download and install the requirements

  1. Downoad 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 (erl 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 write this post the latest stable nginx is 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 just half way to go.

Configure Apache

  1. If you use WHM then you can login 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 seperated
    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 the 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 you WHM panel under Tweak Setting menu, and find 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″ change it to “Listen 8080″.
  3. For WHM/Cpanel user, update your WHM setting and restart Apache from SSH:
    /usr/local/cpanel/whostmgr/bin/whostmgr2 --updatetweaksettings
    /scripts/rebuildhttpdconf
    httpd restart
    

    For non WHM/Cpanel user, 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 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 create this script. But i did a couple tweaks with all the static files for nginx to serve, but exclude html file, this is to prevent if some domain use .html in their permalink which may cause 404 error. And it still have a bug, with addon domain root directory. It won’t point to the right directory, so if you have 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 you websites and feel the better performance of your server. And monitor your server load and memory used, it should be lower and increase response time.

Give me your feedback

This site uses Akismet to reduce spam. Learn how your comment data is processed.