How to Configure Nginx and php-fpm for High Traffic Websites
- Category : Linux Helpline (Easy Guide)
- Posted on : Feb 09, 2019
- Views : 2,995
- By : Yakov R.
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.
Categories
- cPanel Question 47
- cPanel Software Management 29
- cPanel Tutorials 13
- Development 29
- Domain 13
- General 19
- Linux Helpline (Easy Guide) 156
- Marketing 47
- MySQL Question 13
- News 2
- PHP Configuration 14
- SEO 4
- SEO 42
- Server Administration 84
- SSL Installation 54
- Tips and Tricks 24
- VPS 3
- Web Hosting 44
- Website Security 22
- WHM questions 13
- WordPress 148
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 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 |
Recent Articles
-
Posted on : Sep 17
-
Posted on : Sep 10
-
Posted on : Aug 04
-
Posted on : Apr 01
Tags
- ts
- myisam
- vpn
- sql
- process
- kill
- tweak
- server load
- attack
- ddos mitigation
- Knowledge
- layer 7
- ddos
- webmail
- DMARC
- Development
- nginx
- seo vpn
- Hosting Security
- wireguard
- innodb
- exim
- smtp relay
- smtp
- VPS Hosting
- cpulimit
- Plesk
- Comparison
- cpu
- encryption
- WHM
- xampp
- sysstat
- optimize
- cheap vpn
- php-fpm
- mariadb
- apache
- Small Business
- Error
- Networking
- VPS
- SSD Hosting
- Link Building
- centos
- DNS
- optimization
- ubuntu