How to deploy a Laravel application on a subdomain through cPanel?

In this article, I will show the process of creating a subdomain and hosting a Laravel project on it.

Creating Subdomain

To create a subdomain, log in to cPanel and go to the cPanel dashboard page. The subdomain menu is located in the “Domains” section. Alternatively, you can search for subdomains from the Search option.

Next, click on the Subdomain option.

Type your desired subdomain name and select the domain you want to use for the subdomain. It will automatically set the Document Root and create a directory for the subdomain as a sibling to the public_html folder.

Click on the Create button.

You have created a subdomain successfully.

Setup Laravel Project

Upload your Laravel project to the subdomain document root directory. You can use FTP(FileZilla) to transfer the project or directly upload it to the cPanel by making a zip file of the project. Then, extract the project’s content to your subdomain document root directory.

To access your Laravel project open a web browser and enter your subdomain address followed by ‘public’ (e.g. {subdomain}/public).

Now, go to the public directory of your subdomain document root directory. Move all the files of the public directory to its parent directory(subdomain document root) and then edit the index.php file as follows:

if (file_exists($maintenance = __DIR__.'/../storage/framework/maintenance.php')) {
    require $maintenance;
}
require __DIR__.'/../vendor/autoload.php';
$app = require_once __DIR__.'/../bootstrap/app.php';

to

if (file_exists($maintenance = __DIR__.'/storage/framework/maintenance.php')) {
    require $maintenance;
}
require __DIR__.'/vendor/autoload.php';
$app = require_once __DIR__.'/bootstrap/app.php';

Now, you can access your Laravel application from the subdomain address.