# Getting Your Laravel Application Ready For Production

Published 19 November 2024

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:
```bash
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:
```bash
php artisan optimize
```

This command combines several optimizations, including configuration, routes, views, and events. Alternatively, you can run these commands individually if needed:
```bash
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:
```bash
php artisan icons:cache
```

For Filament users, optimize your Filament assets and configuration with:
```bash
php artisan filament:optimize
```

---

## Database Preparation

**Run Migrations**

Ensure all migrations are applied to your production database:
```bash
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](https://pulse.laravel.com/) 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:
```bash
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](https://sorane.io) 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:

```bash
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.

Read this post in a browser: https://ranetrace.com/blogs/getting-your-laravel-application-ready-for-production
