intermediate
Step 11 of 15
Deployment
Laravel Framework
Deployment
Deploying a Laravel application to production involves several steps beyond simply uploading files. You need to configure environment variables, optimize caching, set up proper file permissions, configure your web server, set up SSL, run migrations, and implement a deployment workflow. Laravel provides several tools for deployment, from the simple (shared hosting with SSH) to the sophisticated (Laravel Forge, Vapor, Docker containers). Understanding deployment best practices ensures your application runs securely and performs well in production.
Production Optimization
# Optimize for production
composer install --no-dev --optimize-autoloader
php artisan config:cache # Cache configuration
php artisan route:cache # Cache routes
php artisan view:cache # Cache Blade templates
php artisan event:cache # Cache event listeners
php artisan optimize # Run all optimizations
# .env production settings
APP_ENV=production
APP_DEBUG=false
APP_URL=https://yourdomain.com
LOG_CHANNEL=daily
CACHE_DRIVER=redis
SESSION_DRIVER=redis
QUEUE_CONNECTION=redis
# Nginx configuration
server {
listen 80;
server_name yourdomain.com;
root /var/www/myapp/public; # Point to public/ directory
index index.php;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php8.3-fpm.sock;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.(?!well-known) {
deny all;
}
}
Deployment Script
#!/bin/bash
# deploy.sh
set -e
echo "Deploying application..."
# Pull latest code
git pull origin main
# Install dependencies
composer install --no-dev --optimize-autoloader
# Run migrations
php artisan migrate --force
# Clear and rebuild caches
php artisan optimize:clear
php artisan optimize
# Build frontend assets
npm ci && npm run build
# Restart queue workers
php artisan queue:restart
# Restart PHP-FPM
sudo systemctl reload php8.3-fpm
echo "Deployment complete!"
Pro tip: Usephp artisan downbefore deployment andphp artisan upafter to show a maintenance page during updates. Consider using Laravel Forge ($12/month) for automated server provisioning and zero-downtime deployments — it handles Nginx, SSL, PHP, databases, and deployments automatically.
Key Takeaways
- Always run
php artisan optimizein production to cache config, routes, and views. - Set
APP_DEBUG=falseandAPP_ENV=productionin your production.envfile. - Point your web server's document root to Laravel's
public/directory, not the project root. - Use Redis for cache, sessions, and queues in production for better performance.
- Automate deployments with scripts or tools like Laravel Forge, Envoyer, or GitHub Actions.