PHPStudy运行TP6
PHPStudy
下载PHPStudy并安装,官网
运行Thinkphp6项目
项目拷贝到 PHPStudy 的网站目录
D:\phpstudy_pro\WWW\tp6demo
目录结构
配置虚拟主机(域名绑定)
打开 PHPStudy 面板 → 站点域名 → 添加网站
配置示例如下:
• 域名:tp6.local
• 根目录:C:\phpstudy_pro\WWW\tp6demo\public
• 端口:80
• PHP版本:选择支持的,例如 7.4、8.0
⚠️ 根目录 必须 指向 public 文件夹,这是 ThinkPHP 6 的入口!!
然后点击“保存并生成配置”。
Windows 用户可通过 C:\Windows\System32\drivers\etc\hosts 添加:127.0.0.1 tp6.local
然后浏览器访问 http://tp6.local。
安装依赖
如果项目缺少 vendor 目录,说明你没装 Composer 依赖。
打开终端,进入项目根目录(不是 public!)
cd D:\phpstudy_pro\WWW\tp6demo
composer install
如果 Composer 没装,需要安装下:https://getcomposer.org/
检查 .env、数据库配置等
确认:
• .env 文件是否存在
• config/database.php 中的数据库连接信息是否正确
• 数据库是否已经导入
如果报错 Database connect error,说明数据库没连上
开启伪静态(路由生效关键)
Nginx 环境:
在 PHPStudy 的 Nginx 配置里添加 location 配置:
location / {
index index.php index.html;
if (!-e $request_filename) {
rewrite ^/(.*)$ /index.php/$1 last;
}
}
查看网站的conf配置文件
打开phpstudy的Nginx1目录,D:\phpstudy_pro\Extensions\Nginx1.15.11\conf\vhosts,找到对应网站的conf配置文件,可以直接修改(注意重启会被覆盖)。
server {
# 定义一个虚拟主机 server 块,相当于一套“接待规则”
listen 83;
# 监听端口号 83(默认是 80,这里改成 83,访问时要写 http://ip:83)
server_name 127.0.0.1 192.168.10.195;
# 该 server 的域名 / 主机名匹配规则,这里直接写了 IP
# 表示访问 http://127.0.0.1:83 或 http://192.168.10.195:83 时生效
root "G:/AutumnTP/public";
# 网站根目录,Nginx 会把请求路径拼接到这里去找实际文件
# 例如请求 /css/a.css 实际访问 G:/BE/i-datas/public/css/a.css
location / {
# 匹配网站根路径(所有请求都会先经过这里)
index index.html index.htm index.php;
# 目录请求时的默认首页文件查找顺序
if (!-e $request_filename) {
# 如果请求对应的文件在磁盘上不存在
# ($request_filename = root + URI,对应完整路径)
rewrite ^(.*)$ /index.php?s=/$1 last;
# 内部重写,把请求转交给 index.php
# 把原始路径作为 s 参数传给 index.php
# 例如请求 /foo/bar => /index.php?s=/foo/bar
break;
# 停止继续执行后续 rewrite 规则
# 这里其实和 "last" 有点冲突,一般可省略
}
}
location /index.php {
# 专门处理 /index.php 的请求(包含 rewrite 过来的)
fastcgi_pass 127.0.0.1:9000;
# 把请求交给 PHP-FPM 处理,监听在 127.0.0.1:9000
fastcgi_index index.php;
# 当请求目录时,默认执行的 PHP 文件是 index.php
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
# 拆分 URI,把 index.php 后面的部分分离成 PATH_INFO
# 例如 /index.php/foo/bar
# -> $fastcgi_script_name = /index.php
# -> $fastcgi_path_info = /foo/bar
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
# 指定要执行的脚本文件的绝对路径
# $document_root = root 定义的路径
# $fastcgi_script_name = 拆出来的脚本名
fastcgi_param PATH_INFO $fastcgi_path_info;
# 传给 PHP 的 PATH_INFO,例如 /foo/bar
# PHP 中可以通过 $_SERVER['PATH_INFO'] 访问
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
# 把 PATH_INFO 转换成物理路径
# 一般不常用,留给一些旧框架
include fastcgi_params;
# 引入标准的 FastCGI 参数(请求方法、URI、query_string 等)
}
}
如果这篇文章对你有用,可以关注本人微信公众号获取更多ヽ(^ω^)ノ ~
