Need help?

Our experts will login to your server within 30 minutes to fix urgent issues.

Customer support or server support, make your own solution using our support configuration wizard.

Speed up your sites with Nginx

Some of you might have experienced slow response times from your server, which cause the website to take a long time to load. In cPanel servers, Apache is the default webserver used, both for static and dynamic pages. Now let me tell you how you can speed things up with Nginx.


One way to improve performance is to setup Nginx as a proxy. It will load static pages by default and whenever a request comes for PHP pages, those requests are passed on to Apache. But this is not what I am going to talk about today. If you want to know more about setting this up, check out this link.

To get even faster response times, even if your server is getting a lot of traffic, you can use nginx as the default webserver to load both static and dynamic pages.

In this post I will talk about setting this up on a cPanel server, on which no accounts have been created yet. Before starting the nginx installation, I recommend you to stop Apache, remove Apache from all run levels and disable Apache in the cPanel service monitoring daemon.

/etc/init.d/httpd stop
chkconfig --levels 12345 httpd off

In a cPanel server, by default, PHP and Apache will be installed on the server. You can remove the PHP binaries installed on the server or rename them. Then you should install PHP manually:

cd /usr/local/src

Use wget to download the source from (php 5.3.x or php 5.2.x). Then untar, configure and install it using the following commands:


tar -zxf php-5.x.x.tar.gz
cd php-5.x.x/
./configure --enable-fastcgi --enable-fpm --disable-pdo
--enable-bcmath --enable-calendar --enable-ftp --enable-gd-native-ttf
--enable-libxml enable-magic-quotes --enable-mbstring --enable-sockets
--with-curl=/opt/curlssl --with-curlwrappers --with-freetype-dir=/usr --with-gd
--with-gettext --with-imap=/opt/php_with_imap_client --with-imap-ssl=/usr
--with-jpeg-dir=/usr --with-kerberos --with-libdir=lib64
--with-libexpat-dir=/usr --with-libxml-dir=/opt/xml2/
--with-mcrypt=/opt/libmcrypt/ --with-mime-magic --with-mysql=/usr
--with-mysql-sock=/var/lib/mysql/mysql.sock --with-openssl=/usr
--with-openssl-dir=/usr --with-pcre-regex=/opt/pcre --with-pic
--with-png-dir=/usr --with-ttf --with-xmlrpc --with-xpm-dir=/usr --with-zlib
--with-zlib-dir=/usr --enable-sockets --enable-sysvsem --enable-sysvshm
--enable-pcntl --enable-memcache --enable-zip --with-pcre-regex
make && make install


If you get any dependency error while configuring or compiling php, you need to install the dependencies and should link it correctly while configuring PHP. Once php is installed, you can install memcache, ioncube, and other caching modules if you want.

After successfully installing php and other caching modules(if necessary), open the file “/usr/local/etc/php-fpm.conf”. Look for the lines:

! <value name="owner">nobody</value> !
! <value name="group">nobody</value> !
! <value name="user">nobody</value> !
! <value name="group">nobody</value> !

Change the value to the username under which you will be running the php-fpm processes. The username should not be “root”(recommended “nobody”). After adding the username, make sure to remove the “!” marks. Otherwise, this line will be considered as a comment.

Now, download the latest stable source file for nginx from the location given below.

cd /usr/local/src/

Then untar, configure and install it:

tar -zxf nginx-0.7.67.tar.gz
cd nginx-0.7.67
./configure --sbin-path=/usr/local/sbin --with-http_ssl_module
--without-mail_pop3_module --without-mail_imap_module --without-mail_smtp_module
make && make install

After nginx is installed, open the file “/usr/local/nginx/conf/nginx.conf” and paste the following lines in it.

user www-data;
worker_processes 6;

events {
worker_connections 1024;

http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 10 10;

gzip on;
gzip_comp_level 1;
gzip_proxied any;
gzip_types text/plain text/html text/css application/x-javascript text/xml
application/xml application/xml+rss text/javascript;

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

access_log /var/log/nginx/access.log main;

error_log /var/log/nginx/error.log debug;

include /usr/local/nginx/sites-enabled/*;

Open the file “/usr/local/nginx/conf/fastcgi_params”, add the following lines in it.

fastcgi_connect_timeout 60;
fastcgi_send_timeout 180;
fastcgi_read_timeout 180;
fastcgi_buffer_size 128k;
fastcgi_buffers 4 256k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
fastcgi_intercept_errors on;

Run the following commands:

mkdir /usr/local/nginx/sites-enabled
mkdir mkdir /var/log/nginx/

vi /usr/local/nginx/sites-enabled/default.conf

Paste the following lines in it.

server {
listen *:80;

location / {
root /usr/local/nginx/html;
index index.php;

# if file exists return it right away
if (-f $request_filename) {

# otherwise rewrite the fucker
if (!-e $request_filename) {
rewrite ^(.+)$ /index.php$1 last;


# if the request starts with our frontcontroller, pass it on to fastcgi
location ~ ^/index.php
fastcgi_param SCRIPT_FILENAME
fastcgi_param PATH_INFO $fastcgi_script_name;
include /usr/local/nginx/conf/fastcgi_params;

vi $(which php-fm)

Make sure that you are using the correct php-cgi path and the location of the php-fpm.conf is also correct.

php-fpm start

Run the command pstree -aupl and check whether both services are running on the server and responding to requests.

You can add the above commands in rc.local file so that they will be started whenever the server is rebooted. Make sure to disable apache, easyapache script and apache init script.

You can add an account from cpanel. By, default cPanel will configure the account in httpd.conf file. Use the below script to create the website in nginx.

vi /usr/bin/nginx-siteconf


/bin/cp /dev/null /usr/local/nginx/sites-enabled/sites.conf
cd /var/cpanel/users
for USER in *; do
for DOMAIN in `cat $USER | grep ^DNS | cut -d= -f2`; do
IP=`cat $USER|grep ^IP|cut -d= -f2`;
ROOT=`grep ^$USER: /etc/passwd|cut -d: -f6`;
echo “Converting $DOMAIN for $USER”;

cat >> “/usr/local/nginx/sites-enabled/sites.conf” <<EOF
server {
access_log off;

error_log /var/log/nginx/domlogs/$DOMAIN warn;
listen 80;
server_name $DOMAIN www.$DOMAIN;
root /home/$USER/public_html;

location ~ .php$ {
include /usr/local/nginx/conf/fastcgi_params;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME

/usr/local/sbin/nginx reload

chmod 755 /usr/bin/nginx-siteconf
mkdir /var/log/nginx/domlogs/

run the following command to configure the websites in shell.


Write a php info page inside your domain and check whether the nginx is loading php pages without any problem.

Now, you can see a consistent performance even if there is a large amount of traffic to your server.


One problem with running Nginx as the default webserver is that it does not follow the syntax of the rewrite rules of mod_rewrite(used by Apache). It has its own rewrite rule syntax. You can find most of them from this URL.



About the Author:

Padmakumar has been with Bobcares for just over 3 years now. In that time he’s become an expert in managing cPanel servers and is always on the lookout for new ways to get the best out of it. His love for cPanel is second only to Linux, which he’s been working with before joining Bobcares. When not diving into technical issues, he enjoys chatting with his friends, listening to music and playing cricket.

Bobcares is a server management company that helps businesses deliver uninterrupted and secure online services. Our engineers manage close to 51,500 servers that include virtualized servers, cloud infrastructure, physical server clusters, and more.