插件窝 干货文章 最佳实践:CentOS搭建web服务器的性能调优指南

最佳实践:CentOS搭建web服务器的性能调优指南

优化 启用 65535 OPcache 1040    来源:    2025-04-13

CentOS搭建Web服务器性能调优指南

1. 系统基础优化

1.1 内核参数调优

# 编辑/etc/sysctl.conf添加以下参数
vm.swappiness = 10
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_max_syn_backlog = 8192
net.ipv4.tcp_max_tw_buckets = 5000
net.ipv4.ip_local_port_range = 1024 65535
net.core.somaxconn = 65535
net.core.netdev_max_backlog = 8192
fs.file-max = 65535

# 应用配置
sysctl -p

1.2 文件描述符限制

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

1.3 禁用不必要的服务

systemctl disable postfix
systemctl disable avahi-daemon
systemctl disable cups

2. Web服务器选择与优化

2.1 Nginx优化配置

# /etc/nginx/nginx.conf
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 4;
    gzip_types text/plain text/css application/json application/javascript text/xml;

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

2.2 Apache优化配置

# /etc/httpd/conf/httpd.conf
StartServers 4
MinSpareServers 4
MaxSpareServers 16
ServerLimit 256
MaxClients 256
MaxRequestsPerChild 4000

# 启用KeepAlive
KeepAlive On
KeepAliveTimeout 5
MaxKeepAliveRequests 100

# 启用压缩
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css application/javascript

3. 数据库优化

3.1 MySQL/MariaDB优化

# /etc/my.cnf.d/server.cnf
[mysqld]
innodb_buffer_pool_size = 4G # 总内存的50-70%
innodb_log_file_size = 256M
innodb_flush_log_at_trx_commit = 2
innodb_flush_method = O_DIRECT
innodb_file_per_table = 1
innodb_thread_concurrency = 0
query_cache_size = 0 # 对于高并发写入环境建议禁用
max_connections = 500
table_open_cache = 4000

4. PHP优化(如使用)

4.1 PHP-FPM配置

# /etc/php-fpm.d/www.conf
pm = dynamic
pm.max_children = 50
pm.start_servers = 10
pm.min_spare_servers = 5
pm.max_spare_servers = 15
pm.max_requests = 500

# 启用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

5. 安全与监控

5.1 防火墙配置

firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --reload

5.2 安装监控工具

# 安装sysstat用于系统监控
yum install sysstat -y
systemctl enable --now sysstat

# 使用工具
sar -u 1 3 # CPU使用率
sar -r 1 3 # 内存使用
sar -b 1 3 # I/O统计

6. 高级优化技巧

6.1 使用HTTP/2

# 在Nginx中启用HTTP/2
listen 443 ssl http2;

6.2 启用Brotli压缩

# 安装Brotli模块
yum install brotli -y

6.3 使用CDN加速

  • 配置Cloudflare或其他CDN服务
  • 启用边缘缓存和智能路由

7. 性能测试工具

7.1 安装测试工具

yum install httpd-tools -y  # ab工具
yum install siege -y

7.2 基本测试命令

ab -n 1000 -c 100 http://yourserver.com/
siege -c 100 -t 1M http://yourserver.com/

通过以上优化措施,您的CentOS Web服务器应该能够显著提升性能。请根据实际负载情况调整参数,并定期监控系统性能以进行进一步优化。