插件窝 干货文章 如何优化Linux LNMP性能

如何优化Linux LNMP性能

优化 配置 timeout cache 358    来源:    2025-05-09

Linux LNMP性能优化指南

LNMP (Linux + Nginx + MySQL + PHP) 是常见的Web服务架构,以下是一套全面的性能优化方案:

1. 系统级优化

内核参数调优

# /etc/sysctl.conf 添加或修改以下参数
net.ipv4.tcp_max_tw_buckets = 6000
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_max_syn_backlog = 1024
net.ipv4.tcp_synack_retries = 2
net.ipv4.ip_local_port_range = 1024 65000
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_keepalive_time = 1200
net.ipv4.tcp_mem = 94500000 915000000 927000000
net.ipv4.tcp_rmem = 4096 87380 4194304
net.ipv4.tcp_wmem = 4096 16384 4194304
net.core.somaxconn = 65535
vm.swappiness = 10

执行 sysctl -p 使配置生效

文件描述符限制

# /etc/security/limits.conf
* soft nofile 65535
* hard nofile 65535

磁盘I/O优化

  • 使用SSD硬盘
  • 挂载选项添加 noatime,nodiratime
  • 考虑使用XFS或EXT4文件系统

2. Nginx优化

配置文件优化

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;
    client_header_timeout 10;
    client_body_timeout 10;
    reset_timedout_connection on;
    send_timeout 10;

    # 开启Gzip压缩
    gzip on;
    gzip_min_length 1k;
    gzip_comp_level 2;
    gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript;

    # 静态文件缓存
    open_file_cache max=65535 inactive=20s;
    open_file_cache_valid 30s;
    open_file_cache_min_uses 1;
    open_file_cache_errors on;
}

3. MySQL优化

my.cnf配置优化 (以8GB内存服务器为例)

[mysqld]
innodb_buffer_pool_size = 4G
innodb_log_file_size = 256M
innodb_flush_log_at_trx_commit = 2
innodb_flush_method = O_DIRECT
innodb_thread_concurrency = 8
innodb_read_io_threads = 8
innodb_write_io_threads = 8
key_buffer_size = 256M
max_connections = 300
query_cache_type = 1
query_cache_size = 128M
query_cache_limit = 4M
tmp_table_size = 256M
max_heap_table_size = 256M
table_open_cache = 2000
thread_cache_size = 100
wait_timeout = 300
interactive_timeout = 300
slow_query_log = 1
long_query_time = 2

定期维护

-- 定期执行
OPTIMIZE TABLE 表名;
ANALYZE TABLE 表名;

4. PHP优化

php-fpm配置

pm = dynamic
pm.max_children = 100
pm.start_servers = 20
pm.min_spare_servers = 10
pm.max_spare_servers = 30
pm.max_requests = 500

request_terminate_timeout = 30s
request_slowlog_timeout = 5s

; 开启OPcache
opcache.enable=1
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
opcache.revalidate_freq=60
opcache.fast_shutdown=1
opcache.enable_cli=1

5. 缓存优化

使用Redis缓存

// 在PHP应用中集成Redis
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);

Nginx缓存静态内容

location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
    expires 30d;
    access_log off;
    add_header Cache-Control "public";
}

6. 监控与维护

安装监控工具

# 安装htop, nmon等监控工具
apt-get install htop nmon sysstat

定期日志轮转

配置logrotate定期清理日志

7. 安全优化

  • 定期更新所有组件到最新稳定版
  • 配置防火墙规则
  • 禁用不必要的服务和端口
  • 使用fail2ban防止暴力破解

总结

优化是一个持续的过程,建议: 1. 每次只修改一个配置项 2. 修改前后进行性能测试对比 3. 监控系统资源使用情况 4. 根据实际负载情况调整参数

以上配置需要根据您的服务器硬件配置和实际业务需求进行调整。