Files
chat/deploy/phpstudy.md
T
2026-07-14 10:00:34 +08:00

108 lines
2.9 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# phpstudy 部署说明
## 1. 网站根目录
必须指向:
```
D:\web\chat\backend\public
```
**不能**指到 `backend/` 或项目根目录。
## 2. 编译前端
```powershell
cd D:\web\chat
npm run build
```
## 3. 伪静态 / 重写规则
### 若使用 Apachephpstudy 默认)
项目已自带 `backend/public/.htaccess`,确保 phpstudy 中:
- 网站 → 设置 → **Apache** → 开启 `mod_rewrite`
- 允许 `.htaccess` 覆盖(AllowOverride All
### 若使用 Nginx
phpstudy → 网站 → 设置 → **配置文件**,参考 `deploy/nginx.conf.example`
**关键配置**(缺少会导致登录 404):
```nginx
location ^~ /api {
rewrite ^ /index.php?s=$uri last;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
```
注意:`location /``try_files ... /index.html` 不能拦截 `/api`,所以 `/api` 必须写在前面并使用 `^~` 前缀匹配。
## 4. 验证 API 是否正常
浏览器或 curl 测试:
```
POST http://你的域名/api/auth/login
Content-Type: application/json
{"account":"admin","password":"admin123"}
```
应返回 JSON`code: 0` 表示成功),**不应**是 nginx 404 页面。
## 5. 访问地址
| 功能 | 地址 |
|------|------|
| 会员端 | `http://域名/` |
| 管理后台 | `http://域名/admin/` |
| API | `http://域名/api/...` |
## 6. 常见问题
| 现象 | 原因 | 处理 |
|------|------|------|
| 打开首页是 ThinkPHP 欢迎页 | 默认走了 index.php | 设置 `index index.html index.php`;勿用 ThinkPHP 全站伪静态;已内置兜底会输出 Vue 页面 |
| 登录 404 nginx | `/api` 未转发到 index.php | 按上文添加 Nginx/Apache 重写 |
| 页面空白 | 根目录指错或未编译 | 改为 `backend/public` 并执行 `npm run build` |
| 管理后台 404 | 未编译或未部署 admin | 执行 `npm run build` |
| 上传失败 | PHP 限制 / 目录权限 | php.ini 调大 upload_max_filesize`chmod -R 775 backend/uploads` |
### Linux 上传图片 500 错误
在服务器上执行:
```bash
# 1. 创建并授权 uploads 目录(PHP 运行用户通常是 www-data 或 nginx
mkdir -p /path/to/chat/backend/uploads
chmod -R 775 /path/to/chat/backend/uploads
chown -R www-data:www-data /path/to/chat/backend/uploads
# 2. 确认 PHP 扩展已安装
php -m | grep fileinfo
# 3. 确认 php.ini 上传限制
php -i | grep -E 'upload_max_filesize|post_max_size'
```
常见原因:
- `uploads/` 目录不存在或 PHP 进程无写权限
- Linux 上 MIME 识别为 `application/octet-stream`(代码已做扩展名兜底)
- 未安装 `fileinfo` 扩展
### phpstudy 伪静态注意
**不要**选用「ThinkPHP」默认模板(会把所有请求转发到 index.php)。
请使用 `backend/public/nginx.htaccess` 中的规则,或完整配置见 `deploy/nginx.conf.example`