cPanel: Setup Nginx as Reverse Proxy with Apache

cPanel comes with Apache web server by default. Its not mean that we cannot integrated Nginx inside. With some minor changes, we can install Nginx to listen to port 80 and forward any PHP process to Apache on another port, 88. Apache is not really good in handling static files, so we will pass this task to Nginx. You will noticed that your memory and CPU will decrease once you have done this setup.

Warning: This setting is not suitable for shared hosting environment. I strongly recommend you to apply this if you have 1 busy website running under cPanel. Make sure you have compiled your Apache modules and features using EasyApache.

I am using variables as below:

OS: CentOS 5.6 32bit
cPanel: cPanel 11.30.1 (build 4)
Domain IP: 123.124.125.88
Apache port: 88
Domain: mywebs.net
User: mywebs
Home directory: /home/mywebs

1. Since Nginx will be reverse proxy for Apache, we don’t want our log file to record the proxy IP. We want the real IP as usual. This will make sure our stats page like Webalizer and AWstats will record the correct information. So we need to install mod_rpaf which is “Reverse Proxy Add Forward” module for Apache. You can download that at http://stderr.net/apache/rpaf/download:

cd /usr/local/src
wget http://stderr.net/apache/rpaf/download/mod_rpaf-0.6.tar.gz
tar -xzf mod_rpaf-0.6.tar.gz
cd mod_rpaf-*
apxs -i -c -n mod_rpaf-2.0.so mod_rpaf-2.0.c

2. Once installed, we need to load the module into Apache configuration. Since cPanel already has Include Editor for Apache, we will use that functions. Login to WHM > Service Configuration > Apache Configuration > Include Editor > Pre Main Include > All Versions and paste following text:

LoadModule rpaf_module modules/mod_rpaf-2.0.so
RPAFenable On
RPAFproxy_ips 127.0.0.1  123.124.125.88 # replace the value with your server IP
RPAFsethostname On
RPAFheader X-Real-IP

3. Click Update > Restart Apache. The module should be loaded after restart.

Continue reading “cPanel: Setup Nginx as Reverse Proxy with Apache” »