nginx php fpm small VPS optimization

  • 2020-05-12 06:46:37
  • OfStack

Small VPS is limited by system resources and has too much traffic. When it exceeds the limit that the system can bear, 1 part of the request will be 502. When system resources are sufficient, nginx, php-fpm, and the system itself are optimized for two purposes:

1. Rationally allocate system resources and maximize the use of the limited resources. Good steel is used on the edge of a knife.

2, minimize the I/O disks

1. Main resources of the system


[root@xxxxxx nginx]# free -m  
       total    used    free   shared  buffers   cached  
Mem:      994    815    179     0     43    118  
-/+ buffers/cache:    453    540  
Swap:      0     0     0  
 
[root@xxxxxx nginx]# cat /proc/cpuinfo  
processor    : 0  
vendor_id    : GenuineIntel  
cpu family   : 6  
model      : 62  
model name   : Intel(R) Xeon(R) CPU E5-2650 v2 @ 2.60GHz  
stepping    : 4  
cpu MHz     : 2594.024  
cache size   : 20480 KB  
physical id   : 0  
siblings    : 1  
core id     : 0  
cpu cores    : 1  
apicid     : 0  
initial apicid : 0  
fpu       : yes  
fpu_exception  : yes  
cpuid level   : 13  
wp       : yes  
flags      : fpu vme de pse tsc msr pae mce cx8 apic  . Omit...   
bogomips    : 5188.04  
clflush size  : 64  
cache_alignment : 64  
address sizes  : 46 bits physical, 48 bits virtual  
power management: 

2. php-fpm optimization


pm = dynamic           // Number of processes, dynamic allocation 
pm.max_children = 24       // Maximum number of processes 
pm.start_servers = 8       // Number of processes at startup 
pm.min_spare_servers = 8     // The minimum number of processes when the server is idle 
pm.max_spare_servers = 24     // The maximum number of processes when the server is idle 

php_flag[display_errors] = off  // run 1 After a while, turn off the error message 

php-fpm1 processes take up between 20M-30M. top look at the percentage of memory that php-fpm takes up. max_children, max_spare_servers not bigger is better.

3. nginx optimization

1. Install and stabilize the latest version


# vim /etc/yum.repos.d/nginx.repo  // Add the following 

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1

# yum install nginx   // update nginx

2. Optimize the configuration of nginx


worker_processes auto;   // Set up the auto . nginx Process dynamic allocation 

# access_log  // Comment it out, reduce it I/O
# log_format  // Comment it out, reduce it I/O

gzip on;     // open gzip
gzip_min_length 1k;
gzip_buffers   4 16k;
gzip_http_version 1.1;
gzip_comp_level 5;    //1-9, The greater the compression, the better, and the greater the resource consumption 
gzip_types    text/plain application/x-javascript text/css application/xml;
gzip_vary on;

worker_processes for small VPS, 1,2 is also fine. Will be enough.

For small vps, the optimized configuration of nginx, php-fpm above is beneficial.

4. linux starts process optimization


# chkconfig --list |grep on

Check the startup process and turn off the unnecessary startup process. If you encounter something you don't know, you'd better look it up before deciding whether to close it.


Related articles: