Skip to main content

Deploying a Laravel project to Hostinger shared hosting requires placing your project files outside the public document root and pointing public_html to Laravel’s public directory (or using an .htaccess rewrite rule) to prevent sensitive files like .env from being publicly exposed. Follow this guide to set up your environment, connect your database, and run production commands efficiently.

How to deploy Laravel project on Hostinger shared hosting

1. Preparing Your Local Project & Hostinger hPanel

Before moving any files to production, configure your local environment and prepare your Hostinger environment for deployment.

Step 1: Optimize Your Local Build

On your local computer, open your terminal inside your Laravel project root directory and run these optimization commands to package your routes, config, and views:

Bash

composer install --optimize-autoloader --no-dev
php artisan config:cache
php artisan route:cache
php artisan view:cache

Step 2: Configure PHP and Create the Database on Hostinger

  1. Log into your Hostinger hPanel.

  2. Go to Advanced > PHP Configuration and ensure your PHP version matches your local Laravel requirements (e.g., PHP 8.2 or 8.3).

  3. Enable essential extensions such as mbstring, openssl, pdo_mysql, tokenizer, xml, and curl.

  4. Go to Databases > Management in hPanel.

  5. Create a new MySQL database, database user, and password. Write down these credentials; you will need them for your .env configuration.

2. Uploading Project Files to Hostinger

Hostinger shared hosting uses public_html as the web root. To protect your codebase and sensitive configurations, your core application files must live one level above public_html.

├── /home/u12345678/
│   ├── laravel-app/       <-- Core Laravel files (app, config, routes, bootstrap, etc.)
│   └── public_html/       <-- Only the contents of Laravel's /public folder

Option A: Upload via hPanel File Manager

  1. Zip your local Laravel project (exclude node_modules, .git, and the local vendor folder if you plan to install packages via SSH).

  2. Open Files > File Manager in Hostinger hPanel.

  3. In your root directory (outside of public_html), create a folder named laravel-app.

  4. Upload your .zip file inside laravel-app and extract it.

Option B: Deploy via SSH & Git (Recommended for Developers)

  1. Go to Advanced > SSH Access in hPanel and activate SSH.

  2. Connect to your server using Terminal or PuTTY:

    Bash

    ssh u12345678@your-server-ip -p 65002
    
  3. Clone your GitHub/GitLab repository directly outside public_html:

    Bash

    git clone https://github.com/your-username/your-repo.git laravel-app
    cd laravel-app
    composer install --no-dev --optimize-autoloader
    

3. Web Root Configuration & File Routing

To point web traffic directly to Laravel’s front controller without breaking paths, use one of the following two standard deployment methods:

Method 1: Separate Core Files from public_html (Recommended for Production Security)

  1. Copy all contents from laravel-app/public/ into your main public_html/ folder.

  2. Open public_html/index.php in the hPanel File Manager.

  3. Update the path references to point to your laravel-app directory:

PHP

// Find line 1: Maintenance mode path
if (file_exists($maintenance = __DIR__.'/../laravel-app/storage/framework/maintenance.php')) {
    require $maintenance;
}

// Find line 2: Composer Autoloader path
require __DIR__.'/../laravel-app/vendor/autoload.php';

// Find line 3: Bootstrap Laravel path
$app = require_once __DIR__.'/../laravel-app/bootstrap/app.php';

Method 2: Use Root .htaccess Redirect

If you uploaded the entire application inside public_html/, redirect incoming traffic safely into the public/ directory:

  1. Create a .htaccess file directly inside public_html/.

  2. Paste the following configuration:

Apache

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_URI} !^/public/
    RewriteRule ^(.*)$ public/$1 [L]
</IfModule>

4. Environment Setup & Database Migrations

Step 1: Configure Your Production .env

  1. Inside laravel-app/, copy .env.example to .env.

  2. Update the credentials with your live production details:

Ini, TOML

APP_NAME="Your Application"
APP_ENV=production
APP_DEBUG=false
APP_URL=https://yourdomain.com

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your_hostinger_db_name
DB_USERNAME=your_hostinger_db_user
DB_PASSWORD=your_hostinger_db_password

Step 2: Run Database Migrations and Generate App Key

Via SSH, navigate to laravel-app/ and execute:

Bash

php artisan key:generate
php artisan migrate --force

If SSH is not available on your shared hosting tier:

  • Export your local database via phpMyAdmin into a .sql file.

  • Open Hostinger Databases > phpMyAdmin, select your new database, and click Import.

5. Storage Symlink & Performance Optimization

Link Storage Directory

Laravel requires a symbolic link between storage/app/public and public/storage for file uploads.

If SSH is enabled, run:

Bash

php artisan storage:link

If you encounter symlink permission limitations on shared hosting, create a temporary route in routes/web.php to trigger the symlink directly via browser:

PHP

Route::get('/symlink-setup', function () {
    $target = storage_path('app/public');
    $shortcut = public_path('storage');
    symlink($target, $shortcut);
    return 'Storage symbolic link created successfully!';
});

Visit [https://yourdomain.com/symlink-setup](https://yourdomain.com/symlink-setup) once, then immediately delete this route from your code.

Final Production Caching

Run these commands via SSH to ensure maximum response speed:

Bash

php artisan config:cache
php artisan route:cache
php artisan view:cache

Real-World Benefits of Hosting Laravel on Hostinger

  • Cost-Effective Scalability for Freelancers: Run multiple client staging environments or production web applications at a fraction of VPS costs.

  • Built-In Git & SSH Integration: Automate deployment pipelines directly from your GitHub repositories into shared hosting environments.

  • Free SSL & LiteSpeed Performance: Out-of-the-box HTTPS enforcement paired with LiteSpeed caching ensures fast response times for web applications.

  • Automated Cron Jobs: Easily schedule Laravel background tasks (php artisan schedule:run) directly through Hostinger’s hPanel Cron Job manager.

Exclusive Hosting Offer for Developers & Businesses

Ready to deploy your Laravel applications on high-performance infrastructure? Hostinger provides optimized PHP runtime environments, automated daily backups, free domain registration, and unmetered bandwidth tailored for web developers.

Claim Your Exclusive Discount

Get an EXCLUSIVE 20% DISCOUNT on all hosting packages by using the official referral link below:

Claim 20% Off Hostinger Web Hosting Now

TM

Leave a Reply