Cpanel

How to deploy Laravel 8 project on Cpanel shared hosting

Share this post:

In just a few minutes you can deploy your Laravel application on shared hosting Cpanel. The steps to do that are simple as described in this post.

Here are the steps to deploy Laravel on Cpanel

Let me walk you through how to this…

Transfer your project to the Cpanel file manager

The first step is to transfer your Lavarel project to the Cpanel file manager. You can do this via FTP or zip and upload the Laravel project folder. You can use FileZilla to transfer the files.

Note: Ideally, make sure to upload the Laravel project to the root path on the Cpanel file manager.

Move the public folder

When you have transferred the Laravel project to the root of your Cpanel file manager, move the content of the public folder to the public folder of your Cpanel file manager. The Cpanel public folder is usually named public_html, but whatever the name, it should be the same folder that serves up the pages when you visit the root URL of the domain name for the Laravel app.

Assuming that:

  • the public path of your Cpanel is /public_html
  • and the domain name that points to public_html is mylaravel.com
  • then, you should copy the content of your Larevel public folder to public_html

Edit index.php

Now that you have copied the content of the public folder to public_html, edit index.php and:

a. Point correctly to autoload.php

Assuming the folder for your Laravel project is named my-laravel-app, this folder is at the same level/hierarchy as the public_html.

In index.php, you probably have a line like this: require __DIR__.’/../vendor/autoload.php’;
Change it to the correct path: require __DIR__.’/../my-laravel-app/vendor/autoload.php’;

b.Point correctly to app.php

Similary, $app = require_once __DIR__.’/../bootstrap/app.php’;
should be changed to $app = require_once __DIR__.’/../my-laravel-app/bootstrap/app.php’;

c. bind public path

Still in index.php file, add $app->bind(‘path.public’, function() { return __DIR__; }); AFTER app has been declared; that is, after the line $app = require_once __DIR__.’/../my-laravel-app/bootstrap/app.php’;

Edit .env

Finally, edit the .env file and set the APP_URL to the correct URL of the website – APP_URL=https://mylaravel.com. Also, do not forget to turn off debugging: APP_DEBUG=false

How to deploy Laravel project on Cpanel subdirectory

Deploying to a subdirectory is similar to the steps followed above to deploy to the root public directory with just minor changes:

Assuming the path to the subdirectory is /public_html/tutorials, meaning that the URL to the subdirectory is mylaravel.com/tutorials:

Edit index.php

a. Point correctly to autoload.php: require __DIR__.’/../../my-laravel-app/vendor/autoload.php’;

b.Point correctly to app.php: $app = require_once __DIR__.’/../../my-laravel-app/bootstrap/app.php’;

c. bind public path

Still in index.php file, add $app->bind(‘path.public’, function() { return __DIR__; }); AFTER app has been declared; as before.

Edit .env

Similarly, edit the .env file and set the APP_URL to the correct URL of the website – APP_URL=https://mylaravel.com/tutorials. Also, do not forget to turn off debugging: APP_DEBUG=false

How to deploy Laravel project on Cpanel subdomain

Deploying to a subdomain is also similar to the steps followed above to deploy to the root public directory at a root domain.

Any challenge or comment? Use the comment form below.

Leave a Reply

Your email address will not be published.