LNMP(Linux + Nginx + MySQL + PHP)是常见的Web服务架构,下面我将从各个组件层面介绍性能调优方法。
# 编辑/etc/sysctl.conf
# 增加或修改以下参数
net.ipv4.tcp_max_tw_buckets = 6000
net.ipv4.ip_local_port_range = 1024 65000
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_max_syn_backlog = 262144
net.ipv4.tcp_synack_retries = 1
net.ipv4.tcp_syn_retries = 1
net.ipv4.tcp_fin_timeout = 30
net.core.somaxconn = 262144
net.core.netdev_max_backlog = 262144
fs.file-max = 65535
# 使配置生效
sysctl -p
# 编辑/etc/security/limits.conf
* soft nofile 65535
* hard nofile 65535
systemctl disable postfix.service # 示例:关闭邮件服务
worker_processes auto; # 自动设置为CPU核心数
worker_rlimit_nofile 65535;
events {
worker_connections 65535;
use epoll;
multi_accept on;
}
http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
keepalive_requests 100;
types_hash_max_size 2048;
server_tokens off;
# 开启gzip压缩
gzip on;
gzip_min_length 1k;
gzip_comp_level 2;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
# 静态文件缓存
open_file_cache max=100000 inactive=20s;
open_file_cache_valid 30s;
open_file_cache_min_uses 2;
open_file_cache_errors on;
}
server {
# 禁用不必要的日志记录
access_log off; # 或者只记录重要请求
error_log /var/log/nginx/error.log crit;
# 静态资源缓存
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
expires 30d;
access_log off;
}
}
[mysqld]
# 基础配置
innodb_buffer_pool_size = 4G # 建议为物理内存的50-70%
innodb_buffer_pool_instances = 8
innodb_log_file_size = 256M
innodb_flush_log_at_trx_commit = 2 # 对数据安全性要求高可设为1
sync_binlog = 1
innodb_flush_method = O_DIRECT
innodb_file_per_table = 1
innodb_thread_concurrency = 0
innodb_read_io_threads = 8
innodb_write_io_threads = 8
# 查询缓存
query_cache_type = 1
query_cache_size = 64M
query_cache_limit = 2M
# 连接设置
max_connections = 500
thread_cache_size = 100
table_open_cache = 2000
# 临时表
tmp_table_size = 64M
max_heap_table_size = 64M
# 慢查询日志
slow_query_log = 1
slow_query_log_file = /var/log/mysql/mysql-slow.log
long_query_time = 2
log_queries_not_using_indexes = 1
-- 分析表
ANALYZE TABLE table_name;
-- 优化表
OPTIMIZE TABLE table_name;
[global]
pid = /var/run/php-fpm/php-fpm.pid
error_log = /var/log/php-fpm/error.log
log_level = warning
[www]
listen = /var/run/php-fpm/php-fpm.sock
listen.owner = nginx
listen.group = nginx
listen.mode = 0660
user = nginx
group = nginx
pm = dynamic
pm.max_children = 100
pm.start_servers = 20
pm.min_spare_servers = 10
pm.max_spare_servers = 30
pm.max_requests = 500
slowlog = /var/log/php-fpm/$pool-slow.log
request_slowlog_timeout = 5s
php_admin_value[memory_limit] = 128M
php_admin_value[max_execution_time] = 30
memory_limit = 128M
max_execution_time = 30
upload_max_filesize = 16M
post_max_size = 20M
opcache.enable=1
opcache.enable_cli=1
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
opcache.revalidate_freq=60
opcache.fast_shutdown=1
使用缓存:
CDN加速:将静态资源托管到CDN
数据库优化:
代码优化:
监控工具:
定期维护:
通过以上综合优化,可以显著提升LNMP架构的性能和稳定性。建议每次修改配置后都进行测试,确保修改确实带来性能提升。