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 Sysctl for high connections
Tun the following command.
# sysctl net.core.somaxconn
It should show 65536 or higher if not set the the value
echo "net.core.somaxconn=65536" >> /etc/sysctl.conf
sysctl -p
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_tcp] 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
listen.backlog = 65536 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.
can this be done with php7 as opposed to php56?
Yes this can
Will it Work With Ngnix Proxy in front of Apache?
Yes it will work
once again Sherin,
That awesome tutorial 🙂
Thanks very much.
what should be “pm” if i am wanted to create a remote download server ?? like giving youtube url download links via my server..
How could this be done as the servers main config rather than having to go into a specific users account.
Each php-fpm configuration is based on pools. So if you only need to use one pool, then create a single configuration. That is a big security risk
Hello,
I am running a VPS server running on Apache (Latest version). Last few months, I am experiencing heavy fluctuations in the traffic to my site.
I am sure that server causes the problem because whenever I get “Excessive resource usage” monitor alert from the server, the number of concurrent connections falls below 10. This will last for 2 – 10 minutes and after some 4 – 8 notification emails, the concurrent connections starts to increase.
I have the analyzed the email and found same PID [approx 5 PIDs]. I contacted hosting team and they said this is related to php-fpm processes and nothing to worry about.
But my concurrent connections starts falling. I have been searching lots of internet articles for the best solution.
Can you provide me the solution? I want to enable my server to manage efficiently 100+ concurrent connections.
Regards,
Myilraj G
Contact our support to get a good optimisation task