# 编辑sysctl.conf
sudo nano /etc/sysctl.conf
# 添加或修改以下参数
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_max_syn_backlog = 8192
net.core.somaxconn = 65535
net.core.netdev_max_backlog = 16384
vm.swappiness = 10
vm.vfs_cache_pressure = 50
# 使配置生效
sudo sysctl -p
noatime,nodiratime,data=writeback
# 示例/etc/fstab条目
/dev/sda1 / ext4 defaults,noatime,nodiratime,data=writeback 0 1
# 编辑limits.conf
sudo nano /etc/security/limits.conf
# 添加以下内容
* soft nofile 65535
* hard nofile 65535
* soft nproc 65535
* hard nproc 65535
# 对于高流量站点推荐使用event MPM
<IfModule mpm_event_module>
StartServers 2
MinSpareThreads 25
MaxSpareThreads 75
ThreadLimit 64
ThreadsPerChild 25
MaxRequestWorkers 150
MaxConnectionsPerChild 10000
</IfModule>
# 禁用不需要的模块
HostnameLookups Off
Timeout 30
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5
# 启用压缩
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/javascript
</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"
ExpiresByType application/x-javascript "access plus 1 month"
ExpiresDefault "access plus 2 days"
</IfModule>
[mysqld]
# 内存相关
innodb_buffer_pool_size = 4G # 建议为系统内存的50-70%
innodb_buffer_pool_instances = 4
innodb_log_file_size = 512M
innodb_log_buffer_size = 16M
key_buffer_size = 256M
query_cache_size = 0 # MySQL 8.0已移除查询缓存
# 连接相关
max_connections = 200
thread_cache_size = 50
table_open_cache = 4000
# InnoDB性能
innodb_flush_log_at_trx_commit = 2 # 对数据安全性要求高时设为1
innodb_flush_method = O_DIRECT
innodb_file_per_table = 1
innodb_thread_concurrency = 0
innodb_read_io_threads = 8
innodb_write_io_threads = 8
# 其他优化
tmp_table_size = 64M
max_heap_table_size = 64M
# 定期优化表
mysqlcheck -o -A -u root -p
# 监控慢查询
# 在my.cnf中设置
slow_query_log = 1
slow_query_log_file = /var/log/mysql/mysql-slow.log
long_query_time = 2
; 内存限制
memory_limit = 128M
; 执行时间
max_execution_time = 30
; 上传限制
upload_max_filesize = 16M
post_max_size = 20M
; 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
; 其他优化
realpath_cache_size=256k
realpath_cache_ttl=3600
[www]
pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 10
pm.max_requests = 500
# Nginx示例配置
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m inactive=60m;
server {
location / {
proxy_cache my_cache;
proxy_pass http://backend;
proxy_cache_valid 200 302 10m;
proxy_cache_valid 404 1m;
}
}
// PHP中使用Redis示例
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
$redis->set('key', 'value');
echo $redis->get('key');
系统监控:
Apache监控:
apachectl status
MySQL监控:
mysqladmin -u root -p extended-status
# 或使用mytop
PHP监控:
通过以上优化措施,可以显著提升LAMP环境的性能和稳定性。建议根据实际服务器配置和应用需求调整参数,并通过监控工具持续观察性能变化。