Skip to content


Setup Nginx as Reverse Proxy with Apache.

cPanel: Setup Nginx as Reverse Proxy with Apache

WHM/Cpanel comes by default with Apache web server. That doesn’t mean that we can’t integrate Nginx “EngineX”. With some minor changes, we can install Nginx to listen

on port 80 and forward any PHP request to Apache to handle on another port like 8081. Apache is not really good in handling static files, so we pass this task to

Nginx. You will notice 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.

If you want nginx for shared hosting,please refer the following post/article :-

I am using variables as below:

OS: CentOS 5.6 32bit
cPanel: cPanel 11.30.1 (build 5)
Domain IP: 10.20.30.11
Apache port: 8081
Domain: unixsurgeon.com
User: unixsurgeon
Home directory: /home/unixsurgeon

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  10.20.30.11 # 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.

4. Before we install Nginx, we need to change Apache port to 8081. Login to WHM > Server Configuration > Tweak Settings > Apache non-SSL IP/port:

0.0.0.0:8081

5. We need to run following command so cPanel will remember that Apache configuration template has changed:

#  /usr/local/cpanel/bin/apache_conf_distiller –update –main
#  /scripts/rebuildhttpdconf

6. Lets install Nginx and all requirements using yum. You can download Nginx source at http://nginx.org/en/download.html:

#  yum install pcre* -y
#  cd /usr/local/src
#  wget http://nginx.org/download/nginx-1.0.5.tar.gz
#  cd nginx-*
#  ./configure
#  make
#  make install

7. Once installed, we need to do some changes to Nginx configuration file. Using text editor, copy and paste following line and change the required value to fit your environment:

—————————————

user  nobody;

worker_processes  1;

error_log  logs/error.log  info;

events {
worker_connections  1024;
}

http {
server_names_hash_max_size 2048;
include       mime.types;
default_type  application/octet-stream;

log_format   main ‘$remote_addr – $remote_user [$time_local]  $status ‘
‘”$request” $body_bytes_sent “$http_referer” ‘
‘”$http_user_agent” “$http_x_forwarded_for”‘;

sendfile        on;
tcp_nopush     on;

keepalive_timeout  10;

gzip  on;
gzip_min_length  1100;
gzip_buffers  4 32k;
gzip_types    text/plain application/x-javascript text/xml text/css;
ignore_invalid_headers on;

client_header_timeout  3m;
client_body_timeout 3m;
send_timeout     3m;
connection_pool_size  256;
client_header_buffer_size 4k;
large_client_header_buffers 4 32k;
request_pool_size  4k;
output_buffers   4 32k;
postpone_output  1460;

server {
# this is your access logs location
access_log /usr/local/apache/domlogs/unixsurgeon/unixsurgeon.com;

error_log  logs/vhost-error_log warn;
listen    80;
# change to your domain
server_name  unixsurgeon.com www.unixsurgeon.com;

location ~* \.(gif|jpg|jpeg|png|wmv|avi|mpg|mpeg|mp4|htm|html|js|css)$ {
# this is your public_html directory
root   /home/unixsurgeon/public_html;
}

location / {
client_max_body_size    10m;
client_body_buffer_size 128k;

proxy_send_timeout   90;
proxy_read_timeout   90;

proxy_buffer_size    4k;
proxy_buffers     16 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;

proxy_connect_timeout 30s;

# change to your domain name
proxy_redirect  http://www.unixsurgeon.com:8081   http://www.unixsurgeon.com;
proxy_redirect  http://unixsurgeon.com:8081   http://unixsurgeon.com;

proxy_pass   http://127.0.0.1:8081/;
proxy_set_header   Host   $host;
proxy_set_header   X-Real-IP  $remote_addr;
proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
}

———————————

8. We need to check the Nginx configuration file before start the Nginx. Use following command to check the configuration file:

root@cpanel [~]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

9. If everything is OK, we can start the Nginx as below:

# /usr/local/nginx/sbin/nginx start

10. Lets check whether Nginx and Apache are listening to the correct port:

root@cpanel [~]# netstat -tulpn | grep -e nginx -e httpd
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      16728/nginx
tcp        0      0 0.0.0.0:8081                  0.0.0.0:*                   LISTEN      19655/httpd
tcp        0      0 0.0.0.0:443                 0.0.0.0:*                   LISTEN      19655/httpd

11. If everything is run as expected, edit /etc/rc.local using text editor and add following line so Nginx will start automatically after reboot :-

# /usr/local/nginx/sbin/nginx restart

Nginx no need to be restarted to load the latest configuration file. You can run following command and it will reload the configuration on-the-fly without downtime:

kill -HUP `ps -ef | grep nginx | grep master | awk {’print $2′}`

Enjoy Linux…. !!!!

Posted in Apache, Linux tutorials, Open Source Applications, lamp server, nginx.

Tagged with , .


How to install NginX on cPanel WHM/Cpanel Server

Install NginxAdmin to WHM/Cpanel for Optimize Performance.

Diffrence between nginx & apache :-

Apache is a process-based server, while nginx is an event-based web server.Nginx known as “engineX” probably the best-known asynchronous server and Apache is undoubtedly the best known process-based server.

The main advantage of the asynchronous approach is scalability. In a process-based server, each simultaneous connection requires a thread which incurs significant overhead. An asynchronous server, on the other hand, is event-driven and handles requests in a single (or at least, very few) threads.

While a process-based server can often perform on par with an asynchronous server under light loads, under heavier loads they usually consume far too much RAM which significantly degrades performance. Also, they degrade much faster on less powerful hardware or in a resource-restricted environment such as a VPS.

Pulling numbers from thin air for illustrative purposes, serving 10,000 simultaneous connections would probably only cause Nginx to use a few megabytes of RAM whereas Apache would probably consume hundreds of megabytes (if it could do it at all).

Nginx+cPanel+Apache= Performance boosted secured hosting server

How NginxAdmin will help your cPanel server to increase the performance ?

NginxAdmin is WHM interface of Nginx installer for cPanel server. This plugin will increase your server performance and decrease the server Apache Load. So you can host more websites in a cPanel server than usual.Nginx is known for its high performance, stability, rich feature set, simple configuration, and low resource consumption.Unlike traditional servers, Nginx doesn’t rely on threads to handle requests. Instead it uses a much more scalable event-driven architecture. This architecture uses small, but more importantly, predictable amounts of memory under load.

Even if you don’t expect to handle thousands of simultaneous requests, you can still benefit from Nginx’s high-performance and small memory footprint. Nginx scales in all directions: from the smallest VPS all the way up to clusters of servers.

To install copy and execute following lines :-

# cd /usr/local/src
# wget http://nginxcp.com/nginxadmin2.8-stable.tar
# tar -xvf nginxadmin2.8-stable.tar
# cd publicnginx
# ./nginxinstaller install
# /etc/init.d/httpd restart

Troubleshooting Installation:-

If you receive any pyaml related error or the following error

—————————–
Welcome to the Nginx Admin installer……Starting Install
Generating vhosts…
Traceback (most recent call last):
File “/scripts/createvhosts.py”, line 2, in ?
import yaml
File “/usr/lib/python2.4/site-packages/PyYAML-3.10-py2.4-linux-i686.egg/yaml/__init__.py”, line 26
SyntaxError: ‘yield’ not allowed in a ‘try’ block with a ‘finally’ clause
Installing WHM interface…
deploying booster rockets
Traceback (most recent call last):
File “/usr/local/src/publicnginx/nginxinstaller2″, line 9, in ?
import createvhosts
File “/usr/local/src/publicnginx/createvhosts.py”, line 2, in ?
import yaml
File “/usr/lib/python2.4/site-packages/PyYAML-3.10-py2.4-linux-i686.egg/yaml/__init__.py”, line 26
SyntaxError: ‘yield’ not allowed in a ‘try’ block with a ‘finally’ clause
——————————

Then run execute following commands ….
For i686 architecture :-

# easy_install Pyyaml==3.09
# rm -f /usr/lib/python2.4/site-packages/PyYAML-3.10-py2.4-linux-i686.egg

For x86_64 architecture :-

# easy_install Pyyaml==3.09
# rm -f /usr/lib/python2.4/site-packages/PyYAML-3.10-py2.4-linux-x86_64.egg

Then Again run the following commands :-

# ./nginxinstaller install
# /etc/init.d/httpd restart

Now Nginx run on the port no:80 and apache run on the port no.8081.You can manage the nginx through WHM >> Main >> Nginx Admin

To uninstall nginx use following :-

#  cd /usr/local/src
#  wget http://nginxcp.com/nginxadmin2.8-stable.tar
#  tar xf nginxadmin2.8-stable.tar
#  cd publicnginx
#  ./nginxinstaller uninstall

Enjoy Linux…!!!!

Posted in Apache, Linux tutorials, Open Source Applications, lamp server, nginx.

Tagged with , .