Every time you need to do some testing, roll out an update or fix a bug, you need to enable the Magento 2 maintenance mode. It initiates the display of the "Service Temporarily Unavailable" messages across all your website pages.
So, regardless of other Magento modes, the maintenance mode causes the most inconvenience for both merchants and users. Especially if they don't know what that mode is about.
That's why today you'll get all the answers about what is Magento maintenance mode, why it matters and how to use it properly. We'll also share some secrets about enabling maintenance mode in Magento without downtime.
Intrigued? Let's get started!
What is Magento 2 Maintenance Mode?
Magento 2 maintenance mode is a process of temporarily disabling users from accessing your website while you perform updates, customizations, or other improvements. If you enable maintenance mode, website visitors will see a maintenance page with a custom message.
Some exceptions exist when users from certain IPs can access your site as usual, even during this mode.
Magento 2 maintenance mode is marked as the maintenance.flag file.
- The maintenance mode will be disabled and Magento will work as usual if there is no
var/.maintenance.flag
. - The maintenance mode will work if
var/.maintenance.flag
exists (and there are no excluded IPs). - The maintenance mode will work as usual for the list of IP addresses marked in
var/.maintenance.ip
.
Before Enabling Magento Maintenance Mode
Before you enable maintenance mode in Magento, you need to take a few things into consideration:
- You need to have access to the Magento 2 admin panel and administrator rights to work with maintenance mode.
- We recommend you back up Magento to prevent data from being lost if anything goes wrong during the maintenance setup.
- Prepare a maintenance page design to replace the default "Server Temporarily Unavailable" message and display relevant information to your visitors.
- To upload a maintenance page, modify files, or make other changes, maintain access to your store files via FTP or SSH.
- Set your store to developer mode so that cached content does not affect the display of the maintenance page.
- Create a maintenance schedule. Set precise start and end times for maintenance and find a way to notify your customer about it. For example, send out emails, post news on social media, or post a message on the website that technical work is about to take place.
- Research when user activity on your site is lowest. Your website will be temporarily down, so you don't want to interfere with your customers' search and purchase processes. We'll share how to avoid this later on.
Well-planned actions will help to maintain your website easily and create the best conditions with minimal risk.
Enable/Disable Magento Maintenance Mode
To enable maintenance mode in Magento follow a set of specific steps and Magento bin commands.
1. Choose the directory
Log into your SSH account and go to the directory where Magento is installed. Enter this line at the command prompt:
cd ~/public_html
If you’ve installed Magento 2 in a subdirectory, point to that directory like this:
cd ~/public_html/example
2. Allow access to your store
To allow access to your website storefront to a specific IP address, run the following command (xxx.xxx.xxx.xxx is the IP address you want to allow access to):
php bin / magento maintenance:allow-ips xxx.xxx.xxx.xxx
If you want to add several IP addresses, separate them with a comma.
php bin/magento maintenance:allow-ips xxx.xxx.xxx.xxx xxx.xxx.xxx.xxx
If you want to enable maintenance mode with no IP address exemptions, run:
php bin/magento maintenance:enable
To enable maintenance mode for all clients except xxx.x.x.xx and xxx.x.x.xx, run:
php bin/magento maintenance:enable --ip=xxx.x.x.xx --ip=xxx.x.x.xx
To remove several IP addresses in the same command, you can use this option multiple times
--ip=<ip address>
To remove the list of excluded IPs, use this command:
php bin/magento maintenance:enable --ip=none
3. Enable maintenance mode in Magento 2
To enable the mode, run this command:
php bin/magento maintenance:enable
If you have done everything correctly, visitors will receive a “Service Temporarily Unavailable” message in their web browser (except for excluded IP addresses).
You can also create a maintenance template to replace this error message.
4. Use maintenance template (optional)
You can add a customized template to avoid scaring users off with a maintenance message. For example, you can replace the background image or message, and even give a notification when to expect the site to be back.
In this example, we are using a 503 error template file for localized content.
The constructor of the Magento\Framework\Error\Processor class accepts a skin GET parameter to change the layout:
if (isset($_GET['skin'])) {
$this->_setSkin($_GET['skin']);
}
You can add this rewrite rule in your .htaccess file to add a skin parameter to the URL.
$_GET['skin'] parameter
So, check if the .maintenance.flag exists. Then, note the server address, a reference to HTTP_HOST, or any other variable, such as the ENV variable. Check if the skin parameter exists.
Then, you should set the parameter by one of these rules:
RewriteCond% {DOCUMENT_ROOT} /var/.maintenance.flag -f
RewriteCond% {HTTP_HOST} ^ sub.example.com $
RewriteCond% {QUERY_STRING}! (^ | &) Skin = sub (& | $) [NC]
RewriteRule ^% {REQUEST_URI}? Skin = sub[L]
Copy file 1 into 2:
pub / error / default / 503.phtml
pub / error / sub / 503.phtml
And these:
pub / error / default / css /styles.css
pub / error / sub / styles.css
Then make the necessary changes to these files to provide localized content in the 503.phtml file and custom styles in the styles.css file.
Check that your paths point to the errors directory and that the directory name matches the URL parameter specified in the RewriteRule. The previous example uses a sub directory, as a parameter in the RewriteRule (skin = sub).
4. Check Magento 2 maintenance mode status
To check the maintenance mode status on your website, run:
php bin/magento maintenance:status
5. Disable maintenance mode
Run the following command to bring the website back to life:
php bin/magento maintenance:disable
Avoid Downtime During Maintenance Mode
Unfortunately, you can't avoid the downtime when uploading any changes in default Magento. That's why you often resort to inconvenient deployment times when there are little to no users in your store.
Not very user-friendly, for developers in any case. But there is a solution —
.It helps you to achieve zero downtime deployment with only one command:
bin/magento magefan:zero-downtime:deploy
But there's more. You achieve zero downtime when performing other development tasks — installing extensions, changing Magento modes and others. All these with or without Git. There's a whole set of zero downtime deployment commands for that.
This opens a lot of opportunities for developers to improve the development process. You can now fix bugs and make changes to your store without compromising the user experience.