Deploy Laravel Project on Shared Hosting CPanel

Ignasius
3 min readJan 1, 2021

Hello, In this article, I want to share my experience deploying projects on shared CPanel hosting. I rarely deploy into CPanel, but when I need to deploy, I’m doing this.

There 2 types of deployment on CPanel
-Primary Domain
-Addon Domain

On the Addon domain, you can just point the root directory to a public folder like this, by clicking on the pencil logo on Document Root.

But on the Primary domain, it’s a bit tricky, since you cannot change the Document Root, without the root access to the server.

The step to make it working on the Primary domain is like this:

  1. Refactor the Folder Structure, since the shared hosting will look for either index.php or index.html.
CPanel File Manager Directory
  • Move the file inside the public folder into public_html
  • Create the laravel folder on the home directory
  • Move the rest of the folders (exclude the already moved public folder) into the laravel folder.

2. Open the index.php file on public_html/index.php.

public_html/index.php file
  • Replace the require on line 34 into 35.
  • Replace the #app on line 47 into line 48.
  • Add the bind command to change the public path ( line 49 into 51 ).

3. (Optional) Open the server.php file on laravel/server.php.
Only required on a newer version of laravel.

laravel/server.php
  • Replace the require_once on line 21 into line 22.

4. (Optional) Symbolic Link the Storage

Usually, there is the necessity to link the storage folder to our public folder, if you have shell access, you can easily run this command.

php artisan storage:link

If you don’t have access to the shell, you can add the extra routes on routes/web.php.

Route::get('/storage/{extra}', function ($extra) {
return redirect('/public/storage/$extra');
}
)->where('extra', '.*');

That’s all folks.

Conclusion

Deploying laravel into shared CPanel hosting is not that hard, but if possible, opt for VPS hosting can make your DX more pleasant. This guide is created while I’m deploying the project for the client on shared CPanel hosting, using Laravel 7.

Some References guide I use
- https://medium.com/backenders-club/how-to-host-a-laravel-project-on-a-shared-hosting-via-cpanel-d955d32c528e

--

--