Deploy Laravel on Shared Hosting
Step-by-step guide to deploying a Laravel application on cPanel shared hosting
Deploying a Laravel application on shared hosting is different from deploying on a VPS. Shared hosting environments have specific constraints — you don't have root access, and the public directory structure requires special handling.
This guide walks through deploying a Laravel app on a DreamHost shared hosting plan with cPanel.
Prerequisites
Before starting, ensure you have:
- A Laravel application in a Git repository
- SSH access to your hosting account
- Composer installed on your local machine
- PHP 8.1+ available on the server
Step 1: Prepare Your Laravel Application
First, ensure your Laravel application is ready for production:
# Set production environment
cp .env.example .env
# Generate app key
php artisan key:generate
# Optimize
php artisan config:cache
php artisan route:cache
php artisan view:cache
Update your .env with production database credentials.
Step 2: Set Up the Server Directory
SSH into your hosting account:
ssh user@yourdomain.com
Create the directory structure:
# Application root (outside public_html)
mkdir ~/laravel-app
cd ~/laravel-app
# Clone your repository
git clone https://github.com/yourusername/your-app.git .
Step 3: Install Dependencies
# Install composer dependencies
~/bin/composer install --no-dev --optimize-autoloader
# Set proper permissions
chmod -R 755 storage bootstrap/cache
chmod -R 775 storage/logs
Step 4: Configure the Public Directory
The public directory needs to be accessible from public_html. Create a symbolic link:
cd ~/public_html
ln -nfs ~/laravel-app/public/* .
ln -nfs ~/laravel-app/public/.* .
Alternatively, create an alias in cPanel's "Additional Domains" section pointing the document root to ~/laravel-app/public.
Step 5: Set Up the Database
- Log in to cPanel
- Go to MySQL Databases
- Create a new database and user
- Add the user to the database with all privileges
- Update your
.envfile with the database credentials
Step 6: Run Migrations
cd ~/laravel-app
php artisan migrate --force
Step 7: Set Up the Queue Worker
Create a cron job in cPanel:
- Go to Cron Jobs
- Add a new cron job:
* * * * * /usr/local/bin/php ~/laravel-app/artisan schedule:run >> /dev/null 2>&1
Troubleshooting
| Issue | Solution |
|---|---|
| 500 error | Check ~/laravel-app/storage/logs/laravel.log |
| White screen | Enable PHP error display in .env: APP_DEBUG=true |
| Permission denied | Run chmod -R 755 storage bootstrap/cache |
| Composer out of memory | Run ~/bin/composer install --no-dev --memory-limit=512M |
Next Steps
Congratulations! Your Laravel application is now deployed. Next in this series: setting up Laravel Horizon for queue management on shared hosting.