how-to-deploy-laravel-app-to-shared-hosting-from-cpanel-600fbf444aedf1611644740.jpg

How to deploy Laravel app to shared hosting from cpanel

Laravel

Subhadip Ghorui

3 years ago

Subhadip Ghorui

After finishing local development of your Laravel app clear the application cache. You can do it manually by going to the folder or by running some php artisan commands.

A. Locally 

1. php artisan optimize:clear - Remove the cached bootstrap files.

2. php artisan optimize:clear - Remove the cached bootstrap files.

3. php artisan cache:clear - Flush the application cache

4. php artisan event:clear - Clear all cached events and listeners

5. php artisan cofig:clear - Clear all cofig cache files.

5. php artisan view:clear -  Clear all compiled view files

6. php artisan key:generate  - Delete the APP_KEY  from the .env file. Then run this command. It will set the new application key(Very Important).

 Change all the credentials (Database, Mail Server, etc) to your cloud server database credentials in the .env file.

APP_ENV=production

APP_DEBUG=false

APP_URL=http://domain.com (use https:// or http:// as your server has SSL or not.)

Export the fresh copy of your local database and import it on your cloud database.

Compressed all files to a zip folder (use free 7zip). Upload that file to the server using the in-built file manager or FTP.

 

B. On Server

1. Unzip all the files to the Directory where your server public or public_html directory exists (not inside the public_html folder).

2. After that copy all contents of your Laravel public folder to the server public folder(public_html).

3. Then you need to create a symlink, which we are created in the local development by running php artisan storage:link command. If you have ssh access you run this command in the terminal. If you don't have SSH you create it by PHP script, which you need to create a PHP file in the public_html directory of the server, because we can access anything (except folders) in the public_html directory.

4. To create a symlink you have to know the full path of the public_html directory. For the can create file link.php and paste this code.

<?php
echo getcwd();

Save the file and run it by goto URL – http or https://domain.com/link.php

You will see the full path of the public_html folder directory

/home/USER_NAME/domain/domain.com/public_html

Now We know the directory, to create a symlink.php file and use this code –

<?php
$targetFolder = '/home/USER_NAME/domains/domain.com/storage/app/public';
$linkFolder = '/home/USER_NAME/domains/domain.com/public_html/storage';
symlink($targetFolder,$linkFolder);
echo 'Symlink process successfully completed';

Run this by going this url – http or https://domain.com/symlink.php

*You get a message like - Symlink process successfully completed. If you can not create a symlink folder contact your hosting provider.

If you want to unlink or delete the symlink folder paste this code and follow the same step.

<?php
$targetFolder = '/home/USER_NAME/domains/domain.com/storage/app/public';
$linkFolder = '/home/USER_NAME/domains/domain.com/public_html/storage';

unlink($linkFolder);
unlink($targetFolder);
echo 'Symlink process successfully completed';

Almost Done.

 

C. Create A Cron Job

To run many automated terminal codes for the Laravel app use the task scheduler of the Laravel app. You can the queue worker there as a background task.

Set your terminal commands in the Laravel task scheduler in the app/Console/Kernel.php file.

Example:

        $schedule->command('optimize:clear')->daily();
        $schedule->command('config:cache')->daily();
        $schedule->command('cache:clear')->daily();
        $schedule->command('auth:clear-resets')->weekly();
        
        $schedule->command('queue:work')->withoutOverlapping()->runInBackground();
        $schedule->command('queue:flush')->weekdays();

        $schedule->command('backup:clean')-> daily();       
        $schedule->command('backup:run')->daily();

 

Run this scheduler by setting a cron job. Goto custom cron.You have to give full path of the larvel artisan file.

php /home/USER_NAME/domains/domain.com/artisan schedule:run

Congratulation You Successfully Deploy your laravel app.

0 people like this
3440 views
0 comments
Share it on your social media account.

Please Sign in to post comments - Sing in or Register

0 Comments