安装宝塔面板免费版后,针对Nginx和PHP做一些优化 - 技术宅银魂 - 科技改变生活 - 万事屋

安装宝塔面板免费版后,针对Nginx和PHP做一些优化

目前使用的是阿里云99计划中的2核4G内存服务器,阿里云99计划:点击查看详情

宝塔面板免费版固然很好用,但毕竟不是人,低配高配服务器都同样设置,导致很多低配服务器吃不消容易嘎了,不得不手动调整些参数以适应当下流量环境。

Nginx相关设置:

# 在http模块内添加
http {
    # ... existing code ...
    
    # 基础优化
    worker_processes 2; # 与CPU核心数一致
    worker_rlimit_nofile 65535; # 打开文件数限制
    
    events {
        worker_connections 1024; # 每个进程处理连接数
        use epoll; # 使用高效事件模型
        multi_accept on; # 同时接受多个连接
    }

    # 传输优化
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    keepalive_requests 1000;
    
    # 缓冲区优化
    client_header_buffer_size 4k;
    client_max_body_size 50m;
    large_client_header_buffers 4 16k;
    
    # Gzip压缩
    gzip on;
    gzip_min_length 1k;
    gzip_proxied any;
    gzip_comp_level 6;
    gzip_types text/plain application/javascript application/x-javascript text/javascript text/css application/xml application/json image/jpeg image/gif image/png font/ttf font/otf image/svg+xml application/xml+rss text/x-js;
    
    # 静态文件缓存
    open_file_cache max=2000 inactive=20s;
    open_file_cache_valid 30s;
    open_file_cache_min_uses 2;
    open_file_cache_errors off;
}

网站配置文件:

location ~* \.(jpg|jpeg|png|gif|webp|ico|css|js)$ {
    expires 30d;
    access_log off;
}

PHP相关设置:

; 基础优化
max_execution_time = 30
memory_limit = 128M
post_max_size = 50M
upload_max_filesize = 50M

; OPcache配置
opcache.enable=1
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=10000
opcache.revalidate_freq=60
opcache.save_comments=1
opcache.enable_cli=1

; APCu配置
apc.enabled=1
apc.shm_size=128M
apc.ttl=3600
apc.enable_cli=1
apc.slam_defense=0

 

PHP-FPM设置:

[global]
pid = run/php-fpm.pid
error_log = log/php-fpm.log
log_level = warning

[www]
listen = /tmp/php-cgi-81.sock
listen.backlog = 2048
pm = ondemand # 内存优化模式
pm.max_children = 30 # 根据内存调整:(4G - 1G)/50MB ≈ 60,保守设置30
pm.start_servers = 4
pm.min_spare_servers = 4
pm.max_spare_servers = 8
pm.process_idle_timeout = 10s
pm.max_requests = 1000 # 防止内存泄漏
request_terminate_timeout = 30 # 防止进程僵死
rlimit_files = 65535
catch_workers_output = yes

 

    没有回复内容

万事屋新帖