How to host a Laravel project on Hostinger shared hosting

Introduction

When I was looking to deploy a Laravel project on Hostinger shared hosting, I found no proper guide available on the internet. So I decided to write a step-by-step guide on how to host a Laravel project on Hostinger shared hosting. I hope this will help someone looking to host a Laravel project on Hostinger shared hosting.

Hostinger's shared hosting does not provide the same functionalities as a VPS or Dedicated Server, so there are some limitations. However, I have tried to overcome those limitations and make the Laravel project work on Hostinger shared hosting.

What is working

The following is working:

  1. Host Laravel 10 Project on Hostinger Shared Hosting

  2. Does not have to move any files from Laravel's Public folder or change any folder structure.

  3. The storage link is working i.e. you can create symbolic links

Steps to host a Laravel project on Hostinger shared hosting

  1. Create a new website on Hostinger's hPanel: Add your domain to create a new website.

  2. Create a new MySQL database on Hostinger's hPanel: Also create a new user, assign the user to the database, and note down the database name, username, and password.

  3. Navigate to the root directory of your website: Use the File Manager on Hostinger's hPanel to go to the root directory of your website.

  4. Delete all files and folders in the root directory: This includes public_html and DO_NOT_UPLOAD_HERE.

  5. Optimize your Laravel Project for Production: Optimize the project and make it ready for deployment to production as per the official Laravel documentation.

  6. Upload your Laravel project: After doing all the required steps upload all files and folders of your Laravel project to the root directory of your website.

  7. Get SSH access to your website: SSH access is available on Hostinger's hPanel. Get SSH access and use Windows PowerShell, PuTTY, or any other SSH client to connect to your website via the SSH terminal.

  8. Setup .env file: In a Laravel Project the .env file holds all the configuration variables. You can either create a new .env file or rename the .env.example file to .env. You can also upload your local .env file if you have already. After that open the .env file and modify the following variables:

    • APP_URL: Set this to the URL of your website.

    • DB_HOST: Set this to the hostname of your database server. This is usually localhost.

    • DB_PORT: Set this to the port of your database server. This is usually 3306.

    • DB_DATABASE: Set this to the name of your database.

    • DB_USERNAME: Set this to the username of your database user.

    • DB_PASSWORD: Set this to the password of your database user.

    • APP_ENV: Set this to production, this will optimize your Laravel project for production.

    • APP_DEBUG: Set this to false, this will disable debug mode.

  9. Migrating the Database: After getting SSH access to your website, navigate to the root directory of your website and run the following command:

     php artisan migrate
    

    This will migrate the database and create all the required tables if database migrations are available in your Laravel project and you have configured the .env file correctly with the required database credentials.

  10. Rename the Public folder: The uploaded Public folder should be renamed to public_html.

  11. Open the filesystems.php file: This file is located in the config folder of your Laravel project.

  12. Modify theSymbolic Links section: Change the following line in the filesystems.php file:

    'links' => [
            public_path('storage') => storage_path('app/public'),
        ],
    

    to

    'links' => [
            base_path('public_html/storage') => storage_path('app/public'),
        ],
    
  13. Open the SSH terminal and go to the root directory of your website

  14. Run the following command:

    php artisan storage:link
    

    After that clear all the cache and config by running the following command:

    php artisan optimize:clear
    
  15. Cache the routes, views, config and routes by running the following command:

    php artisan optimize
    
  16. Have a running Laravel project on Hostinger shared hosting: Now you have a running Laravel project on Hostinger shared hosting.