Laravel lumen 使用共享主机时出现内部服务器错误 500?

gla*_*rog 0 php .htaccess shared-hosting laravel lumen

我是 Laravel/lumen 的新手。我编写了一个简单的起始代码来使用控制器函数返回响应。它在我的本地服务器上工作正常,但当我使用共享主机时,控制器功能的路由会出现 500 内部服务器错误。任何不使用控制器功能的东西都可以。就像是

$router->get('/', function () use ($router) {
    return phpinfo();
});
Run Code Online (Sandbox Code Playgroud)

返回 php 信息。使用 IONOS 托管。不使用任何数据库连接。我在 StackOverflow 中尝试过几个类似的答案,但无法弄清楚。该域指向项目的公共文件夹。尝试使用 FTP 和 ssh 安装。两者结果相同。

内部服务器错误 服务器遇到内部错误或配置错误,无法完成您的请求。

请联系服务器管理员,告知他们发生此错误的时间以及您在发生此错误之前执行的操作。

有关此错误的更多信息可在服务器错误日志中找到。

此外,尝试使用 ErrorDocument 处理请求时遇到 500 Internal Server Error 错误。

存储/日志中没有错误信息

控制器代码... <?php

namespace App\Http\Controllers;

class ExampleController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }
    public function profile(){
        return response('hello! Controller Works...');
    }

    //
}
Run Code Online (Sandbox Code Playgroud)

路由器代码..

<?php

/** @var \Laravel\Lumen\Routing\Router $router */

/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It is a breeze. Simply tell Lumen the URIs it should respond to
| and give it the Closure to call when that URI is requested.
|
*/

$router->get('/', function () use ($router) {
    return $router->app->version();
});
$router->get('profile', [
    'as' => 'profile', 'uses' => 'ExampleController@profile'
]);
Run Code Online (Sandbox Code Playgroud)

.htaccess 文件

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

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>
Run Code Online (Sandbox Code Playgroud)

删除 .htaccess 将给出未找到错误。.env 文件

APP_NAME=Lumen
APP_ENV=production
APP_KEY=<my generated key>
APP_DEBUG=false
APP_URL=http://<my domain>
APP_TIMEZONE=UTC

LOG_CHANNEL=stack
LOG_SLACK_WEBHOOK_URL=

CACHE_DRIVER=file
QUEUE_CONNECTION=sync
Run Code Online (Sandbox Code Playgroud)

gla*_*rog 6

经过一整天的搜索,发现问题出在 .htaccess 文件上。

它应该RewriteRule ^ /index.php [L]代替RewriteRule ^ index.php [L]

根据文章,500 个内部服务器错误中的大多数都与 .htaccess 文件有关。所以对于任何新手来说,这都是一个有点头疼的问题。