首页 > 解决方案 > Laravel 部署共享主机子域

问题描述

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

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

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

我也试图让文件夹作为原始文件夹,我的路径是公共的,结果是这样,一切正常!

不起作用 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 文件夹。是什么导致了这个问题?引导的路径是正确的..

网络配置:

<!--
    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>

标签: laravelhostingshared-hosting

解决方案


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

从我能读到的评论中,您正在使用 Hostinger,

这是一个快速解决方法,

在您的托管帐户中 > 选择

托管帐户 > 域 > 子域

输入子域名和

检查子域的自定义文件夹

输入项目的路径,然后输入 laravel 的公共目录

例如,

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

瞧,

你现在可以将你的 laravel 核心文件粘贴到 suddomain 和 public 文件夹文件到 public 文件夹。

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


推荐阅读