Tuesday, November 19, 2024

Getting Your Laravel Application Ready For Production

Rutger Broerze
Getting Your Laravel Application Ready For Production image

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!


Code Optimization

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

Database Preparation

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.


Security Best Practices

Environment Configuration

Double-check your .env file:

  • Set APP_ENV=production.
  • Disable debug mode: APP_DEBUG=false.
  • Use secure APP_KEY.

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:

  • Content-Security-Policy
  • Strict-Transport-Security

Restrict .env Access

Ensure the .env file is not publicly accessible by adding the following to your web server configuration:

location ~ /\.env {
    deny all;
}

Server Configuration

PHP Settings

  • Enable OPcache for improved performance.
  • Set memory_limit and max_execution_time appropriately.

Monitoring and Error Tracking

Use tools like Sorane to monitor Laravel application errors in real-time and ensure issues don’t go unnoticed.


Laravel-Specific Safeguards

  • Debug Mode: Double check and confirm that APP_DEBUG=false is set in your environment file.
  • Debugbar: Ensure Laravel Debugbar is disabled in production, if you're using that.
  • Permissions: Verify correct permissions for storage and bootstrap/cache directories.

Post-Deployment Checklist

After deploying your application:

  1. Test all critical features.
  2. Monitor logs for errors.
  3. Validate external integrations (e.g., APIs, payment gateways).
  4. Remove maintenance mode:
php artisan up

Conclusion

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.

It takes only one minute to get started

Start monitoring your Laravel applications today

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.