文章目录
-
- 在宝塔面板中,点击"软件商店"→"PHP"→"设置"→"配置修改": ; 最大执行时间 max_execution_time = 300 ; 最大内存限制 memory_limit = 256M ; POST最大大小 post_max_size = 100M ; 上传文件最大大小 upload_max_filesize = 50M ; 时区设置 date.timezone = Asia/Shanghai
- 在"性能调整"标签页: 参数 默认值 推荐值 pm.max_children 10 50-100 pm.start_servers 2 10-20 pm.min_spare_servers 1 10-20 pm.max_spare_servers 3 30-50 pm.max_requests 500 1000
- 启用OPcache提升PHP性能: ; php.ini中添加 zend_extension=opcache.so opcache.enable=1 opcache.memory_consumption=256 opcache.interned_strings_buffer=16 opcache.max_accelerated_files=10000 opcache.revalidate_freq=2 opcache.fast_shutdown=1
-
- 在宝塔"数据库"→"配置"中修改: [mysqld] innodb_buffer_pool_size = 1G innodb_log_file_size = 256M max_connections = 200 tmp_table_size = 64M max_heap_table_size = 64M sort_buffer_size = 4M read_buffer_size = 2M read_rnd_buffer_size = 8M slow_query_log = 1 slow_query_log_file = /www/server/data/slow.log long_query_time = 2
- 定期执行: -- 优化所有表 OPTIMIZE TABLE table_name; -- 分析表以更新统计 ANALYZE TABLE table_name; -- 检查表 CHECK TABLE table_name;
-
- 在nginx.conf中添加: gzip on; gzip_vary on; 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/rss+xml font/truetype font/opentype application/vnd.ms-fontobject image/svg+xml; gzip_min_length 1000;
- location ~* \.(jpg|jpeg|png|gif|ico|css|js|woff|woff2|ttf|svg)$ { expires 30d; add_header Cache-Control "public, no-transform"; }
- worker_processes auto; worker_connections 2048; keepalive_timeout 65; client_max_body_size 100M;
- 优化项 优化前 优化后 TTFB 500ms 150ms 内存占用 80% 40% 并发能力 50 200 页面加载 3s 1s 优化完成后,记得在宝塔面板重启PHP和MySQL服务使配置生效。
本文详细介绍宝塔面板环境下PHP和MySQL的性能参数调优,帮助提升网站访问速度。
在宝塔面板中,点击"软件商店"→"PHP"→"设置"→"配置修改":
; 最大执行时间
max_execution_time = 300
; 最大内存限制
memory_limit = 256M
; POST最大大小
post_max_size = 100M
; 上传文件最大大小
upload_max_filesize = 50M
; 时区设置
date.timezone = Asia/Shanghai
在"性能调整"标签页:
| 参数 | 默认值 | 推荐值 |
|---|---|---|
| pm.max_children | 10 | 50-100 |
| pm.start_servers | 2 | 10-20 |
| pm.min_spare_servers | 1 | 10-20 |
| pm.max_spare_servers | 3 | 30-50 |
| pm.max_requests | 500 | 1000 |
启用OPcache提升PHP性能:
; php.ini中添加
zend_extension=opcache.so
opcache.enable=1
opcache.memory_consumption=256
opcache.interned_strings_buffer=16
opcache.max_accelerated_files=10000
opcache.revalidate_freq=2
opcache.fast_shutdown=1
在宝塔"数据库"→"配置"中修改:
[mysqld]
innodb_buffer_pool_size = 1G
innodb_log_file_size = 256M
max_connections = 200
tmp_table_size = 64M
max_heap_table_size = 64M
sort_buffer_size = 4M
read_buffer_size = 2M
read_rnd_buffer_size = 8M
slow_query_log = 1
slow_query_log_file = /www/server/data/slow.log
long_query_time = 2
定期执行:
-- 优化所有表
OPTIMIZE TABLE table_name;
-- 分析表以更新统计
ANALYZE TABLE table_name;
-- 检查表
CHECK TABLE table_name;
在nginx.conf中添加:
gzip on;
gzip_vary on;
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/rss+xml font/truetype font/opentype application/vnd.ms-fontobject image/svg+xml;
gzip_min_length 1000;
location ~* \.(jpg|jpeg|png|gif|ico|css|js|woff|woff2|ttf|svg)$ {
expires 30d;
add_header Cache-Control "public, no-transform";
}
location ~* \.(jpg|jpeg|png|gif|ico|css|js|woff|woff2|ttf|svg)$ {
expires 30d;
add_header Cache-Control "public, no-transform";
}
worker_processes auto;
worker_connections 2048;
keepalive_timeout 65;
client_max_body_size 100M;
worker_processes auto;
worker_connections 2048;
keepalive_timeout 65;
client_max_body_size 100M;
slowlog = /var/log/php-slow.log
request_slowlog_timeout = 5s
slowlog = /var/log/php-slow.log
request_slowlog_timeout = 5s
| 工具 | 用途 |
|---|---|
| Xhprof | PHP性能分析 |
| MySQL Explain | SQL查询分析 |
| Load Impact | 压力测试 |
| GTmetrix | 页面性能评分 |
| 优化项 | 优化前 | 优化后 |
|---|---|---|
| TTFB | 500ms | 150ms |
| 内存占用 | 80% | 40% |
| 并发能力 | 50 | 200 |
| 页面加载 | 3s | 1s |
优化完成后,记得在宝塔面板重启PHP和MySQL服务使配置生效。
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

评论(0)