In this article, I will explain how to use the maintenance mode in Magento 2. Normally it is used to restrict the access to the website during updates of any type, to improve user experience, as they will see a friendly message instead of possibly seeing an error message. That way the customers will know that the shop is not broken.
In Magento 1 this was done by creating a file called maintenance.flag in the root folder. Allowing certain IP Addresses was possible only by editing the index.php file and hard coding them there, however, this was not a standard solution, it was just a workaround.
Magento 2 provides an easier and safe way to do this, without editing any of the core files. All you need to do is to run some commands in the terminal.
To enable the maintenance mode, you need to run the following command in the terminal:
php bin/magento maintenance:enable
In the background, this command will create a file called ".maintenance.flag" in the "var" folder. You can create this manually as well, however, recommended and the best is to do this using the terminal command.
A confirmation message is returned to let you know that it was enabled:
At this point if you'll access the website you'll see the following output:
You can still allow certain IP Addresses to access your website while it is in maintenance mode by running the initial enable command and appending at the end the "--ip=..." argument.
Example to allow only one IP:
php bin/magento maintenance:enable --ip=192.0.2.12
Example to allow multiple IP Addresses:
php bin/magento maintenance:enable --ip=192.0.2.12 --ip=192.0.2.13
The output of the command will confirm that the changes were applied:
The IP Addresses will be saved in a temporary file called ".maintenance.ip" from "var". If the file doesn't exist, it will be created automatically, so you don't have to worry about anything.
Usually, the maintenance mode will be enabled for all the customers and visitors but excluded for the shop owner and developers by adding their IP Addresses in the enable command as explained above.
To disable the maintenance mode, you will need to run the next command:
php bin/magento maintenance:disable
This command will remove var/.maintenance.flag file that was created when the maintenance mode was enabled, and a confirmation message will be returned:
To find out if the maintenance mode is enabled or disabled, or to see the list of whitelisted IP Addresses in case you don't remember, this is the command that you need to run in the terminal:
php bin/magento maintenance:status
This will show you the current status of the maintenance mode:
The standard maintenance page in Magento 2 is quite simple. Soon I will post an article about how to customize the maintenance page.
In case you have questions about maintenance mode do not hesitate to leave a comment.