How to Deploy Laravel Application: Step-by-Step Guide
To deploy a
Laravel application, upload your project files to a web server, configure the .env file for production, run composer install to install dependencies, and execute php artisan migrate --force to set up the database. Finally, set proper permissions and configure your web server to serve the public directory.Syntax
Deploying a Laravel application involves these main commands and steps:
composer install --optimize-autoloader --no-dev: Installs production dependencies.php artisan migrate --force: Runs database migrations safely in production.php artisan config:cache: Caches configuration for better performance.- Set file permissions on
storageandbootstrap/cachedirectories. - Configure your web server (Apache/Nginx) to point to the
publicfolder.
bash
composer install --optimize-autoloader --no-dev php artisan migrate --force php artisan config:cache
Example
This example shows deploying a Laravel app on a Linux server using SSH and Composer:
bash
# Connect to your server ssh user@your-server-ip # Navigate to your web root cd /var/www/html # Clone your Laravel project git clone https://github.com/yourusername/your-laravel-app.git cd your-laravel-app # Install dependencies composer install --optimize-autoloader --no-dev # Copy example environment file and edit cp .env.example .env nano .env # Generate application key php artisan key:generate # Run database migrations php artisan migrate --force # Set permissions sudo chown -R www-data:www-data storage bootstrap/cache sudo chmod -R 775 storage bootstrap/cache # Cache config and routes php artisan config:cache php artisan route:cache
Output
Application key set successfully.
Migrating: 2024_01_01_000000_create_users_table
Migration completed successfully.
Configuration cached successfully.
Routes cached successfully.
Common Pitfalls
Common mistakes when deploying Laravel include:
- Not setting correct permissions on
storageandbootstrap/cachefolders, causing write errors. - Forgetting to run
php artisan migrate --forcein production, so database is not updated. - Not configuring the web server to serve the
publicdirectory, leading to 404 errors. - Leaving
APP_DEBUG=truein.env, exposing sensitive info.
Example of wrong and right permission commands:
bash
# Wrong: No permission change
ls -l storage
# Right: Set ownership and permissions
sudo chown -R www-data:www-data storage bootstrap/cache
sudo chmod -R 775 storage bootstrap/cacheQuick Reference
Summary tips for Laravel deployment:
- Always use
composer install --no-devfor production. - Run
php artisan migrate --forceto update database safely. - Cache config and routes for performance.
- Set correct permissions on storage and cache folders.
- Configure your web server root to the
publicfolder. - Keep
APP_DEBUG=falsein production.
Key Takeaways
Upload your Laravel app files and set the web server root to the public directory.
Run composer install without dev dependencies and migrate the database with --force.
Set correct permissions on storage and bootstrap/cache folders to avoid errors.
Cache configuration and routes for better performance in production.
Always disable debug mode by setting APP_DEBUG=false in the .env file.