Tips / Nginx

How to install and configure Nginx on cPanel servers

Are you ready to give your cpanel servers a big performance boost?  Then take a look at this tutorial, where we will guide you to install & configure Nginx on your cPanel server.

As you know, cPanel doesn’t have native support for Nginx yet, that’s why you are forced to use Apache web server, which is not the best webserver for your website and server performance. Luckly there are many ways to install nginx on cpanel, and on this tutorial we will let you know how to do it using Nginxcp installation script.

nginx cpanel

Having Nginx on your cPanel servers allows you to gain up to 50% of performance, that’s because Nginx will take care of all the static files of your websites (css, images, swf files, mp4, javascript files, and more), and pass the rest of the requests (dynamic requests — php files –) to Apache web server. This is called Nginx reverse proxy, where Nginx stands as front end server,with a Backend server.

One of the good things of this configuration, is that you don’t need to change a single thing in your websites, as your code and .htaccess files are 100% compatibles. This is one of the easiest and fastest way to install Nginx on cPanel server.

This are some of the features of this wonderful script:

  • Compatible with WHM: allows you to configure and analyze nginx logs with a GUI.
  • Gzip compression: it is compatible with Gzip compression, which will allow you to get faster speeds.
  • Integrated with the cPanel Service Monitor configuration
  • You can choose which domains will use Apache and which will use Nginx as reverse proxy.

Installing Nginx on cPanel

Move to WHM >  Cluster/Remote Access > Setup Remote Access Key and click at “Generate New Key button

Let’s start:

cd /usr/local/src
tar xf nginxadmin.tar
cd publicnginx
./nginxinstaller install
/etc/init.d/httpd restart

All done, it should be installed.

The only thing you need is to test this out, we will use curl from the shell to see if the website headers are showing Nginx as web server:

[user@localhost ~]$ curl -I
HTTP/1.1 200 OK
Server: nginx
Date: Thu, 21 Aug 2014 10:26:10 GMT
Content-Type: text/html
Connection: keep-alive
Vary: Accept-Encoding,User-Agent
[user@localhost ~]$

If you see Server: nginx in the headers, that means everything is working as expected.
If you ever need to tweak Nginx configuration, it can be easily done in this way:

nano -w /etc/nginx/nginx.conf

You will see a pretty well configured Nginx server, however it may not be the best for all machines and traffic, so, tweak as you need:

user  nobody;
# no need for more workers in the proxy mode
worker_processes  auto;
error_log  /var/log/nginx/error.log warn;
worker_rlimit_nofile 20480;
events {
 worker_connections 5120; # increase for busier servers
 use epoll; # you should use epoll here for Linux kernels 2.6.x

http {
 server_name_in_redirect off;
 server_names_hash_max_size 10240;
 server_names_hash_bucket_size 1024;
 include    mime.types;
 default_type  application/octet-stream;
 server_tokens off;

# remove/commentout disable_symlinks if_not_owner;if you get Permission denied error
# disable_symlinks if_not_owner;
 sendfile on;
 tcp_nopush on;
 tcp_nodelay on;
 keepalive_timeout  5;
 gzip on;
 gzip_vary on;
 gzip_disable "MSIE [1-6].";
 gzip_proxied any;
 gzip_http_version 1.0;
 gzip_min_length  1000;
 gzip_comp_level  6;
 gzip_buffers  16 8k;

# You can remove image/png image/x-icon image/gif image/jpeg if you have slow CPU
 gzip_types    text/plain text/xml text/css application/x-javascript application/xml application/javascript application/xml+rss text/javascript application/atom+xml;

 ignore_invalid_headers on;
 client_header_timeout  3m;
 client_body_timeout 3m;
 send_timeout     3m;
 reset_timedout_connection on;
 connection_pool_size  256;
 client_header_buffer_size 256k;
 large_client_header_buffers 4 256k;
 client_max_body_size 200M; 
 client_body_buffer_size 128k;
 request_pool_size  32k;
 output_buffers   4 32k;
 postpone_output  1460;
 proxy_temp_path  /tmp/nginx_proxy/;
 proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=microcache:15m inactive=24h max_size=500m;
 client_body_in_file_only on;
 log_format bytes_log "$msec $bytes_sent .";
 log_format custom_microcache '$remote_addr - $remote_user [$time_local] '
        '"$request" $status $body_bytes_sent '
        '"$http_referer" "$http_user_agent" nocache:$no_cache';
include "/etc/nginx/vhosts/*";

Then after you apply the changes, reload Nginx:

service nginx reload

By the way, all the virtual hosts files for your cPanel domains are hosted at /etc/nginx/vhosts

Popular search terms:

  • nginx
  • nginx cpanel
  • cpanel nginx
  • nginx setup for php cpanel

Esteban Borges

Linux Geek, Webperf Addict, Nginx Fan. CTO @Infranetworking

  • Ahmed

    I want to change server headers, where can I edit that?

    • nginxadmin

      You must download the source files, edit the files that shows the webserver header and then compile & install.
      If you want to hide nginx version just set server_tokens off; at nginx.conf, check this out: