Deployment OF Laravel application with NPM (Node Package Manager) to Hostinger
Deployment of Laravel Application with NPM to Hostinger Using the Pre-Build Process and Vite
Introduction: Deployment of Laravel Application with NPM to Hostinger Using the Pre-Build Process and Vite
1. Prepare Your Laravel Application
- Run composer install –optimize-autoloader –no-dev to install PHP dependencies without development dependencies.
- Run npm install and npm run prod to compile front-end assets.
- Set up the .env file with production configurations, especially database and app settings.
2. Upload Laravel Files to Hostinger
Option 1: Using File Manager
- Compress your Laravel project into a .zip file.PHP dependencies without development dependencies.
- Log in to your Hostinger control panel.
- Go to File Manager and upload the .zip file.
- Extract the contents to the public_html folder (or a subfolder for a subdomain).
Option 2: Using FTP
- Use an FTP client (e.g., FileZilla) and connect to your Hostinger account.
- Upload your Laravel files to the public_html directory.
3. Set Up the Public Directory
Option 1: Move the Contents
- Copy all the contents of the public directory in Laravel to public_html.
- Update index.php in public_html:
require __DIR__.'/../vendor/autoload.php';
$app = require_once __DIR__.'/../bootstrap/app.php';
Option 2: Use a Subfolder
- Keep the Laravel structure intact.
- In your Hostinger panel, set the document root of your domain or subdomain to the Laravel public directory.
4. Set File Permissions
chmod -R 775 storage bootstrap/cache
5. Configure Environment
- Update the database credentials (DB_DATABASE, DB_USERNAME, DB_PASSWORD) to match the Hostinger database.
- Set APP_ENV=production and APP_DEBUG=false.
6. Set Up the Database
- Create a new database in the Hostinger control panel under Databases.
- Import your Laravel database backup using phpMyAdmin or the Hostinger database import tool.
- Import your Laravel database backup using phpMyAdmin or the Hostinger database import tool.
php artisan migrate
7. Install Node.js Dependencies
- Run npm install and npm run prod locally.
- Upload the compiled public/css and public/js directories to the server.
8. Set Up .htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
9. Final Checks
- Test the application in your browser.
- Verify the front-end assets and routes are working correctly.
- Check for any errors in the storage/logs directory.
10. Optional: Use SSL
Application shows http://[::1]:4000/
in production
Quick Checklist:
- Set
APP_ENV=production
in.env
. - Configure
vite.config.js
withbuild.outDir
set topublic/build
. - Use the
@vite
Blade directive in templates. - Run
npm run build
to generate production assets. - Clear Laravel cache with
php artisan config:cache
. - Verify asset URLs in the browser’s Developer Tools.
1. Verify Vite Configuration
Proper Vite Configuration for Laravel
import { defineConfig } from 'vite';
import laravel from 'laravel-vite-plugin';
export default defineConfig({
plugins: [
laravel({
input: ['resources/css/app.css', 'resources/js/app.js'], // Adjust as per your setup
refresh: true, // Enables live reload in development
}),
],
server: {
host: '0.0.0.0', // Bind to all IPs for development
port: 4000, // Development server port
},
build: {
outDir: 'public/build', // Production assets go here
},
});
Key Points:
- The
build.outDir
specifies where production assets are output. For Laravel, it should be inpublic/build
. - The
server.port
is only used during development, so it won’t affect production if everything is configured correctly.
2. Build for Production
Run the production build command to generate optimized assets:
npm run build
3. Update Laravel Blade Files
Ensure your Laravel views reference the production assets instead of the development server. If you’re using Vite with Laravel, make sure you include assets properly:
Blade Syntax for Including Vite Assets, in your Blade views (e.g., resources/views/layouts/app.blade.php
):
@vite(['resources/css/app.css', 'resources/js/app.js'])
- Laravel will automatically load the correct assets based on the environment (
APP_ENV=production
for production). - If your environment is set to
APP_ENV=local
, Laravel will try to load from the development server (hencehttp://[::1]:4000
).
4. Set Up the .env
File
Ensure your .env
file is correctly configured for production:
APP_ENV=production
APP_DEBUG=false
APP_URL=https://yourdomain.com
# Optional Vite-related variables
VITE_ASSET_URL=/build
5. Clear and Cache Laravel Configurations
After updating .env
and Blade files, clear and cache the Laravel configurations:
php artisan config:clear
php artisan view:clear
php artisan route:clear
php artisan cache:clear
php artisan config:cache
6. Double-Check Deployment
- Ensure Static Files are Deployed: Verify that the
public/build
directory has been uploaded to your server if you’re using FTP or similar deployment methods. - Use a Reverse Proxy: If your Node.js app handles certain routes (e.g., WebSocket), ensure your web server (e.g., Nginx or Apache) is correctly proxying those routes while serving Laravel for others.
7. Prevent Development Server URLs
If http://[::1]:4000
still appears, it means the application is falling back to the development server because:
- Vite’s Dev Server is Running: Ensure
npm run dev
is not running on your production server. - Asset URLs are incorrect: Laravel tries to use the Vite development server if it thinks it’s in a development environment.
Force Laravel to Use Production Assets
In your vite.config.js
, add the following:
export default defineConfig({
plugins: [
laravel({
input: ['resources/css/app.css', 'resources/js/app.js'],
refresh: true,
}),
],
server: {
strictPort: true, // Avoid port fallbacks
},
});
8. Debug Using Laravel Logs
If the problem persists, check Laravel logs to ensure there are no errors in the asset paths:
tail -f storage/logs/laravel.log
9. Remove Hardcoded http://[::1]:4000
URLs
If there are hardcoded references to http://[::1]:4000
in your app:
Search the codebase for this string and replace it with environment-driven URLs:
const BASE_URL = process.env.NODE_ENV === 'production'
? '/build'
: 'http://localhost:4000';