nginx优化
nginx.conf
#user nobody;
worker_processes 4;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
use epoll;
worker_connections 65535;
multi_accept on;
}
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 logs/access.log main;
sendfile on;
tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
tcp_nodelay on;
client_header_buffer_size 4k;
open_file_cache max=1024 inactive=20s;
open_file_cache_valid 30s;
open_file_cache_min_uses 1;
client_header_timeout 15;
client_body_timeout 15;
reset_timedout_connection on;
send_timeout 15;
server_tokens off;
client_max_body_size 10m;
#gzip on;
#fastcgi
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_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 6;
gzip_types text/plain application/javascript application/x-javascript text/javascript text/xml text/css;
gzip_disable "MSIE [1-6]\.";
gzip_vary on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
include vhost/*.conf;
}
php-fpm优化
一般是/usr/local/php/etc/php-fpm.conf
php7.0.1是修改
/usr/local/php/etc/php-fpm.d/www.conf
查看当前php-fpm总进程数,命令:
ps -ylC php-fpm --sort:rss
ps -fe |grep "php-fpm"|grep "pool"|wc -l
其中RSS就是占用的内存情况。
查看当前php-fpm进程的内存占用情况及启动时间,命令如下:
ps -e -o 'pid,comm,args,pcpu,rsz,vsz,stime,user,uid'|grep www|sort -nrk5
从下图可以看出当前php-fpm所有进程平均每个进程占用了60-70MB的内存,启动时间,是当天的话就是3:12,否则会显示是X月X日。
查看当前php-fpm进程平均占用内存情况,一般来说一个php-fpm进程占用的内存为30-40MB,本次查询的结果是60MB,显然是多了。命令如下:
ps --no-headers -o "rss,cmd" -C php-fpm | awk '{ sum+=$1 } END { printf ("%d%s\n", sum/NR/1024,"M") }'
php-fpm.conf几个重要的参数说明如下:
pm = dynamic #指定进程管理方式,有3种可供选择:static、dynamic和ondemand。
pm.max_children = 16 #static模式下创建的子进程数或dynamic模式下同一时刻允许最大的php-fpm子进程数量。
pm.start_servers = 10 #动态方式下的起始php-fpm进程数量。
pm.min_spare_servers = 8 #动态方式下服务器空闲时最小php-fpm进程数量。
pm.max_spare_servers = 16 #动态方式下服务器空闲时最大php-fpm进程数量。
pm.max_requests = 2000 #php-fpm子进程能处理的最大请求数。
pm.process_idle_timeout = 10s
request_terminate_timeout = 120
pm三种进程管理模式说明如下:
pm = static,始终保持一个固定数量的子进程,这个数由pm.max_children定义,这种方式很不灵活,也通常不是默认的。
pm = dynamic,启动时会产生固定数量的子进程(由pm.start_servers控制)可以理解成最小子进程数,而最大子进程数则由pm.max_children去控制,
子进程数会在最大和最小数范围中变化。闲置的子进程数还可以由另2个配置控制,分别是pm.min_spare_servers和pm.max_spare_servers。
如果闲置的子进程超出了pm.max_spare_servers,则会被杀掉。小于pm.min_spare_servers则会启动进程(注意,pm.max_spare_servers应小于pm.max_children)。
pm = ondemand,这种模式和pm = dynamic相反,把内存放在第一位,每个闲置进程在持续闲置了pm.process_idle_timeout秒后就会被杀掉,
如果服务器长时间没有请求,就只会有一个php-fpm主进程。弊端是遇到高峰期或者如果pm.process_idle_timeout的值太短的话,
容易出现504 Gateway Time-out错误,因此pm = dynamic和pm = ondemand谁更适合视实际情况而定。
三、解决php-fpm进程占用内存大问题
3.1调整管理模式
static管理模式适合比较大内存的服务器,而dynamic则适合小内存的服务器,你可以设置一个pm.min_spare_servers和pm.max_spare_servers合理范围,这样进程数会不断变动。
ondemand模式则更加适合微小内存,例如512MB或者256MB内存,以及对可用性要求不高的环境。
3.2减少php-fpm进程数
如果你的VPS主机的内存被占用耗尽,可以检查一下你的php-fpm进程数,按照php-fpm进程数=内存/2/30来计算,1GB内存适合的php-fpm进程数为10-20之间,具体还得根据你的PHP加载的附加组件有关系。
3.3php-fpm配置示例
这里以1GB内存的VPS配置php-fpm为演示,实际操作来看设置数值还得根据服务器本身的性能、PHP等综合考虑。
pm = dynamic #dynamic和ondemand适合小内存。
pm.max_children = 15 #static模式下生效,dynamic不生效。
pm.start_servers = 8 #dynamic模式下开机的进程数量。
pm.min_spare_servers = 6 #dynamic模式下最小php-fpm进程数量。
pm.max_spare_servers = 15 #dynamic模式下最大php-fpm进程数量。
四、解决php-fpm进程不释放内存问题
上面通过减少php-fpm进程总数来达到减少php-fpm内存占用的问题,实际使用过程中发现php-fpm进程还存长期占用内存而不释放的问题。解决的方法就是减少pm.max_requests数。
最大请求数max_requests,即当一个 PHP-CGI 进程处理的请求数累积到 max_requests 个后,自动重启该进程,这样达到了释放内存的目的了。
以1GB内存的VPS主机设置为例(如果你设置的数值没有达到释放内存可以继续调低):
pm.max_requests = 500
当php-fpm进程达到了pm.max_requests设定的数值后,就会重启该进程,从而释放内存。
五、总结
对于大内存以及对并发和可用性要求的话,建议使用static管理模式+最大的pm.max_children。
如果是小内存的服务器,建议使用dynamic或者ondemand模式,同时降低pm.start_servers和pm.max_spare_servers进程数。
为什么我调整了参数没有达到应有的效果?根据wzfou.com的经验,php-fpm配置文件参数不能一概而论,必须要结合服务器自身的性能、WEB动态内容以及对可用性的要求来进行调整,
内存长期占用最好是再检查一下是否有内存泄露。
数据库优化
/etc/my.cnf
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
# *** DO NOT EDIT THIS FILE. It's a template which will be copied to the
# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of MySQL.
[mysqld]
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
server_id = 1
socket = /tmp/mysql.sock
log-error = /usr/local/mysql/data/mysqld.err
slow_query_log = 1
slow_query_log_file=/usr/local/mysql/data/slow-query.log
long_query_time = 1
log-queries-not-using-indexes
max_connections = 1024
back_log = 128
wait_timeout = 60
interactive_timeout = 7200
key_buffer_size = 256M
query_cache_size = 256M
query_cache_type = 1
query_cache_limit = 50M
max_connect_errors = 20
sort_buffer_size = 2M
max_allowed_packet = 32M
join_buffer_size = 2M
thread_cache_size = 200
innodb_buffer_pool_size = 2048M
innodb_flush_log_at_trx_commit = 1
innodb_log_buffer_size = 32M
innodb_log_file_size = 128M
innodb_log_files_in_group = 3
log-bin=/usr/local/mysql/data/mysqlbin
binlog_cache_size = 2M
max_binlog_cache_size = 8M
max_binlog_size = 512M
expire_logs_days = 7
read_buffer_size = 1M
read_rnd_buffer_size = 16M
bulk_insert_buffer_size = 64M
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
# These are commonly set, remove the # and set as required.
# basedir = .....
# datadir = .....
# port = .....
# server_id = .....
# socket = .....
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
网友评论