Laravel 部署共享托管子域

时间:2021-04-02 01:51:51

标签: laravel hosting shared-hosting

嗨,我花了几个小时让我的网站在共享主机上运行,​​但什么也没发生(本地没问题), 搜索了几个主题并遵循了教程,但无法解决无法加载 bootstrap 和 livewire 组件的问题。

我按照经典步骤将公共文件夹复制到我的子域的根目录,其他文件位于子域路径内的单独文件夹中,如 Laravel 文件夹 我更改了 public/index.php 并且我得到了类似 this

如您所见,livewire 和引导程序不起作用。

我还尝试让文件夹为原始文件夹,并将路径设为公开,结果就像 this 一样,一切正常!

css和livewire不起作用的索引路径:

<?php

use Illuminate\Contracts\Http\Kernel;
use Illuminate\Http\Request;

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

/*
|--------------------------------------------------------------------------
| Check If Application Is Under Maintenance
|--------------------------------------------------------------------------
|
| If the application is maintenance / demo mode via the "down" command we
| will require this file so that any prerendered template can be shown
| instead of starting the framework, which could cause an exception.
|
*/

if (file_exists(__DIR__.'/../storage/framework/maintenance.php')) {
    require __DIR__.'/../storage/framework/maintenance.php';
}

/*
|--------------------------------------------------------------------------
| Register The Auto Loader
|--------------------------------------------------------------------------
|
| Composer provides a convenient, automatically generated class loader for
| this application. We just need to utilize it! We'll simply require it
| into the script here so we don't need to manually load our classes.
|
*/

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

/*
|--------------------------------------------------------------------------
| Run The Application
|--------------------------------------------------------------------------
|
| Once we have the application, we can handle the incoming request using
| the application's HTTP kernel. Then, we will send the response back
| to this client's browser, allowing them to enjoy our application.
|
*/

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

$kernel = $app->make(Kernel::class);

$response = tap($kernel->handle(
    $request = Request::capture()
))->send();

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

我知道我没有放 /../ 因为我得到错误无法识别路径,可能是因为是子域,并且没有 public_html 文件夹。 是什么导致了这个问题? bootstrap的路径是正确的..

Web.config:

<!--
    Rewrites requires Microsoft URL Rewrite Module for IIS
    Download: https://www.iis.net/downloads/microsoft/url-rewrite
    Debug Help: https://docs.microsoft.com/en-us/iis/extensions/url-rewrite-module/using-failed-request-tracing-to-trace-rewrite-rules
-->
<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <rule name="Imported Rule 1" stopProcessing="true">
          <match url="^(.*)/$" ignoreCase="false" />
          <conditions>
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
          </conditions>
          <action type="Redirect" redirectType="Permanent" url="/{R:1}" />
        </rule>
        <rule name="Imported Rule 2" stopProcessing="true">
          <match url="^" ignoreCase="false" />
          <conditions>
            <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" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>

访问:

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews -Indexes
    </IfModule>

    RewriteEngine On

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule ^ %1 [L,R=301]

    # Send Requests To Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

1 个答案:

答案 0 :(得分:1)

晚了,但希望对未来的观众有用

从我读到的评论来看,您正在使用 Hostinger,

这是一个快速的解决方法,

在您的主机帐户中 > 选择

<块引用>

托管帐户 > 域 > 子域

输入子域名和

<块引用>

检查子域的自定义文件夹

输入你的项目路径后跟laravel的public目录

例如,

/home/u796769252/public_html/<subdomain>/public

瞧,

您现在可以将 Laravel 核心文件粘贴到 suddomain 并将公共文件夹文件粘贴到公共文件夹。

我写了一篇blog,其中提到了如何在不编辑 Laravel 核心文件的情况下将 Laravel 应用程序发布到共享主机。