How to Deploy Laravel on Hostinger Shared Hosting

Deploy a Laravel app to Hostinger shared hosting: keep the app outside public_html, set up MySQL, edit index.php, then migrate over SSH.

Mahzaib MirzaMahzaib MirzaJune 15, 20223 min read2 comments
How to Deploy Laravel on Hostinger Shared Hosting

To deploy Laravel on Hostinger shared hosting, keep the application in its own folder next to public_html, copy only the contents of Laravel's public folder into public_html, and point three paths in index.php at the application folder. Then create a MySQL database in hPanel, fill in .env, and run the migrations. This keeps .env and your code out of the public web root.

I checked these steps against Hostinger's support articles and the Laravel 12 documentation and source. I did not run them on a live Hostinger account, so treat the folder layout as a well-reasoned starting point and check the result in your own browser.

Before you start

  • PHP version. Laravel 12 needs PHP 8.2 or newer, plus the extensions listed in the Laravel deployment docs (Ctype, cURL, DOM, Fileinfo, Filter, Hash, Mbstring, OpenSSL, PCRE, PDO, Session, Tokenizer, XML). Hostinger lets you pick 8.2 to 8.5.

  • SSH and Composer. Hostinger lists SSH access for Premium Web plans and above, and pre-installed Composer for Premium Web, Business and Cloud plans. On a plan without them you build the vendor folder on your own computer and upload it. Check your plan in hPanel.

  • The web root is fixed. Hostinger says the home directory cannot be changed on its Web, WordPress and Cloud plans, so you cannot point the domain at Laravel's public folder.

Why not upload everything into public_html?

Many tutorials do exactly that. Laravel's docs warn against serving the application from the project root, because it exposes sensitive configuration files to the internet. Your .env holds the database password and the app key, so keep it outside public_html.

Step 1: choose the PHP version

In hPanel go to Websites, Manage site, then PHP Configuration. Pick 8.2 or newer and click Update. The change takes a couple of minutes and applies to every site on the plan.

Step 2: prepare the project on your computer

composer install --no-dev --optimize-autoloader
npm run build

Building here means you do not need Node.js on the server. Hostinger's Node.js documentation describes deploying Node apps on Business and Cloud plans, not running npm during a PHP deploy, so building locally is the safe route. Leave out node_modules, .git and your local .env when you upload.

Step 3: upload the files

Hostinger's root for a domain looks like this over SSH: /home/u123456789/domains/yourdomain.com/public_html. Create a folder named laravel next to public_html and upload the project into it with File Manager or SFTP. Then copy what is inside your local public folder into public_html:

domains/yourdomain.com/
  laravel/          <- app, bootstrap, vendor, storage, .env ...
  public_html/      <- contents of Laravel's public folder
    index.php
    .htaccess
    build/

Copy .htaccess too. It is a hidden file and is easy to miss, and without it only the home page works.

Step 4: edit public_html/index.php

The default file loads three things relative to its own folder: the maintenance file, the Composer autoloader and bootstrap/app.php. Add /../laravel to each path:

if (file_exists($maintenance = __DIR__.'/../laravel/storage/framework/maintenance.php')) {
    require $maintenance;
}

require __DIR__.'/../laravel/vendor/autoload.php';

$app = require_once __DIR__.'/../laravel/bootstrap/app.php';

Leave the rest of the file as it is.

Step 5: tell Laravel where its public folder is

With this layout, Laravel's default public path (laravel/public) does not exist on the server. That affects php artisan storage:link. At the bottom of laravel/bootstrap/app.php, call usePublicPath() on the application object, only when the folder exists so your local setup keeps working:

$app = Application::configure(basePath: dirname(__DIR__))
    // ...routing, middleware, exceptions...
    ->create();

$publicHtml = dirname(__DIR__, 2).'/public_html';

if (is_dir($publicHtml)) {
    $app->usePublicPath($publicHtml);
}

return $app;

The reasoning behind this call is in how to change Laravel's public folder to public_html.

Step 6: create the database

In hPanel go to Websites, Manage site, then Databases and Management. Enter a database name, a username and a password, and click Create. Hostinger adds a fixed prefix such as u123456789_ to the names, and the password needs at least eight characters with a number, an uppercase letter and a lowercase letter. The hostname for these databases is localhost.

Step 7: write the .env file

Create laravel/.env on the server. Laravel's example file defaults to SQLite, so set the connection to MySQL:

APP_NAME="My App"
APP_ENV=production
APP_KEY=
APP_DEBUG=false
APP_URL=https://yourdomain.com

DB_CONNECTION=mysql
DB_HOST=localhost
DB_DATABASE=u123456789_app
DB_USERNAME=u123456789_user
DB_PASSWORD=your-database-password

Generate the key on your computer with php artisan key:generate --show and paste it into APP_KEY. Laravel's docs say APP_DEBUG should always be false in production, and that .env should not be committed to source control.

Step 8: run the commands over SSH

Enable SSH in hPanel under Websites, Manage site, SSH Access, then connect with the details shown there. Run php -v first to make sure the command line uses the PHP version you chose. Then, from the laravel folder:

cd domains/yourdomain.com/laravel

php artisan migrate --force
php artisan storage:link
php artisan optimize

If you did not upload vendor, run composer2 install --no-dev --optimize-autoloader first. Hostinger uses the name composer2 for Composer 2 on PHP 8.0 and newer.

The web server user must be able to write to storage and bootstrap/cache, as Laravel's docs require. If you get permission errors, fix them in File Manager or over SSH.

No SSH on your plan?

You cannot run Artisan commands on the server, but you can run the migrations from your own computer. Hostinger's Remote MySQL page lets you allow your IP address and shows the server hostname, and its default remote port is 3306. Point a temporary local .env at that database and run php artisan migrate. Remove your IP from the list when you are done.

Symlinks and storage:link

Hostinger says symlinks are disabled by default on Web and Cloud plans and are not supported on Agency plans, so storage:link may fail. You can enable them by following Hostinger's article on enabling disabled PHP functions. The alternative is a disk that writes straight into the public folder, so no link is needed. Add it to config/filesystems.php:

'uploads' => [
    'driver' => 'local',
    'root' => public_path('uploads'),
    'url' => env('APP_URL').'/uploads',
    'visibility' => 'public',
],

Then store files with Storage::disk('uploads').

Changing .env later

After php artisan optimize or config:cache, Laravel stops reading .env. If you edit it, run php artisan config:clear or php artisan optimize again, or your change will be ignored.

Troubleshooting

  • Blank page or HTTP 500. Read laravel/storage/logs/laravel.log. Do not leave APP_DEBUG=true on a live site.

  • Only the home page works. public_html/.htaccess is missing.

  • "Failed to open stream" for the autoloader. The paths in index.php do not match your folder name.

  • A PHP version error. The selected PHP version is older than your Laravel version requires.

  • Database connection refused. Use the full prefixed names and DB_HOST=localhost.

Updating the site later

Re-upload the changed files, then run php artisan migrate --force and php artisan optimize. If you rebuilt assets, upload the new public_html/build folder.

Frequently asked questions

Can I change the document root to Laravel's public folder on Hostinger?

Not on the Web, WordPress and Cloud plans. Hostinger states the home directory cannot be changed there, which is why this guide keeps the app outside public_html.

Which PHP version should I choose for Laravel?

At least 8.2 for Laravel 12. Hostinger offers 8.2 to 8.5, and versions below 8.2 can no longer be selected.

Do I need Node.js on the server?

No. Run npm run build on your computer and upload the build folder.

Share:
Mahzaib Mirza

Written by

Mahzaib Mirza

Software developer & Founder of Coders Vibe.

Related Posts

Liked this post?

Get the next one in your inbox the moment it's published. No spam, unsubscribe anytime.

0 Comments

Leave a comment