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.
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.
Install dependencies:
composer installnpm install
Test Vite build:
npm run build
- 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:
Login to Hostinger:
- Access your Hostinger control panel.
Set up your domain:
- Point your domain to the
public
directory of your Laravel project.
- Point your domain to the
Install Composer and Node.js:
- Use SSH to install Composer and Node.js on Hostinger.
curl -sS https://getcomposer.org/installer | PHPmv composer.phar /usr/local/bin/composer
- To install Composer:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bashsource ~/.bashrcnvm install node
- To install Node.js, use the Node Version Manager (NVM):
Step 3: Clone Your Git Repository on Hostinger
Access your Hostinger server via SSH.
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
Create a
.env
file on your server by copying the.env.example
:
cp .env.example .env
Update the
.env
file with the necessary details, such as database credentials,APP_URL
, and others.Generate an application key:
php artisan key:generate
Step 5: Build Assets Using Vite
Install NPM packages for the production environment:
npm install
Run Vite build:
npm run build
Set the correct permissions for the storage and bootstrap directories:
chmod -R 775 storagechmod -R 775 bootstrap/cache
Step 6: Set Up Web Server
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
.
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 OnRewriteCond %{REQUEST_FILENAME} !-dRewriteCond %{REQUEST_FILENAME} !-fRewriteRule ^ index.php [L]</IfModule>
Step 7: Database Migration
Run your database migrations after setting up your database in the
.env
file:
php artisan migrate
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.
- Go to the Cron Jobs section in your Hostinger control panel.
- 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
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:
git --work-tree=/home/yourusername/your-laravel-project --git-dir=/home/yourusername/your-laravel-project.git pull origin mainnpm run buildphp artisan migrate
- Give the hook permission to execute:
chmod +x .git/hooks/post-receive
Note:-
composer2 install --optimize-autoloader --no-dev
ln -s ../storage/app/public public/storage