0
0
LaravelHow-ToBeginner · 4 min read

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 storage and bootstrap/cache directories.
  • Configure your web server (Apache/Nginx) to point to the public folder.
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 storage and bootstrap/cache folders, causing write errors.
  • Forgetting to run php artisan migrate --force in production, so database is not updated.
  • Not configuring the web server to serve the public directory, leading to 404 errors.
  • Leaving APP_DEBUG=true in .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/cache
📊

Quick Reference

Summary tips for Laravel deployment:

  • Always use composer install --no-dev for production.
  • Run php artisan migrate --force to update database safely.
  • Cache config and routes for performance.
  • Set correct permissions on storage and cache folders.
  • Configure your web server root to the public folder.
  • Keep APP_DEBUG=false in 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.