小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
查看开机启动的进程,把不必要启动进程关掉。如果遇到不知道的,最好先查一下在决定要不要关闭。
声明: 猿站网有关资源均来自网络搜集与网友提供,任何涉及商业盈利目的的均不得使用,否则产生的一切后果将由您自己承担! 本平台资源仅供个人学习交流、测试使用 所有内容请在下载后24小时内删除,制止非法恶意传播,不对任何下载或转载者造成的危害负任何法律责任!也请大家支持、购置正版! 。本站一律禁止以任何方式发布或转载任何违法的相关信息访客发现请向站长举报,会员发帖仅代表会员个人观点,并不代表本站赞同其观点和对其真实性负责。本网站的资源部分来源于网络,如有侵权烦请发送邮件至:2697268773@qq.com进行处理。