部署ThinkPHP API需确保PHP≥7.2、安装必要扩展及Composer;上传项目后执行composer install,配置.env与runtime权限;Nginx指向public目录并设置重写规则:if (!-e $request_filename) { rewrite ^(.*)$ /index.php?s=$1 last; };重启Nginx并确认PHP-FPM运行,访问接口返回JSON即成功。
将 PHP 项目部署到服务器并运行 ThinkPHP API 接口,关键在于环境配置、项目上传、路径设置和 Web 服务器(如 Nginx 或 Apache)的正确解析。以下是 ThinkPHP API 项目的完整部署与运行配置方法。
ThinkPHP 6(常见版本)对环境有明确要求,部署前需确保服务器满足条件:
可通过命令检查 PHP 环境:
php -v将本地开发完成的 ThinkPHP API 项目上传至服务器指定目录,例如:/www/wwwroot/api.example.com
示例 .env 配置:
APP_DEBUG = falseThinkPHP 使用路由重写功能,必须配置 URL 重写规则,确保访问 /api/user 能正确路由到入口文件。
编辑 Nginx 站点配置文件(通常位于 /etc/nginx/sites-available/ 或宝塔面板中修改):
server {
listen 80;
server_name api.example.com;
root /www/wwwroot/api.example.com/public;
index index.php index.html;
location / {
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php?s=$1 last;
break;
}
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
保存后重启 Nginx:
nginx -t # 检查语法完成部署后,通过浏览器或 Postman 访问 API 接口,例如:
http://api.example.com/api/user/list若返回正常 JSON 数据,说明部署成功。若出现 404 或空白页,请检查:
关闭调试模式避免信息泄露:
在 .env 中设置 APP_DEBUG = false基本上就这些。只要环境正确、路径清晰、重写规则配置到位,ThinkPHP API 项目就能稳定运行。