This article will cover how to deploy a Laravel project in shared hosting. As most of the shared hosting user don’t have ssh access enables from the beginning, we will deploy the project with our and composer command. All php artisan commands will be handled by routing featured provided by Laravel  .

Make sure you have the following routes in you r route files. (You can change as per your requirements)

use Illuminate\Support\Facades\Artisan;

Route::group(['prefix' => 'command'], function () {
    Route::get('/migrate', function () {
        Artisan::call('migrate');
        dd('done');
    });

    Route::get('/seed', function () {
        Artisan::call('db:seed');
        dd('done');
    });
    Route::get('/storage', function () {
        Artisan::call('storage:link');
        dd('done');
    });
    Route::get('/clear', function () {
        Artisan::call('cache:clear');
        Artisan::call('config:clear');
    });
});

This urls will be used to access artisan command from browser. You can protect these routes using authentication as per your requirements.

Now the actual deployment starts.

Step 1:
Create a zip of your Laravel project. Make sure you don’t make a rar file as most of shared hosting only supports extraction of zip files.

Step 2:
Upload the zip file to you server and extract in a folder.
Let’s say the folder name is XYZ.

Step 3:
Add database and assign a user to that database.
Assume: database:  XYZ_DATABASE and user: XYZ_USER

Step 4:
Edit .env file of your Laravel project with database credentials.

DB_DATABASE=XYZ_DATABASE
DB_USERNAME=XYZ_USER
DB_PASSWORD=YOUR_PASSWORD


Step 5:
In this step we will point the main domain to public folder. No we are not going to copy all contents of public folder, rather we will create a .htaccess file to directly point the main domain to the index file of public folder.

Here is a sample of the htaccess file:
For domain xyz.com


RewriteEngine on 
# Change example.com to be your main domain. 
RewriteCond %{HTTP_HOST} ^(www.)?xyz.com$ 
# Change 'subdirectory' to be the directory you will use for your main domain. 
RewriteCond %{REQUEST_URI} !^/subdirectory/ 
# Don't change the following two lines. 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
# Change 'subdirectory' to be the directory you will use for your main domain. 
RewriteRule ^(.*)$ /subdirectory/$1 
# Change example.com to be your main domain again. 
# Change 'subdirectory' to be the directory you will use for your main domain 
# followed by / then the main file for your site, index.php, index.html, etc. 
RewriteCond %{HTTP_HOST} ^(www.)?xyz.com$ 
RewriteRule ^(/)?$ subdirectory/index.html [L]


Step 6:
Setting up database table. You can either import your local database to server or run migration using the url we created before step 1.
www.xyz.com/command/migrate

This will run php artisan migrate.
You can also seed database with previously created url and add more  artisan command as per your requirement.


Related

PHP

Custom form request for Google Recaptcha.

PHP

Increase site speed using Redis as a caching server.

PHP

This article will cover how to deploy a Laravel project in shared hosting.

PHP

Tagging is a popular way of categorizing posts in any blog or news related sites.

keyboard_arrow_up