Deploying Laravel
Deploying a Laravel application to production requires proper configuration. The default settings are fine for development but you should optimize everything before going live.
A well-deployed Laravel app is fast, secure, and reliable. Let me walk you through the essential steps.
Production Configuration
Set your application to production mode in the .env file. This disables debug mode and enables caching for better performance.
APP_ENV=production
APP_DEBUG=false
APP_URL=https://yourdomain.com
Never leave APP_DEBUG=true in production. It can expose sensitive information about your application to attackers.
Make sure your database, cache, and queue connections are configured correctly for production.
Optimizing Your Application
Laravel provides several optimization commands that dramatically improve performance in production.
composer install --optimize-dev --no-dev
php artisan config:cache
php artisan route:cache
php artisan view:cache
php artisan event:cache
The config:cache command merges all configuration files into a single file. route:cache precompiles your routes for faster lookups. view:cache compiles all Blade templates.
The --no-dev flag excludes development dependencies, making your production build smaller and faster.
Queue Workers
If your application uses queues, you need a queue worker running in production. The worker listens for jobs and processes them in the background.
php artisan queue:work --sleep=3 --tries=3
The --sleep flag determines how long the worker sleeps when no jobs are available. The --tries flag sets how many times a failed job will be attempted before being released to the failed jobs table.
You should use a process monitor like Supervisor to keep your queue workers running at all times.
Supervisor and Process Management
Supervisor is a process control system that runs on Linux. It automatically restarts your queue workers if they crash.
[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /path/to/your/app/artisan queue:work --sleep=3 --tries=3
autostart=true
autorestart=true
user=www-data
numprocs=2
redirect_stderr=true
stdout_logfile=/path/to/your/app/storage/logs/worker.log
With Supervisor, you do not need to worry about workers dying. It monitors them and brings them back to life automatically.
Laravel Forge
Laravel Forge is a server management tool that simplifies deployment. It handles Nginx, PHP, queue workers, and SSL certificates for you.
Forge connects to your server and configures everything automatically. You can deploy with a single button push from GitHub or GitLab.
Forge also provides monitoring and notifications so you know when something goes wrong. It is a great investment for production Laravel applications.