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 fun.com 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,

[fun]
user = fun
group = fun
listen = 127.0.0.1:9000     # This is the tcp port configuration
listen.owner = fun
listen.group = 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 fun.com

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

fastcgi_pass 127.0.0.1:9000;

Now go to WHM -> Plugins -> Cpnginx -> List Domains and select the domain fun.com, 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.

Subscribe Now

10,000 successful online businessmen like to have our content directly delivered to their inbox. Subscribe to our newsletter!

Archive Calendar

Sat Sun Mon Tue Wed Thu Fri
 123456
78910111213
14151617181920
21222324252627
28293031  

Over 20000 Satisfied Customers!

From 24/7 support that acts as your extended team to incredibly fast website performance

Zelt staff were fantastic, I had a concern with a domain and they got back to me very quickly and they helped me to resolve the issue!

author
Technician, Diageo PLC

I'm using Zelt for my portfolio since 2006. The transition was seamless, the support was immediate, and everything works perfectly.

author
Photographer, Allister Freeman

Very easy to understand & use even though I am not very technologically minded. No complications whatsoever & I wouldn't hesitate to recommend it to all.

author
Actor, A&J Artists

Zelt support team have been amazingly responsive and helpful to any of my queries, thank you so much to the Zelt have been amazingly responsive and helpful to any of my queries 👍👍👍

author
Technician, Diageo PLC

Anytime I've had a problem I can't solve, I've found Zelt to be diligent and persistent. They simply won't let an issue go until the client is happy.

author
Doctor, SmartClinics

Zelt support team have been amazingly responsive and helpful to any of my queries, thank you so much to the Zelt have been amazingly responsive and helpful to any of my queries 👍👍👍

author
Freelancer, Fiverr

24/7 World-Class Support

Ran into trouble? Contact our Customer Success team any time via live chat or email.

  • Receive professional WordPress support
  • Our specialists are available round
Get Support