There are a lot of tutorials out there on uploading Laravel to cPanel hosting, but they are either inaccurate or outdated. In this article, HOSTVN will guide you how to deploy Laravel on hosting most accurately.
Instructions to upload Laravel to cPanel hosting
Step 1: Compress the code
The first step is to compress the entire code as a zip file so that it can be uploaded to hosting quickly. But remember, before compressing the code, delete the entire Laravel cache with the following command:
php artisan cache: clear
php artisan view: clear
php artisan cache:clear php artisan view:clear |
After clearing the cache, you need to compress the code as a zip file Winrar.
Note:
- For you to use git then when compressed ignores uncompressed directory .git
- If you use Phpstorm so the code skips uncompressed directory .idea
Step 2: Export the database
After compressing the code you will need to export the database to import on 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 receive a file of the form name-database.sql Use to import into hosting in the following steps
Step 3: Upload code to hosting
To upload code to hosting you log into cPanel -> Filemanger
Next you need to determine the directory that will contain the source code. If the domain name you want to use is the main domain name (The domain name used when registering hosting), the directory to upload the code to will be public_html. Where your domain name is domain addon or subdomain then the directory to upload will be the directory with the same name domain addon or subdomain.
After you've determined the directory to upload the source code, click on the folder and then select Upload -> Select Select file and choose the zipped zip file. After the upload process is complete please click on Go back to to return to the source directory
Next, right-click the zip file and select Extract to extract
After extracting, don't forget to delete the zip file that you uploaded
Step 4: Create database, Mysql user and import Database
After creating the Database you access Phpmyadmin and select the database to import the exported database in step 2
Step 5: Configure the database connection in the .env file
After creating the database, the user and the database import, 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 : Correct true to false
- APP_URL : Enter the domain website 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 : Enter the User Mysql created in step 4
- DB_PASSWORD : Enter the password Mysql created in step 4
Step 6: Remove public from the Url
By default of Laravel all requests will go into the public directory so there will be more in the url / public.
To remove / public from the url you create the file .htaccess peer to peer directory public with the following content
RewriteEngine On
RewriteCond% {REQUEST_URI}! ^ / Public /
RewriteRule ^ (. *) $ / Public / $ 1 (L, QSA)
<IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{REQUEST_URI} !^/public/ RewriteRule ^(.*)$ /public/$first (L,QSA) </IfModule> |
Step 7: Some notes
- After uploading the code you need to make sure the directory's chmod is 0755 and the file's chmod is 0644 If not, you'll get a 403 error.
- In case of error 500 please check again PHP version on hosting and Mysql connection See if it's correct or not. For Laravel 5.6 and above use PHP 7 to give the best performance.
Conclude
Through this article, HOSTVN has instructed you on how to upload Laravel to cPanel hosting most accurately. If you have any comments you can leave a comment below. In addition, you can view other shared articles of HOSTVN here.