性能优化指南
服务器性能优化技巧
本指南将帮助您优化VPS性能,提升网站响应速度和用户体验。
系统级优化
内核参数优化
# 编辑系统参数
vim /etc/sysctl.conf
# 网络优化
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.ipv4.tcp_rmem = 4096 65536 16777216
net.ipv4.tcp_wmem = 4096 65536 16777216
net.ipv4.tcp_congestion_control = bbr
# 文件系统优化
fs.file-max = 65536
vm.swappiness = 10
vm.dirty_ratio = 15
vm.dirty_background_ratio = 5
# 应用配置
sysctl -p
文件描述符限制
# 编辑limits配置
vim /etc/security/limits.conf
# 添加以下内容
* soft nofile 65536
* hard nofile 65536
root soft nofile 65536
root hard nofile 65536
# 重启后生效
ulimit -n 65536
磁盘I/O优化
# 查看当前I/O调度器
cat /sys/block/sda/queue/scheduler
# 设置为deadline调度器(适合SSD)
echo deadline > /sys/block/sda/queue/scheduler
# 永久设置
echo 'echo deadline > /sys/block/sda/queue/scheduler' >> /etc/rc.local
Web服务器优化
Apache性能优化
# /etc/apache2/conf-available/performance.conf
# 启用必要模块
LoadModule rewrite_module modules/mod_rewrite.so
LoadModule deflate_module modules/mod_deflate.so
LoadModule expires_module modules/mod_expires.so
LoadModule headers_module modules/mod_headers.so
# 工作模式优化
<IfModule mpm_prefork_module>
StartServers 8
MinSpareServers 5
MaxSpareServers 20
ServerLimit 256
MaxRequestWorkers 256
MaxConnectionsPerChild 10000
</IfModule>
# 启用压缩
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/javascript application/json
DeflateCompressionLevel 6
</IfModule>
# 缓存设置
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access plus 1 year"
ExpiresByType image/jpeg "access plus 1 year"
ExpiresByType image/gif "access plus 1 year"
ExpiresByType image/png "access plus 1 year"
ExpiresByType text/css "access plus 1 month"
ExpiresByType application/pdf "access plus 1 month"
ExpiresByType text/javascript "access plus 1 month"
ExpiresByType application/javascript "access plus 1 month"
</IfModule>
Nginx性能优化
# /etc/nginx/nginx.conf
# 工作进程数(通常等于CPU核心数)
worker_processes auto;
# 每个工作进程的连接数
events {
worker_connections 1024;
use epoll;
multi_accept on;
}
http {
# 基本优化
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# 缓冲区优化
client_body_buffer_size 128k;
client_max_body_size 10m;
client_header_buffer_size 1k;
large_client_header_buffers 4 4k;
output_buffers 1 32k;
postpone_output 1460;
# Gzip压缩
gzip on;
gzip_vary on;
gzip_min_length 1024;
gzip_proxied any;
gzip_comp_level 6;
gzip_types
text/plain
text/css
text/xml
text/javascript
application/json
application/javascript
application/xml+rss
application/atom+xml
image/svg+xml;
# 缓存设置
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
expires 1y;
add_header Cache-Control "public, immutable";
add_header Vary Accept-Encoding;
}
}
数据库优化
MySQL性能优化
# /etc/mysql/mysql.conf.d/mysqld.cnf
[mysqld]
# 基本设置
skip-name-resolve
skip-external-locking
# 内存优化
innodb_buffer_pool_size = 512M # 设置为可用内存的70%
innodb_log_buffer_size = 16M
key_buffer_size = 256M
sort_buffer_size = 2M
read_buffer_size = 2M
read_rnd_buffer_size = 8M
myisam_sort_buffer_size = 64M
# 连接优化
max_connections = 100
thread_cache_size = 16
table_open_cache = 2000
table_definition_cache = 1400
# InnoDB优化
innodb_flush_log_at_trx_commit = 2
innodb_log_file_size = 256M
innodb_flush_method = O_DIRECT
innodb_file_per_table = 1
# 查询缓存
query_cache_type = 1
query_cache_size = 64M
query_cache_limit = 2M
# 慢查询日志
slow_query_log = 1
slow_query_log_file = /var/log/mysql/slow.log
long_query_time = 2
PostgreSQL优化
# /etc/postgresql/14/main/postgresql.conf
# 内存设置
shared_buffers = 256MB
effective_cache_size = 1GB
work_mem = 4MB
maintenance_work_mem = 64MB
# 检查点设置
checkpoint_completion_target = 0.9
wal_buffers = 16MB
default_statistics_target = 100
# 连接设置
max_connections = 100
PHP优化
PHP-FPM配置
# /etc/php/8.1/fpm/pool.d/www.conf
[www]
user = www-data
group = www-data
# 进程管理
pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 500
# 性能优化
request_terminate_timeout = 300
request_slowlog_timeout = 5s
slowlog = /var/log/php-fpm-slow.log
PHP配置优化
# /etc/php/8.1/fpm/php.ini
# 内存限制
memory_limit = 256M
max_execution_time = 300
max_input_time = 300
# 文件上传
upload_max_filesize = 64M
post_max_size = 64M
max_file_uploads = 20
# 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
缓存优化
Redis缓存
# 安装Redis
apt install redis-server
# 配置Redis
vim /etc/redis/redis.conf
# 内存优化
maxmemory 256mb
maxmemory-policy allkeys-lru
# 持久化设置
save 900 1
save 300 10
save 60 10000
Memcached缓存
# 安装Memcached
apt install memcached
# 配置Memcached
vim /etc/memcached.conf
# 内存设置
-m 128
-c 1024
-t 4
网络优化
TCP优化
# /etc/sysctl.conf
# TCP窗口缩放
net.ipv4.tcp_window_scaling = 1
# TCP时间戳
net.ipv4.tcp_timestamps = 1
# TCP SACK
net.ipv4.tcp_sack = 1
# TCP快速回收
net.ipv4.tcp_tw_reuse = 1
# TCP拥塞控制
net.ipv4.tcp_congestion_control = bbr
防火墙优化
# 优化iptables规则
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A INPUT -p tcp --dport 80 -m limit --limit 25/minute --limit-burst 100 -j ACCEPT
监控和分析
性能监控脚本
#!/bin/bash
# performance_monitor.sh
echo "=== System Performance Report ===" > /tmp/perf_report.txt
echo "Date: $(date)" >> /tmp/perf_report.txt
echo "" >> /tmp/perf_report.txt
# CPU使用率
echo "CPU Usage:" >> /tmp/perf_report.txt
top -bn1 | grep "Cpu(s)" >> /tmp/perf_report.txt
echo "" >> /tmp/perf_report.txt
# 内存使用
echo "Memory Usage:" >> /tmp/perf_report.txt
free -h >> /tmp/perf_report.txt
echo "" >> /tmp/perf_report.txt
# 磁盘使用
echo "Disk Usage:" >> /tmp/perf_report.txt
df -h >> /tmp/perf_report.txt
echo "" >> /tmp/perf_report.txt
# 网络连接
echo "Network Connections:" >> /tmp/perf_report.txt
netstat -tuln | wc -l >> /tmp/perf_report.txt
网站性能测试
# 使用ab进行压力测试
ab -n 1000 -c 10 http://your-website.com/
# 使用curl测试响应时间
curl -o /dev/null -s -w "Total time: %{time_total}s\n" http://your-website.com/
优化建议
定期维护
- 清理日志文件:定期清理过大的日志
- 更新系统:保持系统和软件最新
- 监控资源:定期检查CPU、内存、磁盘使用情况
- 优化数据库:定期优化数据库表和索引
硬件升级建议
- CPU:多核心处理器提升并发处理能力
- 内存:增加RAM改善缓存效果
- 存储:使用SSD提升I/O性能
- 网络:升级带宽提升访问速度
应用层优化
- 代码优化:优化算法和数据库查询
- 图片优化:压缩图片,使用WebP格式
- CDN加速:使用内容分发网络
- 静态资源:启用浏览器缓存