Search code examples
phplaravel.htaccesslumenshared-hosting

Laravel lumen Internal server error 500 using shared hosting?


I am new to Laravel/lumen. I made a simple starter code to return a response using a controller function. It works fine on my local server but when I use my shared hosting, the route to the controller function gives 500 internal server error. Anything not using controller functions works. Something like

$router->get('/', function () use ($router) {
    return phpinfo();
});

returns the php info. Using IONOS hosting. Not using any database connection. I have tried several similar answers in StackOverflow, but couldn't figure out. The domain points to the public folder of the project. Tried using FTP and ssh installation. Both the same result.

Internal Server Error The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator at to inform them of the time this error occurred, and the actions you performed just before this error.

More information about this error may be available in the server error log.

Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request.

There is no error info in the storage/logs

Controller code... <?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...');
    }

    //
}

Router code..

<?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'
]);

.htaccess file

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

Removing the .htaccess will give a not found error. .env file

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

Solution

  • After searching a full day, figured out the problem is with the .htaccess file.

    it should be RewriteRule ^ /index.php [L] instead of RewriteRule ^ index.php [L]

    According to the articles, most of the 500 internal server errors are related to .htaccess file. So for any newbies, it's a bit of a headache.