要优化 CentOS 上 ThinkPHP 的响应时间,可以从多个方面入手,包括服务器配置、PHP 代码优化、数据库优化、缓存策略等。以下是详细的优化建议:
如果你使用 Nginx 作为 Web 服务器,可以进行以下优化:
http {
# 增加 worker 进程数
worker_processes auto;
worker_connections 1024;
# 启用 Gzip 压缩
gzip on;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
# 缓存静态文件
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
expires 30d;
add_header Cache-Control "public";
}
# 反向代理设置
server {
listen 80;
server_name yourdomain.com;
location / {
root /path/to/your/thinkphp/project;
index index.php index.html index.htm;
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
include fastcgi.conf;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
}
编辑 /etc/php-fpm.d/www.conf 文件,调整以下参数:
pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35
request_terminate_timeout = 30s
config/app.php 中启用。'cache' => [
'type' => 'file',
'path' => '/tmp/cache',
],
eval() 和 assert() 等高开销函数。如果你的应用有大量的静态资源(如图片、CSS、JS 文件),可以考虑使用 CDN 来加速这些资源的加载。
通过以上步骤,你应该能够显著优化 CentOS 上 ThinkPHP 的响应时间。记住,优化是一个持续的过程,需要根据实际情况不断调整和改进。