如何配置Thinkphp在Nginx和Apache服务器下实现伪静态功能?
- 内容介绍
- 文章标签
- 相关推荐
在把刚写好的 ThinkPHP 项目迁移到线上服务器时往往会遇到一连串令人头疼的问题:先是 PHP 版本过低导致无法解析代码;再有 URL 重写功能被禁用,导致所有访问都报错 404;最终还要在不同的 Web 服务器下完成伪静态配置,手工调试几次才行。下面按痛点拆解,给你一份完整的配置教程。
痛点一这方面,PHP 版本不匹配
很多线上环境默认安装的是 PHP 5.4 或更早版本。而 ThinkPHP 5/6 至少需要 PHP 5.6+。如果你直接使用程序自带的 PHP,往往会出现语法解析错误。
# 检查当前 PHP 版本
php -v
# 若低于 5.6。请升级或安装兼容版本
yum install php-7.4 # CentOS 示例
再看痛点二,URL 重写未开启
没有启用 mod_rewrite或相应的 Nginx 配置,任何以 /index.php?s=... 的方法都会被当作普通文件请求,从而返回 404。
说到痛点三,伪静态规则不统一
不同框架、不同服务器对伪静态规则要求略有差异。下面给出 ThinkPHP 5 与 ThinkPHP 6 在 Apache 和 Nginx 下的标准配置。
ThinkPHP 5
Apache :
# .htaccess 文件内容
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME}!-f
RewriteCond %{REQUEST_FILENAME}!-d
RewriteRule ^$ index.php?s=$1
# nginx.conf 中 server 块示例
server {
listen 80;server_name example.com www.example.com;root /var/www/html/thinkphp5/public;location / {
try_files $uri $uri/ /index.php?其实,$query_string;}
location ~ \.php$ {
fastcgi_pass unix:/var/run/php-fpm.sock;fastcgi_index index.php;fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;include fastcgi_params;}
}
# .htaccess 内容同上,但注意不要使用 s 参数
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME}!-f
RewriteCond %{REQUEST_FILENAME}!话说回来,-d
RewriteRule ^$ index.php/$1
# nginx.conf 中 server 块示例
server {
listen 80 default_server;server_name _;root /var/www/html/thinkphp6/public;其实,location / {
try_files $uri $uri/ /index.php?$query_string;# 如果想让路由更友好,可改为:
# rewrite ^/$ /index.php/$1 last;}
location ~ \.php$ {
include fastcgi_params;fastcgi_pass unix:/var/run/php-fpm.sock;fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;}
}
快速检查与调试技巧
- 确认已重载 Nginx 配置: sudo nginx -t && sudo systemctl reload nginx
- 在 Apache 上执行: apachectl configtest && systemctl reload httpd 或 apachectl -S
- 至于检查日志文件,Nginx 错误日志 /var/log/nginx/error.log;话说回来,Apache 错误日志 /var/log/httpd/error_log 。老实说,查看是否还有 “rewrite” 或 “permission” 的报错。其实,
- 若使用 phpStudy 或 XAMPP。本地模拟时请确保 “Web Service” 已切换为 PHP+Nginx,而且 vhosts 配置中加入上述伪静态规则。
-
如果项目放在子目录,记得在 RewriteRule 前加上前缀。例如:
# 子目录下的 Apache .htaccess 示例: RewriteBase /app/ RewriteRule ^$ index.php/$1
location ~* ^/app { alias /var/www/html/app/public$1;tryfiles $uri $uri/ @approuter;} location @app_router { rewrite ^/app/$ /app/index.php/$1 last;}
& 实践建议
- 先在本地搭建一个与线上环境相同的 LAMP/LEMP 堆栈,再把项目推送到远程服务器进行“灰度”部署。
在把刚写好的 ThinkPHP 项目迁移到线上服务器时往往会遇到一连串令人头疼的问题:先是 PHP 版本过低导致无法解析代码;再有 URL 重写功能被禁用,导致所有访问都报错 404;最终还要在不同的 Web 服务器下完成伪静态配置,手工调试几次才行。下面按痛点拆解,给你一份完整的配置教程。
痛点一这方面,PHP 版本不匹配
很多线上环境默认安装的是 PHP 5.4 或更早版本。而 ThinkPHP 5/6 至少需要 PHP 5.6+。如果你直接使用程序自带的 PHP,往往会出现语法解析错误。
# 检查当前 PHP 版本
php -v
# 若低于 5.6。请升级或安装兼容版本
yum install php-7.4 # CentOS 示例
再看痛点二,URL 重写未开启
没有启用 mod_rewrite或相应的 Nginx 配置,任何以 /index.php?s=... 的方法都会被当作普通文件请求,从而返回 404。
说到痛点三,伪静态规则不统一
不同框架、不同服务器对伪静态规则要求略有差异。下面给出 ThinkPHP 5 与 ThinkPHP 6 在 Apache 和 Nginx 下的标准配置。
ThinkPHP 5
Apache :
# .htaccess 文件内容
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME}!-f
RewriteCond %{REQUEST_FILENAME}!-d
RewriteRule ^$ index.php?s=$1
# nginx.conf 中 server 块示例
server {
listen 80;server_name example.com www.example.com;root /var/www/html/thinkphp5/public;location / {
try_files $uri $uri/ /index.php?其实,$query_string;}
location ~ \.php$ {
fastcgi_pass unix:/var/run/php-fpm.sock;fastcgi_index index.php;fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;include fastcgi_params;}
}
# .htaccess 内容同上,但注意不要使用 s 参数
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME}!-f
RewriteCond %{REQUEST_FILENAME}!话说回来,-d
RewriteRule ^$ index.php/$1
# nginx.conf 中 server 块示例
server {
listen 80 default_server;server_name _;root /var/www/html/thinkphp6/public;其实,location / {
try_files $uri $uri/ /index.php?$query_string;# 如果想让路由更友好,可改为:
# rewrite ^/$ /index.php/$1 last;}
location ~ \.php$ {
include fastcgi_params;fastcgi_pass unix:/var/run/php-fpm.sock;fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;}
}
快速检查与调试技巧
- 确认已重载 Nginx 配置: sudo nginx -t && sudo systemctl reload nginx
- 在 Apache 上执行: apachectl configtest && systemctl reload httpd 或 apachectl -S
- 至于检查日志文件,Nginx 错误日志 /var/log/nginx/error.log;话说回来,Apache 错误日志 /var/log/httpd/error_log 。老实说,查看是否还有 “rewrite” 或 “permission” 的报错。其实,
- 若使用 phpStudy 或 XAMPP。本地模拟时请确保 “Web Service” 已切换为 PHP+Nginx,而且 vhosts 配置中加入上述伪静态规则。
-
如果项目放在子目录,记得在 RewriteRule 前加上前缀。例如:
# 子目录下的 Apache .htaccess 示例: RewriteBase /app/ RewriteRule ^$ index.php/$1
location ~* ^/app { alias /var/www/html/app/public$1;tryfiles $uri $uri/ @approuter;} location @app_router { rewrite ^/app/$ /app/index.php/$1 last;}
& 实践建议
- 先在本地搭建一个与线上环境相同的 LAMP/LEMP 堆栈,再把项目推送到远程服务器进行“灰度”部署。

