Currently there are a lot of tutorials on the net regarding uploading Laravel to cPanel hosting, but they are all incorrect or out of date.
There are a lot of tutorials out there on the net regarding uploading Laravel to cPanel hosting now, but they are all incorrect or out of date. In this article, HOSTVN will guide you how to deploy Laravel on hosting correctly.
Instructions for uploading Laravel to cPanel hosting
Step 1: Compress the code
The first step to do is to compress the entire code as a zip file so that it can be uploaded to the hosting quickly. But remember before compressing the code, please clear all Laravel cache with the following command:
php artisan cache:clear php artisan view:clear
After clearing the cache, you proceed to compress the code as a zip file with Winrar.
Note:
- For those of you who use git then, when compressing, skip not compressing the directory .git
- If you use Phpstorm to code, skip uncompressing the folder .idea
Step 2: Export the database
After you have compressed the code, you will need to export to the database to import to hosting. If you are using Localhost software like Xampp, Laragon, you can use Phpmyadmin to export the database as a .sql file.
After exporting you will get a file in the form name-database.sql used to import to hosting in the following steps
Step 3: Upload the code to hosting
To upload the code to your hosting, log in to cPanel -> Filemanger
Next you need to specify the folder that will contain the source code. If the domain name you want to use is the main domain name (Domain name used when registering for hosting), the directory where you need to upload the code will be public_html. Where your domain name is domain addon or subdomains then the folder to upload will be the folder with the same name as domain addon or subdomains.
After determining the folder to upload the source code, click on the folder then select Upload -> Select Select files and select the compressed zip file. Once the upload is complete, click Go back to to return to the directory containing the source code
Next, right click on the zip file and select Extract to extract it
After extracting, don’t forget to delete the zip file that you have uploaded
Step 4: Create database, user Mysql and import Database
After you have created the Database, you can access it Phpmyadmin and select the database to import the exported database in step 2
Step 5: Configure the database connection in the .env . file
Before the name to be able to view and edit the file .envyou need to enable show hidden files
After creating the database, user, import database, you proceed to configure the database connection in the file .env. Right click on the .env file and select Edit
Here you need to correct some of the following information
- APP_DEBUG : Change true to false
- APP_URL : enter the website domain in the form http://domain.com or https://domain.com if using SSL
- DB_DATABASE : Enter the name of the database created in step 4
- DB_USERNAME : Fill in the Mysql User created in step 4
- DB_PASSWORD : Enter the User Mysql password created in step 4
Step 6: Remove public from Url
By default of Laravel all requests will go to the public folder so in the Url there will be /public.
To remove /public from the url you create a file .htaccess directory peer public with the following content
<IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{REQUEST_URI} !^/public/ RewriteRule ^(.*)$ /public/$1 [L,QSA] </IfModule>
Step 7: Setup Cronjob
In case you use Laravel’s queue work, you will need to configure Cronjob on hosting to work. Access cPanel -> Cron Jobs to configure Cron
Here you configure the cronjob similar to the following
/usr/local/bin/php /home/support1/public_html/artisan queue:work --sleep=1 --tries=3 --timeout=120 >/dev/null 2>&1
In there:
- /home/support1/public_html/ : Path to the directory containing the code
- >/dev/null 2>&1 : Don’t spam mail every time you run cronjob
Step 8: Some notes
- After uploading the code, you need to make sure the chmod of the folder is 0755 and the chmod of the file is 0644 otherwise you will get a 403 error.
- In case of error 500, please check again PHP version on hosting and Mysql connection see if it is correct. For Laravel 5.6 and above use PHP 7 for best performance.
Conclusion
Through this article, HOSTVN has guided you in detail how to upload Laravel to cPanel hosting in the most accurate way. If you have any suggestions, you can leave a comment below. In addition, you can register to use cheap hosting or see other shared articles of HOSTVN here.