How to Implement Maintenance Mode in Symfony ≥ 6.4
Putting your Symfony 6.4 (or newer) application into maintenance mode is a smart way to safely perform updates or deployments while preventing users from accessing a broken or unstable version. In this article, we'll explore how to set up maintenance mode in Symfony 6.4 using community bundles, custom logic, and best practices.
Maintenance mode isn’t built into Symfony out of the box, but you can easily add it using community bundles or by writing a simple custom solution. Below are the methods to implement maintenance mode for Symfony 6.4 (or above), plus recommendations and best practices.
1. Using a Maintenance Bundle
One of the most common and reliable ways is to use a bundle that handles maintenance mode for you.
a) ToshY / Lexik Maintenance Bundle
-
Package:
toshy/maintenance-bundlepackagist.org -
Installation:
-
Features:
-
Enable/disable maintenance via console commands (
maintenance:enable,maintenance:disable). packagist.org -
Return a 503 Service Unavailable status code while in maintenance. packagist.org
-
Whitelist IPs: you can configure certain IP addresses to bypass the maintenance mode and still access the site. packagist.org
-
Storage options: supports file-based lock, database, or even memcache. packagist.org
-
-
Configuration example (YAML):
b) Kefisu Maintenance Bundle
-
Package:
kefisu/maintenance-bundlepackagist.org -
Installation:
-
Features:
-
Simple file-based storage of the maintenance state (default in cache or tmp). packagist.org
-
Enable / disable via CLI. packagist.org
-
Customizable response page (you can define how users see the maintenance page). packagist.org
-
-
Configuration example:
c) Drawik Maintenance Mode Bundle
-
Package:
drawik/symfony-maintenance-mode-bundlepackagist.org -
Installation:
-
Usage commands:
-
Configuration options:
-
Configure allowed IPs. packagist.org
-
Define the lock file path (default is
/tmp/maintenance_mode.lock). packagist.org
-
d) FreezLike Maintenance Bundle
-
Package:
freezlike/maintenance-bundlepackagist.org -
Features:
-
Activate/deactivate maintenance mode. packagist.org
-
Option to set a “next maintenance date,” so users know when the site will be back. packagist.org
-
Role-based bypass: allow certain roles (e.g.,
ROLE_ADMIN) to access the site even during maintenance. packagist.org
-
-
Configuration:
2. Custom Approach (Without a Bundle)
If you don’t want to use a third-party bundle, you can build a basic maintenance mode yourself:
-
Use Kernel Event Subscriber
-
Create an event subscriber on
kernel.request. -
Check for a “maintenance” flag. This could be an environment variable, a file in cache, or a database entry.
-
If maintenance is enabled and the request is not from an allowed IP / user, return a Symfony Response with status 503 and a custom maintenance page.
-
Configuration: define the lock file path and allowed IPs in your
services.yamlorparameters.yaml.
-
-
Enable / Disable Maintenance Mode
-
To turn on maintenance: create the lock file (e.g.
touch var/maintenance.flag). -
To turn off: remove the file (
rm var/maintenance.flag). -
This is a very simple but effective mechanism.
-
-
Custom Maintenance Page
-
Create a Twig template (e.g.
templates/maintenance.html.twig) for your maintenance page. -
Use it when returning the 503 Response in your subscriber.
-
3. Best Practices for Maintenance Mode in Symfony
-
Use 503 Status Code: Always return HTTP 503 (Service Unavailable) during maintenance. It tells clients and search engines that downtime is temporary.
-
Graceful Bypass: Allow certain IPs, roles, or users (admins, devs) to bypass to test things during maintenance.
-
Cache Behavior: Be careful with caching. You don’t want proxies or CDNs to cache your maintenance page forever. Use appropriate headers (
Cache-Control: no-store,Retry-After). -
Automate Maintenance During Deployments: Add maintenance enable/disable commands to your deployment script. For example, run
maintenance:enablebefore deployment andmaintenance:disableafter. -
Inform Users: If possible, show a friendly maintenance page with an estimated “back online” time.
-
Health Checks: Even in maintenance mode, health check endpoints (e.g., for load balancers) might need to remain accessible.
Conclusion
For a Symfony 6.4 (or newer) application, using a dedicated maintenance bundle (like ToshY, Kefisu, Drawik, or FreezLike) is the easiest and most reliable way to enable maintenance mode. But if you want full control, implementing a custom subscriber gives you flexibility and avoids third-party dependencies.