How to deploy a Laravel application on a cPanel powered server
We love Laravel. But when it comes to deploying our app on shared hosted server powered by cPanel, it can be a little confusing. I've been searching the web for a solution that has these characteristics: (1) Allowed me to clone my repo from remote source control (2) Allowed me to pull changes without redeploying my app (3) Was clean. I didn't find it right away, so I am bringing it to you.
Step 1 : Login to cPanel
This is a no brainer:
- Go to https://cpanel.yourdomain.com/ or http://yourdomain.com:2083 (Replace "yourdomain.com" with your own domain name
- Login with your cPanel credentials
- Go into Advance section > terminal (Make sure you have SSH access from your provider)
Step 2 : Composer
Make sure you have Composer installed. If not, install it. We're gonna need it.
Step 3 : Clone your repo
In your terminal, execute the following commands. First cd to your public_html folder:
Replace "username" with your username.
Then initialize git
Add your remote repository's URL
Replace with your own repositoy URL, whether from Github, Bitbucket or whichever remote repository hosting service you use.
When done, pull your app with this command
I assume here that you want to deploy your master branch to production. You can deploy any branch, just change the name of the branch.
Step 4: Setup your application
Remember when I asked you to check for or install composer? We need it now. So we run the following command to install our dependencies:
Then change your environmental variables in the .env file. Setup your database, database name, database host and all other things.
When done, generate your application key with this command
Time to migrate your database
Don't forget to set the correct permissions for storage/ folder
Step 5: Make your app accessible to the public
This is the trickier part. Many resources I found online propose many solutions. But Most of them don't follow my requirements as described in introduction to this article. Here is the solution that meets better those requirements
First, backup your .htaccess file by running this command:
Then create another .htaccess file:
In the open .htaccess file, paste the following lines:
Replace example.com with your own domain. Then save your file and close it (Ctrl + o then Ctrl x).
Finally, create a sym link to your app's storage folder
You can check your application by opening your browser and going to yourdomain.com. You should see your app's home page.
I hope this tutorial helped you deploy your Laravel app on a cPanel powered server.
Share with others:
Recently published articles!