You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Git将Laravel项目部署到Hostinger根目录?共享主机最佳部署方式

Laravel Deployment on Hostinger Shared Hosting: Answers to Your Questions

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 .env file to set APP_URL=https://yourdomain.com and APP_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/ and bootstrap/cache/ have proper permissions (run chmod -R 775 storage bootstrap/cache—avoid 777 for security).

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/public to /home/uXXXXXXXX/public_html (overwrite any default files like index.html).
  • Open /home/uXXXXXXXX/public_html/index.php and 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 .env configuration and permission steps from Method 1.

Git Deployment Automation

To avoid manual uploads, set up automated deployment with Git:

  1. Create a bare Git repository in your main directory:
    mkdir /home/uXXXXXXXX/laravel-repo.git && cd $_
    git init --bare
    
  2. Create a post-receive script inside the repo’s hooks folder to handle deployment:
    nano hooks/post-receive
    
    Paste this content (adjust paths to match your setup):
    #!/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
    
  3. Make the script executable:
    chmod +x hooks/post-receive
    
  4. 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 public folder.
  • If you can’t adjust the document root, use the rsync command in a Git post-receive hook to automate syncing the public/ folder to public_html every 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 09:50:59