Web Development Handbook

Web Development Handbook

Development is fun in a funny way

Deploy guideline: Laravel project in Shared Hosting(Cpanel)

Deploy guideline: Laravel project in Shared Hosting(Cpanel)

0 votes
0 votes
0 comments
104 views
Share

By Nihar Ranjan Das, Published on March 7th 2024 | 7 mins, 675 words

Hey there, fellow Laravel enthusiasts! Are you looking to deploy your Laravel application on a shared hosting server without breaking the bank? Look no further! I've got you covered with a simple yet effective guide to make your deployment process a breeze.

Shared hosting is one of the cheapest ways to deploy a Laravel application to a live server. To deploy a Laravel project on Shared hosting, you generally need to follow these steps:

  1. Compress the source code and upload
  2. Setting up the server.php file
  3. Create a .htaccess file
  4. Setting up database
  5. Delete the files inside bootstrap/cache & storage/logs folder
  6. Run few artisan command


Compress the source code and upload

You need to compress the Laravel application to a zip file on your local machine. This is the easiest way to upload the source code in Cpanel. Then upload the compressed file in the website's root directory. After uploading the file, unzip it.

Setting up the server.php file

There are many ways to make live Laravel project live but the best easy way is to create a server.php file in the root directory of the website. After creating the server.php file, put the bellow code in the file.

<?php

require_once __DIR__ . '/public/index.php';

Create a .htaccess file

The full meaning of htaccess is Hypertext Access. Using the htaccess file and some command within it, we can control the some specific functionality. 

We need to create a .htaccess file in the root directory of website. Then need to put the bellow code in the htaccess file.

<IfModule mod_rewrite.c>    
    RewriteEngine On 
       
    RewriteCond %{HTTPS} !=on   
    RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]

    RewriteCond %{REQUEST_FILENAME} -d [OR]    
    RewriteCond %{REQUEST_FILENAME} -f    
    RewriteRule ^ ^$1 [N]

    RewriteCond %{REQUEST_URI} (\.\w+$) [NC]    
    RewriteRule ^(.*)$ public/$1 

    RewriteCond %{REQUEST_FILENAME} !-d    
    RewriteCond %{REQUEST_FILENAME} !-f  
  
    RewriteRule ^ server.php
</IfModule>

Delete the files inside bootstrap/cache & storage/logs folder

Sometimes the old files of bootstrap/cache and storage/logs folder break the website. We need to delete those files.

Run few artisan command

After creating laravel project locally we need to run some artisan commands. In live server also we need to run those commands also. There are three ways to run those commands. The most important artisan commands are

  • php artisan key:generate
  • php artisan storage:link
  • php artisan optimize
  • php artisan migrate
  • php artisan seed

Create route url in the web.php and add those artisan commands as bellow:

use Illuminate\Support\Facades\Artisan;

Route::get('/clear-cache', function () {    
   Artisan::call('key:generate');    
   Artisan::call('storage:link');    
   Artisan::call('optimize');    
   Artisan::call('migrate');    
   Artisan::call('seed');
});

Or get access of your server using ssh from command line. Then go to the source code directory and run those above listed commands one by one.

Or if there is a Terminal tool in your cpanel, open the tool, go to the root directory and run those commands

Access the source code using terminal.png 73.85 KB

Here's a quick rundown of the steps:

  1. Compress and Upload: Zip up your Laravel application on your local machine and upload it to your shared hosting server via cPanel. Unzip it once uploaded.
  2. Setup server.php: Create a server.php file in your website's root directory and add the necessary PHP code to it. This will streamline the process of serving your Laravel application.
  3. Create .htaccess File: Craft a .htaccess file in your root directory to handle URL rewriting and redirection. This ensures smooth navigation within your application.
  4. Clear Cache and Logs: Delete any outdated files within the bootstrap/cache and storage/logs folders to keep your application running smoothly.
  5. Run Artisan Commands: Execute essential artisan commands such as key generation, storage linking, optimization, migration, and seeding. You can do this either through a custom route in web.php or by accessing your server via SSH or Terminal tool in cPanel.


By following these steps, you'll have your Laravel application up and running on shared hosting in no time, saving both time and money. So go ahead, deploy with confidence, and let your creativity shine online! 🌟 #Laravel #SharedHosting #DeploymentMadeEasy

If you like our tutorial, do make sure to support us by buy us a coffee ☕️

Comments

Default avatar

Are you interested to learn more?

Be notified on future content. Never spam.