Deploying Laravel 10 with Vite and Node.js on Hostinger Using Git

 In this guide, we'll cover how to deploy a Laravel 10 project with Vite and Node.js to Hostinger, pull updates from Git, and build the assets using Vite.

    hostinger flate 20% discount


Step 1: Set Up Laravel on Local Machine

Make sure your Laravel 10 project is working perfectly on your local machine, and you are using Vite for asset management.

  1. Install dependencies:

composer install 
npm install
  1. Test Vite build:

npm run build
  1. Commit changes to Git: Ensure your project is connected to a Git repository.
git add . 
git commit -m "Initial Laravel setup with Vite" 
git push origin main

Step 2: Configure Hostinger for Laravel

On your Hostinger shared hosting, follow these steps:

  1. Login to Hostinger:

    • Access your Hostinger control panel.
  2. Set up your domain:

    • Point your domain to the public directory of your Laravel project.
  3. Install Composer and Node.js:

    • Use SSH to install Composer and Node.js on Hostinger.
curl -sS https://getcomposer.org/installer | PHP 
mv composer.phar /usr/local/bin/composer
    • To install Composer:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash 
source ~/.bashrc 
nvm install node
    • To install Node.js, use the Node Version Manager (NVM):

Step 3: Clone Your Git Repository on Hostinger

  1. Access your Hostinger server via SSH.

  2. Navigate to the desired folder (where you want to clone your Laravel project).

cd /home/yourusername/domain_name

Clone your repository:

git clone https://github.com/yourusername/your-laravel-project.git .

Install Laravel dependencies:

composer install

Step 4: Set Up Environment Variables

  1. Create a .env file on your server by copying the .env.example:

cp .env.example .env
  1. Update the .env file with the necessary details, such as database credentials, APP_URL, and others.

  2. Generate an application key:

php artisan key:generate

Step 5: Build Assets Using Vite

  1. Install NPM packages for the production environment:


npm install

  1. Run Vite build:


npm run build
  1. Set the correct permissions for the storage and bootstrap directories:

chmod -R 775 storage 
chmod -R 775 bootstrap/cache

Step 6: Set Up Web Server

  1. Modify the public folder as the web root:

    • Go to the Hostinger control panel.
    • Under Domains, set the document root to /home/yourusername/your-laravel-project/public.
  2. Configure .htaccess (Optional): You may need to edit your .htaccess file to ensure proper routing.


<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews -Indexes
    </IfModule>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>


Step 7: Database Migration

  1. Run your database migrations after setting up your database in the .env file:


php artisan migrate
  1. Seed your database (Optional):

php artisan db:seed

Step 8: Set Up Cron Jobs (Optional)

If your project requires scheduled tasks (e.g., running Laravel queues or scheduled commands), set up a cron job in Hostinger.

  1. Go to the Cron Jobs section in your Hostinger control panel.
  2. Add a cron job for running the Laravel scheduler:
* * * * * /usr/bin/php /home/yourusername/your-laravel-project/artisan schedule:run >> /dev/null 2>&1

Step 9: Automate Pulling Changes from Git

  1. Set up a Git hook to automatically pull the latest changes from the repository when you push to the main branch:

    Create a post-receive hook:

    • On your local machine
nano .git/hooks/post-receive
    • Add the following:
#!/bin/bash git --work-tree=/home/yourusername/your-laravel-project --git-dir=/home/yourusername/your-laravel-project.git pull origin main 
npm run build 
php artisan migrate
  1. Give the hook permission to execute:

    chmod +x .git/hooks/post-receive


Note:-

Composer not working try this 


composer2 install --optimize-autoloader --no-dev


laravel storage link not working try


ln -s ../storage/app/public public/storage



Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.