centos/linux/php-fpm服务器配置与优化

业务假设:阿里云 centos7+nginx+php-fpm+mysql

第一章 centos调优

vi /etc/sysctl.conf
net.ipv4.tcp_max_tw_buckets = 6000
net.ipv4.tcp_sack = 1
net.ipv4.tcp_window_scaling = 1
net.ipv4.tcp_rmem = 4096 87380 4194304
net.ipv4.tcp_wmem = 4096 16384 4194304
net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.core.netdev_max_backlog = 262144
net.core.somaxconn = 262144
net.ipv4.tcp_max_orphans = 3276800
net.ipv4.tcp_max_syn_backlog = 262144
net.ipv4.tcp_timestamps = 0
net.ipv4.tcp_synack_retries = 1
net.ipv4.tcp_syn_retries = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_mem = 94500000 915000000 927000000
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_keepalive_time = 30
net.ipv4.ip_local_port_range = 1024 65000

/sbin/sysctl -p
;修改后要重启

第二章 php-fpm的调优

为减少不必要的模块,进而减少每个php-fpm进程耗用的内存,推荐使用yum安装后逐个去掉没必要加载的扩展(但仍没有手工编译效果好,会多一半内存耗费,通过lsof查看进程打开的文件数,Yum安装的多了近30个文件),所有的扩展的加载定义在/etc/php.d/目录下。

Yum方式安装后一般可以去掉的模块(根据你自己的实际情况修改)
sqlite3
pdo_sqlite
calendar
exif
ftp
gmp
igbinary
shmop
sysvmsg
sysvsem
sysvshm
xml_wddx
xsl

另外一种方式是从源代码安装,不过这种方式安装的php-fpm不太好管理,还要手工去配置启动服务等,安装的路径也不统一。从源码安装编译后php-fpm文件在33M左右,刚启动占用内存8M。

./configure --prefix=/usr/local/php56 --with-config-file-path=/usr/local/php56/etc --enable-inline-optimization --disable-debug --disable-rpath --enable-shared --enable-opcache --enable-fpm --with-fpm-user=apache --with-fpm-group=apache --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-gettext --enable-mbstring --with-iconv --with-mcrypt --with-mhash --with-openssl --enable-bcmath --enable-soap --with-libxml-dir --enable-pcntl --enable-shmop --enable-sysvmsg --enable-sysvsem --enable-sysvshm --enable-sockets --with-curl --with-zlib --enable-zip --with-bz2 --with-readline --with-gd=/usr/local --with-png-dir=/usr/ --with-png-dir=/usr --with-freetype-dir=/usr --enable-gd-native-ttf --without-sqlite3 --without-pdo-sqlite --disable-sysvsem --disable-sysvshm  --disable-sysvmsg --disable-shmop --disable-soap --without-gettext --without-ldap --without-ldap-sasl

#注意参数--with-fpm-systemd的必要性,如果你在centos7里安装php-fpm成service,并且service里配置了notify通知方式,则--with-fpm-systemd参数是必须的,否则会启动不了服务。
#不过--with-fpm-systemd在某些系统里不支持,如centos7。
#可以将php-fpm.service中的通知类型改为simple

安装后的php-fpm.conf一些配置指令的解释:

1 php-fpm运行一段时间后会有内存泄露的问题,所以下面的参数不能设得太高

max_requests=0

Warning!此参数有可能导致连接间歇性重置,如果是订单等关键系统,不要配置成一个大于0的数字,调成0不让Php-fpm重启

这个参数的意思是当前php-fpm进程处理了512个请求后会重启这个进程,这样可以释放之前占用的资源。在实际生产环境中php内存泄露还是比较严重的。

2 如果php不稳定,有时会异常退出,下面的配置可以监控,如1分钟内超过10次异常,将自动平滑重启fpm

emergency_restart_threshold = 10
emergency_restart_interval = 1m

3 并发进程管理和数量。推荐直接使用static,因为动态要复杂的计算才能知道有什么效果,如果内存足够,直接设置固定进程数量比较好。max_children的数量按单进程35M计算,max_children就是=空闲内存/35M,要多留一些富余的内存。内存如果运行一段时间比较稳定后,可以适当调大max_children,将并发处理数量提高一些。

pm = static

#此值不能过大,如果查看php错误日志发现内存不足,要降低此值
pm.max_children = 256
;32G内存时
;pm.max_children = 1300

4 要减少单进程使用的内存量,编译时优化一些参数,将不必要的组件去掉。

lsof -p 进程id
查下找开的文件,删减一些没必要的打开。同时发现引用/usr/lib/locale/locale-archive文件有100M以上,这个是语言包,有些语言基本不用,可以重新打包生成一个压缩版本:cp /usr/lib/locale/locale-archive /usr/lib/locale/locale-archive.bak && rm -fv /usr/lib/locale/locale-archive && localedef -f UTF-8 -i zh_CN zh_CN.UTF8 && localedef -v -c -i en_US -f UTF-8 en_US.UTF-8  && localedef --list-archive

2019.5.6 发现locale-archive在系统升级(打补丁)后会恢复默认大小,需要重新删除再打包。

关于locale的更多解释参考这里

5 并发请求数量,还依赖于cpu内核数量,内核越多并发越高

6 并发处理数量,还依赖于数量库和php本身程序的优化,如果业务处理时间越短,相对来讲并发数量就越高,如果业务处理缓慢导致卡顿,容易产生雪崩效应。

php-fpm配置汇总

pm = static
pm.max_children = 1300
max_requests=0

pm.start_servers = 20


emergency_restart_threshold = 10
emergency_restart_interval = 1m

;下面两条指令不能修改,会报错 failed to set rlimit_core for this pool,可能是系统的limits没调整好
;rlimit_files=65535
;rlimit_core = unlimited

request_slowlog_timeout = 10s
slowlog = /var/log/php-fpm/slowlog-site.log

php-fpm.service例子  

[Unit]
Description=The PHP FastCGI Process Manager
After=syslog.target network.target

[Service]
#或改为simple
Type=notify
PIDFile=/usr/local/php56/var/run/php-fpm.pid
EnvironmentFile=/etc/sysconfig/php-fpm
ExecStart=/usr/local/php56/sbin/php-fpm --nodaemonize
ExecReload=/bin/kill -USR2 $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

第三章 nginx 优化

vi /etc/nginx/conf/nginx.conf

worker_processes  8;
worker_rlimit_nofile 65535;

第四章  手动安装非php原生支持的第三方扩展,如redis

#下面的值受/proc/sys/net/core/somaxconn值的影响,
#确保sysctl.conf已经定义了
#net.core.netdev_max_backlog = 262144
#net.ipv4.tcp_max_syn_backlog = 262144
#注意下面的值不能超过65535
#net.core.somaxconn = 65535

tcp-backlog 65535

vi /etc/sysctl.conf
echo never > /sys/kernel/mm/transparent_hugepage/enabled

如果有配置密码的记得修改 requirepass 参数

redis更多调优设置请参数这篇文章

进程内存分析请点击

转载于:https://my.oschina.net/swingcoder/blog/1457015

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值