【问题标题】:Laravel 5 shared hosting deployment issuesLaravel 5 共享主机部署问题
【发布时间】:2016-08-19 16:46:52
【问题描述】:

我正在尝试在共享主机上部署 laravel 5.2。

我将 laravel 核心 app 文件夹安装在 public_html 文件夹(/../public_html 或 /home/username)之上的一级。

我提取了 laravel 核心公用文件夹 (public) 中的文件,并将 index.php 文件指向 laravel 核心应用程序文件夹上方的根目录(/../laravel-app/bootstrap/autoload.php 和 /.. /laravel-app/bootstrap/app.php).

网站主页正在运行,但页面链接返回 404,并且没有拉入任何 css 或 js。

关于如何解决此问题的任何想法?

注意事项: 我在 php 版本 5.6 我将 laravel-app/storage 中的文件权限更改为 777 我删除了 public_html 文件夹中的 .htaccess 文件 服务器是apache

【问题讨论】:

  • 您是否在代码中使用asset() 来链接资源? (css、js、img...)
  • 为什么要删除 .htaccess?该文件对路由很重要。当您没有正确的 .htaccess 时,链接不会发送到 Laravel index.php,因此它会尝试访问不存在的文件夹/文件(因此您会得到 404)
  • 好的。那么如何获取新的 .htaccess 文件呢?
  • 没有。我没有使用 assets() 链接到资源。但是我不确定。如何检查?

标签: php apache .htaccess laravel laravel-5


【解决方案1】:

小伙伴只要按照这个教程,我试过了,它工作得很好!

https://medium.com/laravel-news/the-simple-guide-to-deploy-laravel-5-application-on-shared-hosting-1a8d0aee923e#.5lywcjmnn

顺便说一句,你甚至不需要这句话后面的最后一部分

如果你的服务器上还没有安装 composer,你可以很容易地把它抓取到项目目录中。

就我而言,我做了以下事情: 将 laravel 项目放在 public_html 之外作为 lara 文件夹

然后我有这个htaccess:

<IfModule mod_rewrite.c>
RewriteEngine On

RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>

在 public_html 里面我有 .htacess :

RewriteEngine On
RewriteCond %{REQUEST_URI} !^awesome
RewriteRule ^(.*)$ awesome/$1 [L]

其中awesome是包含htacess(向下列出)和index.php、favicon和robots.txt的目录:

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

    RewriteEngine On

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

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]

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

最后的变化在 index.php 中:

<?php

/**
 * Laravel - A PHP Framework For Web Artisans
 *
 * @package  Laravel
 * @author   Taylor Otwell <taylorotwell@gmail.com>
 */

/*
|--------------------------------------------------------------------------
| 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 nice to relax.
|
*/

/* die(__DIR__);  /home/{YOURHOST}/public_html/awesome */
require __DIR__.'/../../lara/bootstrap/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__.'/../../lara/bootstrap/app.php';

/*
|--------------------------------------------------------------------------
| Run The Application
|--------------------------------------------------------------------------
|
| Once we have the application, we can handle the incoming request
| through the kernel, and send the associated response back to
| the client's browser allowing them to enjoy the creative
| and wonderful application we have prepared for them.
|
*/

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

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

$response->send();

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

【讨论】:

  • 如何运行 compser 命令?我无法通过 ssh 访问服务器。另一件事。我做了教程中列出的所有事情(据我所知,仍然得到错误)。但是,我没有 .htaccess 文件(我错误地删除了它)。
  • 上传你的本地版本的.htaccess,关于你不需要的composer命令,约定是:composer update应该只在本地运行,在这种情况下只有composer.lock是什么将要更改,在您的情况下,您也必须上传供应商文件夹,不要忽略它:)
  • 这就是问题所在。我错误地删除了它(.htaccess 文件)。我该怎么办?
  • 我此时正在运行nginx,所以无法为你测试stackoverflow.com/questions/17228936/…
  • 好的,谢谢。我认为这也是一个 .htaccess 文件问题,但我无法验证。我得想办法得到它
猜你喜欢
  • 1970-01-01
  • 2016-05-21
  • 2018-03-10
  • 2015-06-17
  • 2021-06-28
  • 2019-09-25
  • 2015-06-06
  • 2018-12-02
  • 1970-01-01
相关资源
最近更新 更多