支持Laravel.io的持续发展→

在Hostinger共享主机上部署Laravel7应用🌎

22 Jul, 2020 4分钟阅读

大家好!

在这篇文章中,我将尝试解释如何将您的Laravel应用程序部署到共享主机。我将使用hostinger.com。

强烈不建议将共享主机用于您的Laravel 7应用程序。还有其他非常便宜的Web托管服务,起价每月5美元。

但一些客户(预算非常低的客户)或情况(例如,您想测试一些东西然后继续)可能要求您使用共享主机。

以下是这里要涵盖的内容:

如何将您的应用程序部署到共享主机(Hostinger)。

步骤1) 前往控制面板的主机部分下的添加网站部分,并添加您的域名。

Alt Text

Alt Text

步骤2) 点击您想使用的域的“管理”。

Alt Text

步骤3) 现在,您必须使用SFTP、SSH访问或Hostinger文件管理器上传应用程序的所有源代码到Hostinger,并在相应的域名目录中。

我假设您已经完成了应用程序的构建——至少是一个可以在本地主机上工作的功能应用程序。

假设您的Laravel项目名称为laravel,具有以下文件夹结构:

Alt Text

  • 使用Hostinger文件管理器:在您的本地计算机上压缩整个项目文件夹。您将得到一个zip文件——laravel.zip,将其上传到项目的根目录并解压缩到laravel文件夹中。

  • 使用SSH:您可以使用ssh客户端(例如putty)在项目的根目录中下载项目。

  • 使用SFTP:您可以使用FileZilla在相应的文件目录中上传项目文件。

之后,您的文件结构应如下所示。 Alt Text

步骤 4) 打开 laravel 文件夹,将 'public' 文件夹的内容移动到您的面板的 public_html 文件夹中。现在您也可以删除空的 public 文件夹了。

您的 public_html 文件夹应该看起来是这样的。

Alt Text

步骤 5) 从 public_html 文件夹中找到 index.php 文件。右键单击它,从菜单中选择代码编辑。

修改 index.php 文件中的以下代码

<?php


define('LARAVEL_START', microtime(true));

/*
|--------------------------------------------------------------------------
| Register The Auto Loader
|--------------------------------------------------------------------------
|
| Composer provides a convenient, automatically generated class loader for
| our application. We just need to utilize it! We'll simply require it
| into the script here so that we don't have to worry about manual
| loading any of our classes later on. It feels great to relax.
|
*/

require __DIR__.'/../vendor/autoload.php';

/*
|--------------------------------------------------------------------------
| Turn On The Lights
|--------------------------------------------------------------------------
|
| We need to illuminate PHP development, so let us turn on the lights.
| This bootstraps the framework and gets it ready for use, then it
| will load up this application so that we can run it and send
| the responses back to the browser and delight our users.
|
*/

$app = require_once __DIR__.'/../bootstrap/app.php';


$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);

$response = $kernel->handle(
    $request = Illuminate\Http\Request::capture()
);

$response->send();

$kernel->terminate($request, $response);

修改为

<?php

define('LARAVEL_START', microtime(true));

/*
|--------------------------------------------------------------------------
| Register The Auto Loader
|--------------------------------------------------------------------------
|
| Composer provides a convenient, automatically generated class loader for
| our application. We just need to utilize it! We'll simply require it
| into the script here so that we don't have to worry about manual
| loading any of our classes later on. It feels great to relax.
|
*/

require __DIR__.'/../laravel/vendor/autoload.php';

/*
|--------------------------------------------------------------------------
| Turn On The Lights
|--------------------------------------------------------------------------
|
| We need to illuminate PHP development, so let us turn on the lights.
| This bootstraps the framework and gets it ready for use, then it
| will load up this application so that we can run it and send
| the responses back to the browser and delight our users.
|
*/

$app = require_once __DIR__.'/../laravel/bootstrap/app.php';


$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);

$response = $kernel->handle(
    $request = Illuminate\Http\Request::capture()
);

$response->send();

$kernel->terminate($request, $response);

如果一切顺利,访问 http://yourdomain.com 应该会抛出数据库错误(如果您有在您的应用程序中运行的模型)。不必担心!下一个阶段是将您的数据库迁移到共享主机。

步骤 6) 如果您的应用程序中正在运行模型,则必须将表迁移到数据库中。我在迁移表时使用 sqlite 数据库。

使用 ssh 访问或文件管理器,在 'laravel/database' 目录中创建一个 'database.sqlite' 文件。(确保授予读写权限)

Alt Text

现在更改 laravel 文件夹中的 .env 文件中的 'DB_DATABASE' Alt Text

确保正确输入这些详细信息。

现在使用 SSH 在您的 laravel 目录中迁移所有文件到数据库。 Alt Text

如果一切顺利,现在您的站点应该能够正常工作了。那么就试试看吧。

谢谢!

上次更新 1 年前。

joedixon, oguzeren, pushpak1300 喜欢这篇文章

3
喜欢这篇文章? 让作者知道并给他们一个掌声!

你可能还喜欢这些文章

2024年3月11日

如何使用 Larastan 将您的 Laravel 应用从 0 运行到 9

在 Laravel 应用执行之前发现错误是可能的,多亏了 Larastan,它...

阅读文章
2024年7月19日

在无需 traits 的情况下标准化 API 响应

我注意到,大多数用于 API 响应的库都是使用 traits 实现的,并且...

阅读文章
2024年7月17日

在 Laravel 项目中通过 Discord 通知收集反馈

如何在 Laravel 项目中创建一个反馈模块,并在收到消息时收到 Discord 通知...

阅读文章

感谢这些 极棒的公司 对我们的支持

在这里放置您的标志?

Laravel.io

Laravel 问题的解决、知识共享和社区建设的大门。

社交

社区

© 2024 Laravel.io - 版权所有。