nignx +PHP 性能优化

 于Nginx对高并发的优良性能,故配了个Nginx+php-fpm来跑在线代理程序,是按照张宴文章配的,刚配置好时运行正常,但运行一段时间 后,网站打开很慢,打开网站后,在输入框输入要访问的网站,也慢得不行。在网站打开慢时,在SSH终端上输入命令也慢,怀疑是机房网速问题,但在ssh上 输入
w3m www.linuxidc.com
这个打开也慢,基本可以排除机房的网速问题。

当打开网站慢时,把服务器重启后,就会快起来,后来发现,用
/usr/local/webserver/php/sbin/php-fpm restart
把fastcgi重启下也会快起来,最把它加入计划任务,每小时重启下,基本保证网站不会慢,但终究不是办法。
查看了nginx.log和php-fpm.log,根据里面的错误,找了以上转载的几篇文章,总算是把问题解决了,主要修改了两个地方
问题:
发现/usr/local/webserver/php/etc/php-fpm.conf文件里定义的打开文件描述符的限制数量是
<value name="rlimit_files">51200</value>
但用 命令ulimit -n查看,发现只有1024
我已在/etc/rc.local里添加了
ulimit -SHn 51200
竟然没生效:
解决:
vi /etc/security/limits.conf
文件最后加上
* soft nofile 51200
* hard nofile 51200
2、
问题:
用命令
netstat -np | grep 127.0.0.1:9000 |wc -l
发现只有100多
解决:
根据服务器内存情况,可以把PHP FastCGI子进程数调到100或以上,在4G内存的服务器上200就可以
服务器上内存为8G,我把PHP FastCGI子进程数调整到300
vi /usr/local/webserver/php/etc/php-fpm.conf
将max_children修改为300
<value name="max_children">300</value>
重启服务器,这样,网站打开速度快,而且稳定了。

When you running a highload website with PHP-FPM via FastCGI, the following tips may be useful to you : ) '
如果您高负载网站使用PHP-FPM管理FastCGI,这些技巧也许对您有用:)
1. Compile PHP’s modules as less as possible, the simple the best (fast);
1.尽量少安装PHP模块,最简单是最好(快)的
2. Increas PHP FastCGI child number to 100 and even more. Sometime, 200 is OK! ( On 4GB memory server);
2.把您的PHP FastCGI子进程数调到100或以上,在4G内存的服务器上200就可
注:我的1g测试机,开64个是最好的,建议使用压力测试获取最佳值
3. Using SOCKET PHP FastCGI, and put into /dev/shm on Linux;
3.使用socket连接FastCGI,linux操作系统可以放在 /dev/shm中
注:在php-fpm.cnf 里设置<value name="listen_address">/tmp/nginx.socket</value>就可以通过socket连接 FastCGI了,/dev/shm是内存文件系统,放在内存中肯定会快了 (这一步待确认
4. Increase Linux “max open files”, using the following command (must be root):
# echo ‘ulimit -HSn 65536′ >> /etc/profile
# echo ‘ulimit -HSn 65536 >> /etc/rc.local
# source /etc/profile
4.调高linux内核打开文件数量,可以使用这些命令(必须是root帐号)
echo 'ulimit -HSn 65536' >> /etc/profile
echo 'ulimit -HSn 65536' >> /etc/rc.local
source /etc/profile
注:我是修改/etc/rc.local,加入ulimit -SHn 51200的


max_children :这个要按照服务器的繁忙程度来计算,最好是得到详细的访问者日志后再进行调整。设置的值越大后台跑的php-cgi 进程就越多,这样排队速度就越快。反之就是越少就越慢。但是每一个php-cgi所耗费的内存在20M左右。所以2g的服务器这个值默认都设置成40-64。

request_slowlog_timeout:0s的含义是让PHP-CGI一直执行下去而没有时间限制。默认就设置成0,如果可能出现脚本执行时间超长的话,那就设置为600s左右。








=====================================================================
Nginx+PHP-fpm组合,以内存占用小,负载能力强壮的特点,成为小内存VPS建站的首选组合。我们一起来探讨一下nginx+php-fpm高负载的优化方法。
先来看看nginx配置参数的优化。nginx是前端接受浏览器端请求的web server, 配置可调的参数如下:
下面是示例nginx配置
user www-data;
worker_processes 8;
#worker_processes 调至8, 大于8没什么用,小于8,nginx性能发挥不出来
worker_cpu_affinity 01 10 01 10 01 10 01 10;
#worker_cpu_affinity 参数可以使nginx充分发挥多核Cpu的性能优势 ,上面的配置是针对双核CPU的配置。01表示第一个核,10表示第二个核,如果是四核cpu,一至四个核分别表示为 0001 0010 0100 1000
error_log /var/log/nginx/error_log crit;
pid /var/run/nginx.pid;
worker_rlimit_nofile 10240;
#worker_rlimit_nofile 是nginx能打开文件的最大句柄数,我们需要把这个数字设大一点。
#linux系统的文件查看数限制查看是用 ulimit -n ,修改这个限制是用 ulimit -HSn 65535
events
{
use epoll;
#必须要用高效的event驱动,以获得最大性能
worker_connections 10240;
#max_clients = worker_processes * worker_connections/4 (最大连接数的计算公式)
}
http
{
include /etc/nginx/deny.iplist;
include /etc/nginx/mime.types;
default_type application/octet-stream;
server_name_in_redirect off;
server_names_hash_bucket_size 128;
server_tokens off;
client_header_buffer_size 32k;
#client头buffer可以调为32K
large_client_header_buffers 4 32k;
client_max_body_size 8m;
sendfile on;
tcp_nopush on;
keepalive_timeout 65;
tcp_nodelay off;
client_body_timeout 10;
client_header_timeout 10;
send_timeout 60;
output_buffers 1 32k;
postpone_output 1460;
open_file_cache max=1000 inactive=20s;
open_file_cache_valid 30s;
open_file_cache_min_uses 2;
open_file_cache_errors on;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 32k;
fastcgi_buffers 4 32k;
fastcgi_busy_buffers_size 32k;
fastcgi_temp_file_write_size 32k;
gzip on;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_proxied expired no-cache no-store private auth;
proxy_cache_path /var/lib/nginx/cache levels=1:2 keys_zone=staticfilecache:80m inactive=1d max_size=2500m;
proxy_temp_path /var/lib/nginx/proxy;
proxy_connect_timeout 300;
proxy_read_timeout 120;
proxy_send_timeout 120;
proxy_buffer_size 16k;
proxy_buffers 4 16k;
upstream wordpressnginx
{
server 127.0.0.1:6000 weight=1 fail_timeout=120s;
}
include /etc/nginx/sites-enabled/*;
}
上面的配置里面,有多处设及到buffer和timeout的地方。我们可以根据需要,慢慢调大这些参数,buffer自然是大点好,但不要太大。16K是标准配置,可以增加到32,往上加更大也不是不行,但 要考虑到你系统内存大不大,够不够用。timeout是超时,如果服务器很繁忙,不妨增加超时等待时间,以避免频繁出现502错误。
gzip是必须开启的,reverse proxy在允许的情况下,也尽量开启,一 是可以提升响应效率,二是降低服务器压力,gzip开启后更可以节省服务器带宽。
nginx主要的配置如上所述。
现在看一下php-fpm的配置。
[global]
pid = run/php5-fpm.pid
process_control_timeout = 5
[www]
listen = /dev/shm/php-cgi.sock
listen.allowed_clients = 127.0.0.1
user = www-data
group = www-data
pm = static
pm.max_children = 7
#这个决定了 php-fpm的总进程。我们要想同时响应更多的并发数,这个数值要尽可能大,比如500,1000
pm.max_requests = 10000
#并发数越大,这个最大请求数应该越大,并发数小,这个数值也应该越小。它表示,php-fpm进程响应了10000个并发请求之后,就自动重启一下进程。
request_terminate_timeout = 30
#表示等待30秒后,结束那些没有自动结束的php脚本,以释放占用的资源。
env[HOSTNAME] = $HOSTNAME
env[PATH] = /usr/local/bin:/usr/bin:/bin
env[TMP] = /tmp
env[TMPDIR] = /tmp
env[TEMP] = /tmp
小内存的vps虽然经过使用php-fpm+nginx,提升了系统的效率,可以同时响应较多的并发请求,但是当并发数上来了,比如从100上升到10000,小内存肯定响应不过来,cpu也会 因为太忙,而导致系统负载变得很高很高,这个时候,我们就要考虑升级硬件配置了。
内存越大越好,CPU核心频率越高越好,CPU核越多越好。硬盘最好是SSD+RAID10。这样性能不仅高,数据安全也有保障。
上面所提到的各个配置参数,设及到数值的,不妨自己 多试着调小,调大参数,然后重启下nginx或者php-fpm进程,看看效果怎么样。
下面介绍一个比较好的压力测试工具,siege.
debian和ubuntu用户可以通过apt-get install siege来安装siege.
siege是一个跟ab.exe相似的http压力测试软件。
我们可以用siege来测试我们的网站和服务器性能。
siege -r 100 -c 10 http://www.domain.com/test.php
-r 是 repeat , -r 100是重复100次测试
-c 10是表示模拟10个用户同时并发连接
最后面是要测试的URL地址。
测试过程中可以随时按CTRL+C中止进程,siege会生成一个报告给我们。
我们可以同时根据siege的测试结果和监视服务器的负载情况,对系统压力状况进行一个深入了解和分析。接下来可以帮助我们判断该如何进行下一步操作,是继续优化配置,还是升级硬件。


==================================================================

When you running a highload website with PHP-FPM via FastCGI, the following tips may be useful to you : )
如果您高负载网站使用PHP-FPM管 理FastCGI,这些技巧也许对您有用:)
1. Compile PHP’s modules as less as possible, the simple the best (fast);
1.尽量少安装PHP模块,最简单是最好(快)的
2. Increas PHP FastCGI child number to 100 and even more. Sometime, 200 is OK! ( On 4GB memory server);
2.把您的PHP FastCGI子进程数调到100或以上,在4G内存的服务器上200就可以
注:我的1g测试机,开64个是最好的,建议使用压力测试获取最佳值

3. Using SOCKET PHP FastCGI, and put into /dev/shm on Linux;
3.使用socket连接FastCGI,linux操作系统可以放在 /dev/shm中
注: 在php-fpm.cnf 里设置<value name="listen_address">/tmp/nginx.socket</value>就可以通过socket连接 FastCGI了,/dev/shm是内存文件系统,放在内存中肯定会快了
4. Increase Linux “max open files”, using the following command (must be root):
# echo ‘ulimit -HSn 65536′ >> /etc/profile
# echo ‘ulimit -HSn 65536 >> /etc/rc.local
# source /etc/profile
4.调高linux内核打开文件数量,可以使用这些命令(必须是root帐号)
echo 'ulimit -HSn 65536' >> /etc/profile
echo 'ulimit -HSn 65536' >> /etc/rc.local
source /etc/profile
注:我是修改/etc/rc.local,加入ulimit -SHn 51200的
5. Increase PHP-FPM open file description rlimit:
# vi /path/to/php-fpm.conf
Find “<value name=”rlimit_files”>1024</value>”
Change 1024 to 4096 or higher number.
Restart PHP-FPM.
5. 增加 PHP-FPM 打开文件描述符的限制:
# vi /path/to/php-fpm.conf
找到“<value name="rlimit_files">1024</value>”
把1024 更改为 4096 或者更高.
重启 PHP-FPM.
6. Using PHP code accelerator, e.g eAccelerator, XCache. And set “cache_dir” to /dev/shm on Linux.
6.使用php代码加速器,例如 eAccelerator, XCache.在linux平台上可以把`cache_dir`指向 /dev/shm

posted on 2013-12-27 17:26  一个石头  阅读(640)  评论(0编辑  收藏  举报