Hosting Laravel 8 to a Shared Host

Posted by admin at November 8, 2021

Some personal notes on keeping hosting on a shared host. Just finished up a couple of Laravel projects and I want to host them quickly so that the clients can see.

Below are the steps to hosting Laravel 8 on a control panel based hosting service.

1. Generate production files

At the terminal, type:

npm run dev

This will create CSS and JS scripts that will run on the production server. If this step is not done, the inertiaJS scripts will still try to connect to the Laravel development server (usually localhost:8080), which will be a problem.

2. Upload the project files:

Fire up FileZilla, login and upload to a specified sub folder on the server. Or the kunkly way, zip the files up and upload via the browser.

3. The /public folder

Open the /public folder in the project root
Move all the contents from this location to the project root (one directory level up). You can also decide to move it to a designated public_html folder if you CPanel mandates one of those. In this exercise, I want all my files contained in the project root.

4. The ./index.php file

Next navigate to the project root (one directory level up).
Open the ./index.php file
Locate and change the following lines:

Next navigate to the project root (one directory level up).
Open the ./index.php file
Locate and change the following lines:


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

// locate this
require __DIR__.'/../vendor/autoload.php';

// change to this
require __DIR__.'/vendor/autoload.php';

// locate this
$app = require_once __DIR__.'/../bootstrap/app.php';

// change to this
$app = require_once __DIR__.'/bootstrap/app.php';

5. The ./server.php folder

Still at the root folder, locate the server.php file.
Locate and change the following:


// locate this
if ($uri !== '/' && file_exists(__DIR__.'/public'.$uri)) {
    return false;
}
// change to this
if ($uri !== '/' && file_exists(__DIR__.$uri)) {
    return false;
}

// locate this
require_once __DIR__.'/public/index.php';

// change to this
require_once __DIR__.'/index.php';

6. The app.blade.php file

Next, located the file resources/views/app.blade.php. Then locate the line:


<script src="{{ mix('/js/app.js') }}" defer></script>

Change it to:


<script src="{{ asset('/js/app.js') }}" defer></script>

The mix tool is used in the development environment to update the changes to the code base on the UI and for creating production files. It is not needed in production

7. Additional Laravel settings:

Open the .env file in the folder and make the following changes:


// from local to staging
APP_ENV=staging

// turn off debug messages, set from true to false
APP_DEBUG=true

// from localhost to actual url of the project root
APP_URL=https://sharedhost.com/
   0 likes

Suggested Read