ThinkPHP默认必须要设置public目录为运行目录才可以成功运行项目,但是如果使用的是虚拟主机无法设置运行目录怎么办?而最稳妥的方法是移动public目录到根目录并修正路径。
先看 TP8 原版 public/index.php
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2019 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
// [ 应用入口文件 ]
namespace think;
require __DIR__ . '/../vendor/autoload.php';
// 执行HTTP应用并响应
$http = (new App())->http;
$response = $http->run();
$response->send();
$http->end($response);
问题:
__DIR__.'/../vendor/autoload.php'
这里的../表示返回上一级,虚拟主机把入口放到根目录后,不能用../了。
TP8虚拟主机部署:
第1步:移动文件
把项目里的
public/index.php
public/.htaccess
public/static
全部移动到虚拟主机根目录(wwwroot)
移动后目录结构变成:
wwwroot/
├── index.php (原来 public 里的)
├── .htaccess (原来 public 里的)
├── static/ (原来 public 里的)
├── app/
├── vendor/
├── config/
├── route/
└── ...
第2步:修改根目录的index.php
把刚才的代码,只改一行:
把
require __DIR__ . '/../vendor/autoload.php';
改成
require __DIR__ . '/vendor/autoload.php';
修改后的TP8根目录index.php
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2019 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
// [ 应用入口文件 ]
namespace think;
require __DIR__ . '/vendor/autoload.php';
// 执行HTTP应用并响应
$http = (new App())->http;
$response = $http->run();
$response->send();
$http->end($response);
就改了这一句:
/../vendor/→/vendor/
第3步:根目录放.htaccess(Apache虚拟主机)
直接在根目录新建/覆盖.htaccess:
<IfModule mod_rewrite.c>
Options +FollowSymlinks -Multiviews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [L,E=PATH_INFO:$1]
</IfModule>
现在直接访问:
http://你的域名/
就能正常打开TP8网站,样式、路由、控制器全部正常。
如果你是IIS服务器(用这个web.config)
根目录新建web.config:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="ThinkPHP" stopProcessing="true">
<match url="^(.*)$" ignoreCase="false" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
</conditions>
<action type="Rewrite" url="index.php/{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
最常见的2个问题
页面空白/报错500:给runtime目录权限设为755或777
CSS/JS图片不显示:确认static文件夹已经移到根目录
本站原创内容,转载请注明来源:https://www.liutonghui.com/243
评论列表(0条)
暂无评论