如何通过Git将Laravel项目部署到Hostinger根目录?共享主机最佳部署方式
Let’s break down your questions one by one with practical, Hostinger-specific solutions that fit shared hosting constraints:
1. Can you deploy Laravel directly to the root directory (/home/uXXXXXXXX)?
Short answer: You technically can, but you shouldn’t. Hostinger’s web server (Apache/Nginx) is configured to serve content from public_html as the default document root for your domain. If you deploy your entire Laravel project to /home/uXXXXXXXX, the web server won’t access those files directly—meaning your site won’t load for visitors.
That said, keeping Laravel’s core files (like app/, config/, .env) in the main directory (outside public_html) is actually the recommended secure practice—you just need to pair it with adjusting your domain’s web root to point to Laravel’s public folder (more on that below).
2. Best Deployment Scheme for Laravel on Hostinger Shared Hosting
The most secure and maintainable approach is to separate Laravel’s sensitive core files from the web-accessible content. Here are two reliable methods tailored to Hostinger:
Method 1: Adjust Domain’s Document Root (Cleanest Option)
If your Hostinger plan supports it (most modern shared plans do):
- Upload your full Laravel project to a new folder in your main directory, e.g.,
/home/uXXXXXXXX/laravel-project. - Go to cPanel → Domains → Manage Domains.
- Find your domain, click Modify, and set the Document Root to
/home/uXXXXXXXX/laravel-project/public. - Save changes, then finish setup with these steps:
- Update your
.envfile to setAPP_URL=https://yourdomain.comandAPP_DEBUG=false. - Run these commands via cPanel’s Terminal or SSH to optimize performance:
php artisan config:cache php artisan route:cache php artisan view:cache - Ensure
storage/andbootstrap/cache/have proper permissions (runchmod -R 775 storage bootstrap/cache—avoid 777 for security).
- Update your
Method 2: Sync Public Files to public_html (If Document Root Adjustment Isn’t Allowed)
If you can’t modify the document root:
- Upload your full Laravel project to
/home/uXXXXXXXX/laravel-project. - Move all files/folders from
/home/uXXXXXXXX/laravel-project/publicto/home/uXXXXXXXX/public_html(overwrite any default files likeindex.html). - Open
/home/uXXXXXXXX/public_html/index.phpand update the paths to point to your core Laravel files:// Replace these default lines: require __DIR__.'/../vendor/autoload.php'; $app = require_once __DIR__.'/../bootstrap/app.php'; // With these adjusted paths: require __DIR__.'/../laravel-project/vendor/autoload.php'; $app = require_once __DIR__.'/../laravel-project/bootstrap/app.php'; - Follow the same
.envconfiguration and permission steps from Method 1.
Git Deployment Automation
To avoid manual uploads, set up automated deployment with Git:
- Create a bare Git repository in your main directory:
mkdir /home/uXXXXXXXX/laravel-repo.git && cd $_ git init --bare - Create a
post-receivescript inside the repo’shooksfolder to handle deployment:
Paste this content (adjust paths to match your setup):nano hooks/post-receive#!/bin/bash # Check out the latest code to your Laravel project folder git --work-tree=/home/uXXXXXXXX/laravel-project --git-dir=/home/uXXXXXXXX/laravel-repo.git checkout -f # Sync public folder to public_html (skip this line if using Method 1) rsync -av --delete /home/uXXXXXXXX/laravel-project/public/ /home/uXXXXXXXX/public_html/ # Clear and rebuild caches cd /home/uXXXXXXXX/laravel-project php artisan config:cache php artisan route:cache - Make the script executable:
chmod +x hooks/post-receive - On your local machine, add the Hostinger repo as a remote:
git remote add hostinger ssh://uXXXXXXXX@yourdomain.com/home/uXXXXXXXX/laravel-repo.git
Now pushing to hostinger main will auto-deploy your project without manual file transfers.
3. Do You Have to Manually Move Folders?
No, manual moving isn’t required. As shown above:
- If you can adjust the document root (Method 1), you don’t need to move any folders at all—just point your domain to Laravel’s built-in
publicfolder. - If you can’t adjust the document root, use the
rsynccommand in a Git post-receive hook to automate syncing thepublic/folder topublic_htmlevery time you deploy.
Manual moving is only necessary for one-time, quick uploads—but automation will save you time and reduce human error in future deployments.
内容的提问来源于stack exchange,提问作者Churrumino

