Home >Backend Development >PHP Tutorial >Optimization tips for Nginx+PHP-FPM
This article mainly introduces the optimization skills of Nginx PHP-FPM, which has certain reference value. Now I share it with you. Friends in need can refer to it
Here is an article I found on the Internet. I have practiced it carefully. There are many places worthy of reference that can be learned. Since the layout of the previous article is very confusing, I will rewrite and organize this article while studying. All rights reserved. All rights reserved to the original author
I have briefly introducedUnix Domain Socket
this communication method before, see:Nginx PHP-FPM
DomainSocket
Configuration methodUnix
DomainSocket
Because it does not go through the network, it can indeed improveNginx
andphp- fpm
Communication performance, but it will be unstable under high concurrency.
Nginx
will frequently report errors:
connect() to unix:/dev/shm/php-fcgi.sock failed (11: Resource temporarily unavailable) while connecting to upstream
You can improve stability in the following two ways:
1. Increase the Thephp-fpm parameter tuning2.1 Number of processesbacklog
configuration method in
nginxand
php-fpm
is:of this domain name in the
nginxconfiguration file Under server
, adddefault backlog=1024
afterlisten 80
.
Also configurelisten.backlog
inphp-fpm.conf
to1024
, the default is128
.
2. Increase the number ofsock
files andphp-fpm
instances and create a newsock
file and pass ## inNginx
The #upstreammodule load-balances requests to two sets of
php-fpminstances behind two
sock
files.
# php-fpm初始/空闲/最大worker进程数 pm.max_children = 300 pm.start_servers = 20 pm.min_spare_servers = 5 pm.max_spare_servers = 35
php The worker
process of -fpm will be terminated after processing a number of requests, and the
master process will
respawn a new one.
The main purpose of this configuration is to avoid memory leaks caused by
php third-party libraries referenced by the interpreter or program
pm.max_requests = 102402.3 Maximum execution timeMaximum execution The time can be configured in
php.ini and
php-fpm.conf. The configuration items are
max_execution_time and
request_terminate_timeout respectively.
For its role and impact, please refer to: Detailed explanation of 502 and 504 errors in Nginx
top command: After directly executing the
top command, enter 1 to see the
CPU usage of each core. And the sampling time can be shortened by
top -d 0.1.
The following
sar seems to be the shortest is 1 second
sar command:
# sar和iostat命令的安装: sysstat.x86_64 : The sar and iostat system monitoring commands yum install -y sysstat.x86_64 # 执行sar -P ALL 1 100。-P ALL表示监控所有核心,1表示每1秒采集,100表示采集100次。 # 输出结果如下: CPU %user %nice %system %iowait %steal %idle all 85.54 0.00 5.69 0.00 0.00 8.76 0 74.75 0.00 25.25 0.00 0.00 0.00 1 98.00 0.00 2.00 0.00 0.00 0.00 2 89.22 0.00 3.92 0.00 0.00 6.86 3 91.00 0.00 2.00 0.00 0.00 7.00 4 75.00 0.00 9.00 0.00 0.00 16.00 5 94.95 0.00 5.05 0.00 0.00 0.00 6 95.00 0.00 4.00 0.00 0.00 1.00 7 87.88 0.00 4.04 0.00 0.00 8.08 8 93.94 0.00 3.03 0.00 0.00 3.03 9 88.00 0.00 3.00 0.00 0.00 9.00 10 89.11 0.00 2.97 0.00 0.00 7.92 11 82.35 0.00 3.92 0.00 0.00 13.73 12 73.27 0.00 7.92 0.00 0.00 18.81 13 81.44 0.00 4.12 0.00 0.00 14.43 14 77.23 0.00 6.93 0.00 0.00 15.84 15 78.79 0.00 4.04 0.00 0.00 17.172. Turn on the slow logConfiguration output
php-fpmSlow log, the threshold is 2 seconds:
request_slowlog_timeout = 2 slowlog = log/$pool.log.slow
Use the sort/uniq command to analyze and summarize the php-fpm slow log:
[root@boole log] grep -v "^$" www.log.slow.tmp | cut -d " " -f 3,2 | sort | uniq -c | sort -k1,1nr | head -n 50 5181 run() /www/test.net/framework/web/filters/CFilter.php:41 5156 filter() /www/test.net/framework/web/filters/CFilterChain.php:131 2670 = /www/test.net/index.php 2636 run() /www/test.net/application/controllers/survey/index.php:665 2630 action() /www/test.net/application/controllers/survey/index.php:18 2625 run() /www/test.net/framework/web/actions/CAction.php:75 2605 runWithParams() /www/test.net/framework/web/CController.php:309 2604 runAction() /www/test.net/framework/web/filters/CFilterChain.php:134 2538 run() /www/test.net/framework/web/CController.php:292 2484 runActionWithFilters() /www/test.net/framework/web/CController.php:266 2251 run() /www/test.net/framework/web/CWebApplication.php:276 1799 translate() /www/test.net/application/libraries/Limesurvey_lang.php:118 1786 load_tables() /www/test.net/application/third_party/php-gettext/gettext.php:254 1447 runController() /www/test.net/framework/web/CWebApplication.php:135 # 参数解释: sort: 对单词进行排序 uniq -c: 显示唯一的行,并在每行行首加上本行在文件中出现的次数 sort -k1,1nr: 按照第一个字段,数值排序,且为逆序 head -10: 取前10行数据3. Use strace to track the process 1. Use
nohup to convert
strace to background execution until
attach
php-fpm Until the process dies:
nohup strace -T -p 13167 > 13167-strace.log & # 参数说明: -c 统计每一系统调用的所执行的时间,次数和出错的次数等. -d 输出strace关于标准错误的调试信息. -f 跟踪由fork调用所产生的子进程. -o filename,则所有进程的跟踪结果输出到相应的filename -F 尝试跟踪vfork调用.在-f时,vfork不被跟踪. -h 输出简要的帮助信息. -i 输出系统调用的入口指针. -q 禁止输出关于脱离的消息. -r 打印出相对时间关于,,每一个系统调用. -t 在输出中的每一行前加上时间信息. -tt 在输出中的每一行前加上时间信息,微秒级. -ttt 微秒级输出,以秒了表示时间. -T 显示每一调用所耗的时间. -v 输出所有的系统调用.一些调用关于环境变量,状态,输入输出等调用由于使用频繁,默认不输出. -V 输出strace的版本信息. -x 以十六进制形式输出非标准字符串 -xx 所有字符串以十六进制形式输出. -a column 设置返回值的输出位置.默认为40. -e execve 只记录 execve 这类系统调用 -p 主进程号2. You can also use the
-c parameter to let
strace help summarize, which is very convenient and powerful !
[root@b28-12 log]# strace -cp 9907 Process 9907 attached - interrupt to quit Process 9907 detached % time seconds usecs/call calls errors syscall ------ ----------- ----------- --------- --------- ---------------- 56.61 0.016612 5 3121 read 11.11 0.003259 1 2517 715 stat 8.04 0.002358 7 349 brk 6.02 0.001767 1 1315 poll 4.28 0.001255 6 228 recvfrom 2.71 0.000796 1 671 open 2.54 0.000745 0 2453 fcntl 2.37 0.000696 1 1141 write 1.69 0.000497 1 593 13 access 1.37 0.000403 0 1816 lseek 0.89 0.000262 1 451 22 sendto 0.56 0.000163 1 276 208 lstat 0.49 0.000145 0 384 getcwd 0.31 0.000090 0 1222 fstat 0.28 0.000082 0 173 munmap 0.26 0.000077 0 174 mmap 0.24 0.000069 2 41 socket 0.23 0.000068 0 725 close 0.00 0.000000 0 13 rt_sigaction 0.00 0.000000 0 13 rt_sigprocmask 0.00 0.000000 0 1 rt_sigreturn 0.00 0.000000 0 78 setitimer 0.00 0.000000 0 26 26 connect 0.00 0.000000 0 15 2 accept 0.00 0.000000 0 39 recvmsg 0.00 0.000000 0 26 shutdown 0.00 0.000000 0 13 bind 0.00 0.000000 0 13 getsockname 0.00 0.000000 0 65 setsockopt 0.00 0.000000 0 13 getsockopt 0.00 0.000000 0 8 getdents 0.00 0.000000 0 26 chdir 0.00 0.000000 0 1 futex ------ ----------- ----------- --------- --------- ---------------- 100.00 0.029344 18000 986 total4. Speed up PHP interpretation and executionIf there is indeed no problem with your program, it just performs too many operations and cannot be optimized anymore. Then consider using PHP accelerators such as
APCInstallationor
xcacheto reduce the time it takes
CPUto interpret
phpfiles.
These
PHPaccelerators will generate intermediate code
opcodewhen the
phpfile is first interpreted, so subsequent executions will be much faster and less expensive.
CPUoperations. Let's take
xcacheas an example.
Let's see how to install and configure it.
xcacheThe command is as follows. There are many parameters in
./configure and I don’t know what they are used for. There is no explanation on the official website, so I only enable
- -enable-xcache: The configuration in
tar zxvf xcache-3.0.3.tar.gz /usr/local/php/bin/phpize ./configure --with-php-config=/usr/local/php/bin/php-config --enable-xcache make make install
php.ini is as follows. The most important ones are the two parameters marked in red. It is generally recommended
xcache.sizeDetermined based on the number of
php files,
xcache.count is the same as
CPU core number:
[xcache.admin] xcache.admin.enable_auth = Off xcache.admin.user = "xcache" xcache.admin.pass = "" [xcache] xcache.shm_scheme ="mmap" xcache.size=1024M xcache.count =16 xcache.slots =8K xcache.ttl=0 xcache.gc_interval =0 xcache.var_size=16M xcache.var_count =1 xcache.var_slots =8K xcache.var_ttl=0 xcache.var_maxttl=0 xcache.var_gc_interval =300 xcache.test =Off xcache.readonly_protection = Off ;xcache.readonly_protection = On xcache.mmap_path ="/dev/zero" ;xcache.mmap_path ="/tmp/xcache" xcache.coredump_directory ="" xcache.cacher =On xcache.stat=On xcache.optimizer =Off [xcache.coverager] ;;xcache.coverager =On ;;xcache.coveragedump_directory =""A common problem is starting
php -fpm will report an error:
Cannot open or create file set by xcache.mmap_path, check the path permission or check xcache.size/var_size against system limitationThis is because
/tmp/xcache is a file and cannot be created as a directory.
php-fpm service, use the
top command to observe the
VIRT( Including the
swap area) are all
xcache.size size, but
REQ becomes very small.
Using the above configuration has shortened the peak time of CPU
usage, but at the peak time all cores will still reach more than
90%. I don’t know if there is no configuration somewhere. right.
In addition, when concurrency is high, /dev/zero
this configuration method often leads to
Nginx 502 errors.
/tmp/xcache and turning on
readonly_protection are very stable.
常用的方法就是开启xdebug
的性能监控功能,将xdebug输出结果通过WinCacheGrind
软件分析。xdebug
的安装和配合IDE调试的方法参见:Vim+XDebug调试PHP
php.ini中配置的这几项是输出性能信息的:
xdebug.auto_trace = on xdebug.auto_profile = on xdebug.collect_params = on xdebug.collect_return = on xdebug.profiler_enable = on xdebug.trace_output_dir = "/tmp" xdebug.profiler_output_dir ="/tmp"
这样XDebug会输出所有执行php函数的性能数据,但产生的文件也会比较大。可以关闭一些选项如collect_params、collect_return,
来减少输出的数据量。或者关闭自动输出,通过在想要监控的函数首尾调用xdebug函数来监控指定的函数。
输出的文件名类似cachegrind.out.1277560600
和trace.3495983249.txt
,可以拿到Windows
平台下用WinCacheGrind
进行图形化分析。WinCacheGrind
使用方法网上有很多介绍,这里就不详细说明了,WinCacheGrind for github
以上都是近期做php
程序优化工作总结出的一些优化方法,针对每个地方的配置请详细阅读官方文档进行修改,并不一定要以本文为依据,本文档只阐述方法
相关推荐:
The above is the detailed content of Optimization tips for Nginx+PHP-FPM. For more information, please follow other related articles on the PHP Chinese website!