# 编辑/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
# 编辑/etc/security/limits.conf
* soft nofile 65535
* hard nofile 65535
* soft nproc 65535
* hard nproc 65535
systemctl disable postfix
systemctl disable avahi-daemon
systemctl disable cups
# /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;
}
# /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
# /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
# /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
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --reload
# 安装sysstat用于系统监控
yum install sysstat -y
systemctl enable --now sysstat
# 使用工具
sar -u 1 3 # CPU使用率
sar -r 1 3 # 内存使用
sar -b 1 3 # I/O统计
# 在Nginx中启用HTTP/2
listen 443 ssl http2;
# 安装Brotli模块
yum install brotli -y
yum install httpd-tools -y # ab工具
yum install siege -y
ab -n 1000 -c 100 http://yourserver.com/
siege -c 100 -t 1M http://yourserver.com/
通过以上优化措施,您的CentOS Web服务器应该能够显著提升性能。请根据实际负载情况调整参数,并定期监控系统性能以进行进一步优化。