An example of PHP-FPM optimization and pressure test in CentOS

Source: Internet
Author: User
Tags fpm socket centos

PHP fastcgi process Manager, compared to fastcgi static evoke CGI,FPM can dynamically invoke the CGI process and destroy to reach the dynamic adjustment CGI number according to the access pressure, so that memory can be used effectively. There are other advantages, for example, FPM can also be smooth overload of the PHP configuration, because FPM is using Unix-socket to communicate with the server, so there is no need to configure the CGI port, FPM have better state output and Slowlog log, When you are 502, you can give more error details.

This article discusses configuration details about PHP-FPM and stress testing based on a real-world environment:

The server configuration is as follows:
4 Core 4G Memory
Nginx version for 1.7.0
PHP version for 5.3.27

The Nginx configuration file is as follows

The code is as follows Copy Code
User www www.
Worker_processes 8;
Error_log/var/log/nginx/error.log warn;
Worker_rlimit_nofile 204800;
PID Logs/nginx.pid;
Events {
Use Epoll;
Worker_connections 204800;
}
HTTP {
Include Mime.types;
Default_type Application/octet-stream;
Log_format Main ' $remote _addr-$remote _user [$time _local] ' $request '
' $status $body _bytes_sent ' $http _referer '
' $http _user_agent ', ' $http _x_forwarded_for ';
Access_log/var/log/nginx/access.log main;
Index index.shtml index.php index.html index.htm;
Server_names_hash_bucket_size 128;
Client_header_buffer_size 32k;
Open_file_cache max=204800 inactive=20s;
# # #我这边测试, this is turned on PHP processing performance has 15% upgrade ###
Open_file_cache_valid 30s;
Open_file_cache_min_uses 1;
Large_client_header_buffers 4 32k;
Client_max_body_size 300m;
Sendfile on;
Tcp_nopush on;
Keepalive_timeout 60;
Tcp_nodelay on;
Server_tokens off;
Client_body_buffer_size 512k;
Fastcgi_cache_path/usr/local/nginx/fastcgi_cache levels=1:2 keys_zone=test:10m inactive=5m;
Fastcgi_connect_timeout 300;
Fastcgi_send_timeout 300;
Fastcgi_read_timeout 300;
Fastcgi_buffer_size 64k;
Fastcgi_buffers 4 64k;
Fastcgi_busy_buffers_size 128k;
Fastcgi_temp_file_write_size 128k;
Fastcgi_cache TEST;
# # #这个开启后感觉效果非常明显, effectively reduce CPU load, improve the processing capacity of PHP ###
Fastcgi_cache_use_stale error timeout Invalid_header http_500;
Fastcgi_intercept_errors on;
Fastcgi_cache_valid 302 1h;
Fastcgi_cache_valid 1d;
Fastcgi_cache_valid any 1m;
Fastcgi_cache_min_uses 1;
gzip on;
Gzip_min_length 1k;
Gzip_buffers 4 16k;
Gzip_http_version 1.1;
Gzip_comp_level 2;
Gzip_vary on;
include/usr/local/nginx/conf.d/*.conf;
SSI on;
Ssi_silent_errors on;
Ssi_types text/shtml;
Upstream Phpbackend {
Server Unix:/dev/shm/php-fpm.sock weight=100 max_fails=10 fail_timeout=30;
Server Unix:/dev/shm/php-fpm2.sock weight=100 max_fails=10 fail_timeout=30;
Server Unix:/dev/shm/php-fpm3.sock weight=100 max_fails=10 fail_timeout=30;
}
server {
Listen 80;
server_name 10.168.41.2 default backlog=204800;
Location/{
root HTML;
Index index.html index.htm;
}
Error_page 502 503 504/50x.html;
Location =/50x.html {
root HTML;
}
Location ~. php$ {
Fastcgi_pass Phpbackend;
Fastcgi_index index.php;
Fastcgi_param Script_filename/usr/local/nginx/html$fastcgi_script_name;
Include Fastcgi_params;
}
}
}

The optimized PHP-FPM configuration is as follows: I opened 3 php-fpm instances, each with 60 processes, with socket connection fastcgi, which can be adjusted according to your server configuration

The code is as follows Copy Code
Listen =/dev/shm/php-fpm.sock
###/dev/shm is the memory file system, in memory must be fast ###
Pm.start_servers = 60
Pm.min_spare_servers = 40
Pm.max_spare_servers = 80
# # #Default Value:min_spare_servers + (max_spare_servers-min_spare_servers)/2###
Pm.max_requests = 204800
Rlimit_files = 102400
Listen.backlog = 102400
# # #队列等待长度, if there are 11:resource temporarily unavailable errors, you can turn this value up a bit, respectively, corresponding to the system kernel Backlog,nginx backlog###

Kernel parameters are optimized as follows

The code is as follows Copy Code
Net.ipv4.tcp_max_tw_buckets = 250000
# # #同时保持TIME_WAIT套接字的最大数量, if this number is exceeded, the time_wait socket will be cleared immediately and print the warning message ###
Net.ipv4.tcp_sack = 1
# # #通过有选择地应答乱序接收到的报文来提高性能, let the sender send only the missing segment, (for WAN Communications) This option should be enabled, but will increase CPU occupancy ###
net.ipv4.tcp_window_scaling = 1
# # #支持更大的TCP窗口 If the TCP window is greater than 65535 (64K), you must set this value to 1###
Net.ipv4.tcp_rmem = 4096 87380 4194304
# # #这个参数定义了TCP接收缓存 Minimum, default, maximum value for TCP receive sliding window ###
Net.ipv4.tcp_wmem = 4096 16384 4194304
# # #这个参数定义了TCP发送缓存 Minimum, default, maximum value for TCP send sliding window ###
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
# # #当网卡接收数据包的速度大于内核处理的速度时, there will be a queue to hold these packets, which represents the maximum value of the queue ###
Net.core.somaxconn = 262144
The backlog upper bound of # # #socket监听 (listen) may cause a link timeout or trigger retransmission in high concurrent requests ###
Net.ipv4.tcp_max_orphans = 3276800
# # #设定系统中最多有多少个TCP套接字不被关联到任何一个用户文件句柄上, more than will print to message###
Net.ipv4.tcp_max_syn_backlog = 262144
# # #这个参数表示TCP三次握手建立阶段接收SYN请求队列的最大长度, the default is 1024, it can be set to a large number of nginx busy too late accept new connection, Linux does not lose the client-initiated connection request ###
Net.ipv4.tcp_timestamps = 0
# # #开启对于TCP时间戳的支持, its timestamp is increased by 12 bytes (refer to the RFC 1323) TCP Header ###
Net.ipv4.tcp_synack_retries = 1
# # #SYN-ack handshake State Retry count, defaults to 5, Syn-flood attack to 1 or 2###
Net.ipv4.tcp_syn_retries = 1
# # #外向syn握手重试次数, defaults to 4###
Net.ipv4.tcp_tw_recycle = 1
# # #表示开启TCP连接中TIME-wait sockets fast recycle, defaults to 0, means close ###
Net.ipv4.tcp_tw_reuse = 1
# # #表示开启重用. Allows time-wait sockets to be re used for a new TCP connection, which defaults to 0, which means shutdown ###
Net.ipv4.tcp_mem = 94500000 915000000 927000000
# # #三个文本字段, more than the third field TCP rejects the socket, which is in memory page ###
Net.ipv4.tcp_fin_timeout = 1
# # #这个参数表示当服务器主动关闭连接时 The maximum time that the socket remains in the fin-wait-2 state ###
Net.ipv4.tcp_keepalive_time = 30
# # #当keepalive启用时, TCP sends keepalive message frequency, the default is 2 hours, if it set smaller, you can quickly clean up the invalid connection ###
Net.ipv4.ip_local_port_range = 1024 65000
# # #这个参数定义了在UDP和TCP连接中本地 (excluding remote connections) port value range ###
net.netfilter.nf_conntrack_tcp_timeout_established = 36000
# # #established的超时时间 ###
Net.nf_conntrack_max = 655360
# # #这个值决定了你作为网关的工作能力上限, default 65536###
net.ipv4.tcp_syncookies = 0
# # #关闭SYN Cookies, default off ###

Increase the number of Linux kernel open files

The code is as follows Copy Code
Echo ' ULIMIT-HSN 102400′>>/etc/profile
Echo ' ULIMIT-HSN 102400′>>/etc/rc.local
Source/etc/profile

Use AB Stress Test (page is a plain PHP page, no database is read)

Turn on cache effect: 1W3 's ops,cpu load is 20%

Turn on cache effect: 1W3 's ops,cpu load is 20%
Php-fpmtesting1
Effect of turning off caching: OPS is: 9700,CPU load is 100%
Php-fpmtesting2
Php-fpmtesting3

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.