How to Deploy Laravel to Shared Hosting: Step-by-Step Guide
To deploy
Laravel on shared hosting, upload your project files excluding node_modules and vendor folders, then run composer install via SSH or locally and upload the vendor folder. Set the public folder as the web root and configure your .env file with database and app settings.Syntax
Deploying Laravel to shared hosting involves these main steps:
- Upload files: Transfer your Laravel project files except
vendorandnode_modules. - Install dependencies: Run
composer installto get PHP packages. - Set public folder: Point your hosting's web root to Laravel's
publicdirectory. - Configure environment: Update
.envwith your hosting database and app settings. - Set permissions: Make
storageandbootstrap/cachewritable.
bash
composer install # Upload all files except vendor and node_modules # Set web root to /public # Update .env with DB credentials # Set permissions: chmod -R 775 storage bootstrap/cache
Example
This example shows how to prepare and upload your Laravel app to shared hosting using FTP and SSH:
bash
# 1. On your local machine, open terminal in your Laravel project folder composer install --no-dev --optimize-autoloader # 2. Zip your project excluding node_modules and vendor (if you want to upload vendor separately) zip -r laravel.zip . -x "node_modules/*" "vendor/*" # 3. Upload laravel.zip to your hosting via FTP # 4. Extract laravel.zip on hosting (via control panel or SSH) # 5. If no SSH access, upload vendor folder from local after running composer install # 6. Set permissions on storage and bootstrap/cache chmod -R 775 storage bootstrap/cache # 7. Edit .env file on hosting with your database and app URL # 8. Set document root to /public folder in hosting control panel
Common Pitfalls
Common mistakes when deploying Laravel to shared hosting include:
- Not setting the web root to the
publicfolder, causing 404 errors. - Forgetting to upload the
vendorfolder or runcomposer install, leading to missing dependencies. - Incorrect file permissions on
storageandbootstrap/cache, causing write errors. - Leaving the default
.envfile without updating database credentials and app URL. - Uploading
node_modulesunnecessarily, which wastes space and time.
bash
# Wrong: Uploading all files including node_modules and not setting public as root # Right: Exclude node_modules, set public folder as root # Wrong permissions example chmod -R 644 storage bootstrap/cache # Right permissions example chmod -R 775 storage bootstrap/cache
Quick Reference
Summary tips for deploying Laravel to shared hosting:
- Always set your hosting document root to the
publicfolder. - Run
composer install --no-dev --optimize-autoloaderbefore uploading or via SSH. - Update your
.envfile with correct database and app URL. - Set write permissions on
storageandbootstrap/cachefolders. - Use FTP or hosting control panel file manager to upload files.
Key Takeaways
Set your hosting web root to Laravel's public folder to serve the app correctly.
Run composer install to get all PHP dependencies before or after uploading.
Update the .env file with your hosting database and app settings.
Set proper write permissions on storage and bootstrap/cache folders.
Avoid uploading unnecessary folders like node_modules to save space.