Launching a Laravel application in production requires more than just moving your code to a live server. It’s about ensuring security, performance, and stability. This guide covers the essential steps to prepare your Laravel app for a smooth and professional deployment. Let’s dive in!
Minimize Dependencies
Remove any unused packages from composer.json to reduce the size of your application and improve performance. Once cleaned, run the following command to install only production dependencies:
composer install --optimize-autoloader --no-dev
Optimize Laravel Files
Laravel provides powerful caching commands to significantly improve performance. Run the following command as part of your deployment process:
php artisan optimize
This command combines several optimizations, including configuration, routes, views, and events. Alternatively, you can run these commands individually if needed:
php artisan config:cache
php artisan route:cache
php artisan view:cache
php artisan event:cache
If you’re using Blade Icons, be sure to cache them as well:
php artisan icons:cache
For Filament users, optimize your Filament assets and configuration with:
php artisan filament:optimize
Run Migrations
Ensure all migrations are applied to your production database:
php artisan migrate --force
The --force flag skips the confirmation step to avoid issues during automated deployments.
Add Indexes
Review your database schema and add indexes to columns that are frequently queried to improve performance. Use database monitoring tools like Laravel Pulse to identify slow queries.
Environment Configuration
Double-check your .env file:
Secure Cookies
Laravel automatically encrypts cookies, but ensure SESSION_COOKIE_SECURE=true if your application uses HTTPS.
Set Secure Headers
Use the Header middleware to add headers such as:
Restrict .env Access
Ensure the .env file is not publicly accessible by adding the following to your web server configuration:
location ~ /\.env {
deny all;
}
PHP Settings
Use tools like Sorane to monitor Laravel application errors in real-time and ensure issues don’t go unnoticed.
APP_DEBUG=false
is set in your environment file.After deploying your application:
php artisan up
Preparing a Laravel application for production might seem daunting, but following these best practices ensures your app is secure, fast, and reliable.
Once deployed, you can never be certain that you did everything right. Issues will always arise, so don’t forget to monitor your application with tools like Sorane to catch any issues and know about them the moment they happen.
Websites will go down, and errors will happen. With Sorane, you are sure to catch any errors and performance issues the moment they happen.
Takes 1 minute to get started.
No credit card required. Cancel anytime.