How To Deploy Laravel On Shared Hosting(Cpanel)

Recent Posts

Derived Web

AI vs ChatGPT

10

AI vs ChatGPT

Let’s imagine that you have developed an app with Laravel and then upload or deploy the app on shared hosting (cPanel). Unfortunately, it’s not straightforward because of the different directory structures of Laravel and cPanel.

In this post, I will show you the step-by-step process of how to upload or deploy a Laravel project on cPanel.

How do you deploy a Laravel application to shared hosting?

To deploy a Laravel application to shared hosting, we can use the following steps.

Step 1: Compress the Laravel project folder.

The Laravel application should be compressed to a zip file on your local machine. This allows for easy upload to the Cpanel

Step 2: Upload the zipped project to cPanel
  • Login into your cPanel


  • Click on File Manager



  • Go inside the public_html



  • Click on Upload
  • In the pop-up, choose the zipped project (which you did in step 1)
Step 3: Extract your zipped project

After uploading the zipped project, extract the files by right-clicking on the zip file (in public_html).



Open the extracted folder and click on the select all tab to select all the files. Right-click on the files and click move. We want to move them to the main folder we created and then delete the folder that was created when we extracted the files.

Step 4: Setting up the index.php file

Navigate to the public folder (public_html/public) and move the index.php file to the public_html folder and click on edit. On this file, there are two lines we need to edit to link the index.php with our laravel project folder.



Step 5. Setting up the database

Go to the database tab in the Cpanel as shown below and click on the desired database



In this case, I will use the MySQL database for reference. Click on the option for MySQL Databases and this will allow us to create a database

Create a database & user. (Make sure to save the database name, user, and password into a text file in a safe place.)



  • Add New User



  • Add User to Database

Go to the phpMyAdmin tab and select the database we have created. Now go to the import tab and import the database file we have exported.



Step 6: Update database details

Go to public_html/.env file and update following section:



Step 7: (optional) Clearing cache


Now if you visit your site e.g. http://example.com it should render your app properly.

Conclusion

I think shared hosting is a good place to start deploying your Laravel application, but it definitely is not the best. It is better to have your application in a cloud hosting provider such as Digital Ocean, Vultr, Cloudways, or Contabo if you are on a tight budget and scale from there.

This way, you can have more control over your application, and you will have access to an SSH terminal from where you can run artisan commands at will. Thank you for reading.