How to Configure Nginx and php-fpm for High Traffic Websites

This tutorial will help you to configure your cpnginx server for managing high traffic websites. In other words this will help you to configure your nginx+php-fpm in tcp mode. The Cpnginx software ( A complete nginx plugin for cpanel servers ) by default use the unix sockets for php-fpm. So if you have a site with high traffic in your server, then the unix socket configuration will span hundreads of php-fcgi process , which may result in increasing cpu and memory usage. You can see the cpu and memory usage of the php process using the linux command “top”.

Suppose if your site gets 1000+ concurrent connections , then spanning 100+ php process is not a good idea. At this point it is better to configure the php-fpm and nginx in TP mode. So what is TCP mode ? In this mode, the php-fpm will listen on a particular port ( eg : 9000 ) as a TCP service. All requests from nginx will pass to this port only. This method won’t increase the cpu usage or memory usage.

This article will help you to configure your high traffic website in TCP mode so that you can utilize your server resource in a better way. You may need to do the following steps in your cpnginx server.

  • 1. Configure php-fpm for user on specific php version
  • 2. Create a custom template for that domain

Let us assume we have a website with cpanel user “fun” and he use php56 as his php.

Configure php-fpm for the cpanel user “fun” with php version php56

You can see the php-fpm configuration file of this user is located on /opt/cpanel/ea-php56/root/etc/php-fpm.d/fun.conf . Now copy this file as follows,

# cp -f /opt/cpanel/ea-php56/root/etc/php-fpm.d/fun.conf /opt/cpanel/ea-php56/root/etc/php-fpm.d/fun_tcp.conf

Now edit this file /opt/cpanel/ea-php56/root/etc/php-fpm.d/fun_tcp.conf and make the changes as follows,

user = fun
group = fun
listen =     # This is the tcp port configuration
listen.owner = fun = nobody
listen.mode = 0660
pm = ondemand
pm.max_children = 256
pm.process_idle_timeout = 15
pm.start_servers = 8
pm.min_spare_servers = 5
pm.max_spare_servers = 10 
pm.max_requests = 6000
php_admin_value[sys_temp_dir] = "/tmp"
php_admin_value[sys_temp_dir] = "/tmp"
php_value[session.save_path] = "/tmp" 
env[TMPDIR] = "/tmp"

Now restart the php56-fpm service

# systemctl restart ea-php56-php-fpm.service

If there is no errors in the configuration you can see a php-fpm tcp-connection listening on port 9000 . You can use the following command to see this .

# netstat -pant | grep :9000

Now we have php56-fpm on port 9000 as a tcp service for the cpanel user “fun”. Next step is to configure nginx with this port.

Create a custom nginx vhost template for the domain

As cpnginx is based on templates , you can create a vhost template file (funcom.conf). You need to follow the vhost template creation documentation from here.

So now you have a custom nginx vhost template file in /etc/cpnginx/templates/custom/funcom.conf . you need to make the following changes in this file.

Replace every occurrence of the following line in this file ,

fastcgi_pass unix:${FPM_SOCK};

With the following line


Now go to WHM -> Plugins -> Cpnginx -> List Domains and select the domain, then assign this new template and php version 5.6 for that domain. That will do the rest of the job.

This is how you change a unix socket based php-fpm to tcp in Cpnginx server. So the question is how you can create this configuration for more than one cpanel users. It is simple during the php-fpm tcp configuration time, increae the value of tcp port ( eg: 9001 ), so that you will have different php-fpm tcp ports for different users.