部署laravel项目到Cpanel时出现404错误如何解决?

How to solve Error 404 when deploying laravel project to Cpanel?

我正在尝试将我的 laravel(Laravel Framework 7.28.3)部署到 Cpanel,但出现 404 错误。 我将我的项目上传到 /public_html,修改了 index.php 文件以指向正确的文件(如下所示)。 我认为 index.php 文件中一定有错误,但无法弄清楚。 第一次问这个问题(找了好几遍),希望能得到答案!

require __DIR__.'/../vendor/autoload.php';
$app = require_once __DIR__.'/../bootstrap/app.php';

这是我在文件管理器中的文件结构:

您需要确保您的应用程序位于 public_html 之外的文件夹中。

然后您需要为应用程序中 public 目录中的所有内容创建一个符号 link。这个符号 link 应该放在你的 public_html.

这样您的业务逻辑就无法从外部获得,只能从您自己的应用程序获得。

实际上,建议使用 git 克隆您的应用程序,然后按照文档中的步骤安装它。 (https://laravel.com/docs/7.x/installation)

在 Cpanel 中部署 laravel 应用程序

  • 设置 1 : - 上传文件到 Cpanel root目录——不是 public_html.

  • 设置 2:- 打开该文件夹并将 public 文件夹的内容移动到您的 cpanel 的 public_html.

  • 设置 3:- 导航到 public_html 文件夹并找到 index.php 文件。右键单击它,然后从菜单中选择 select 代码编辑器。

并更改此行

require __DIR__.'/../folderName/vendor/autoload.php';
$app = require_once __DIR__.'/../folderName/bootstrap/app.php';

NOTE : - folderName here is in root where you laravel application stay

就是这样,现在您的所有请求都将进入 public_html 文件夹 index.php,此文件将包括 require __DIR__.'/../folderName/vendor/autoload.php; 和 运行 laravel申请


文件夹结构如下

/laravel
/public_html/index.php

里面index.php

require __DIR__.'/../laravel/vendor/autoload.php';;
$app = require_once __DIR__.'/../laravel/bootstrap/app.php'; // here laravel is folder name