nginx php-fpm 小VPS 优化

时间:2022-06-01 20:46:55

小VPS受系统资源的限制,访问量过大,超过系统所能承受的极限时,有一部分请求就会502了。在系统资源够用的情况,优化nginx,php-fpm,以及系统本身,达到2个目的:

1,合理配置系统资源,将有限的资源,最大化利用。好钢用在刀刃上。

2,尽量减少磁盘的I/O

一,系统主要资源

?
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
31
[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 。。。。省略。。。。 
bogomips    : 5188.04 
clflush size  : 64 
cache_alignment : 64 
address sizes  : 46 bits physical, 48 bits virtual 
power management:

二,php-fpm优化

?
1
2
3
4
5
6
7
pm = dynamic           //进程数,动态分配
pm.max_children = 24       //最大进程数
pm.start_servers = 8       //刚启动时的进程数
pm.min_spare_servers = 8     //服务器空闲时的最小进程数
pm.max_spare_servers = 24     //服务器空闲时的最大进程数
 
php_flag[display_errors] = off  //运行一段时间后,将错误提示信息关闭掉

php-fpm一个进程占了20M-30M之间,top看一下php-fpm占的内存百分比,估算一下就知道了。max_children,max_spare_servers不是越大越好。

三,nginx优化

1,安装稳定最新版

?
1
2
3
4
5
6
7
8
9
# vim /etc/yum.repos.d/nginx.repo  //加上以下内容
 
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1
 
# yum install nginx   //更新nginx

2,优化配置nginx

?
1
2
3
4
5
6
7
8
9
10
11
12
worker_processes auto;   //设置auto,nginx进程动态分配
 
# access_log  //注释掉,减少I/O
# log_format  //注释掉,减少I/O
 
gzip on;     //开启gzip
gzip_min_length 1k;
gzip_buffers   4 16k;
gzip_http_version 1.1;
gzip_comp_level 5;    //1-9,越大压缩越好,消耗资源越大
gzip_types    text/plain application/x-javascript text/css application/xml;
gzip_vary on;

worker_processes对于小VPS的话,设成1,2也是可以的。够用了。

对于小vps的话,上面nginx,php-fpm的优化配置都是有益的。

四,linux启动进程优化

 

复制代码 代码如下:

# chkconfig --list |grep on

 

查看开机启动的进程,把不必要启动进程关掉。如果遇到不知道的,最好先查一下在决定要不要关闭。