Enable Magento 2 Maintenance Mode
Magento lets you put your store in maintenance mode while you make changes and upgrades to the website. In maintenance mode, the site redirects visitors to a default Service Temporarily Unavailable
page.
We cover the steps to enable and disable the maintenance mode in Magento 2.
Magento 2 Maintenance Mode
Maintenance mode in Magento 2 helps disable bootstrapping. It is useful when you are maintaining, upgrading, or configuring your site.
Magento detects maintenance mode with the Magento maintenance.flag, which indicates:
- If
var/.maintenance.flag
doesn’t exist - Maintenance mode turns off and Magento will work normally. - If
var/.maintenance.flag
exists - Maintenance mode is on, except where var / .maintenance.ip is present.
Here, the var/.maintenance.ip
contains a list of IP addresses. If an HTTP or IP address enters that list, the maintenance mode is turned off.
The authorized IP addresses can still reach your website normally. The feature allows you to test and develop your website before launching. It also lets you perform other maintenance tasks such as installing updates or reconfiguring.
Pre-requisites for Configuring Magento 2 Maintenance Mode
Usually, the Magento app of website builder will perform the Magento maintenance services. If you are working on the site yourself, you must set up some prerequisites.
Login to the Magento server with permissions to the file system, such as a web server account. Enter the command below to your system path to run Magento commands from any directory:
<your Magento install dir> /bin
1
<your Magento install dir> /bin
You can also run the command in the following ways -
Add cd <your Magento install dir> /bin
and run them as ./magento <command name>
Or php <your magento install dir> /bin/magento <command name>
Note: <your Magento install dir>
is a subdirectory of the web server’s document root.
Enable/disable Magento 2 Maintenance Mode.
1. Choose the working directory.
Before enabling Magento 2 maintenance mode, follow these steps:
- Login to your SSH account
- Type the following command at the command prompt:
cd ~/public_html
If Magento 2 is installed in a subdirectory, you need to point to that directory. For example, if you’ve installed Magento in the subdirectory named mag1, you can add:
cd ~/public_html/mag1
2. Allow an IP address to access the store
To allow Magento storefront access from a specific IP address while in maintenance mode, enter the following command:
bin/magento maintenance:allow-ips xxx.xxx.xxx.xxx
Here, the xxx.xxx.xxx.xxx
is the IP address you want to allow access to.
-
Example - Enable Magento website maintenance with no IP address exemptions:
bin/magento maintenance:enable
-
Example - Enable maintenance mode for all clients except 183.0.2.20 and 183.0.2.21:
bin/magento maintenance:enable --ip=183.0.2.20 --ip=183.0.2.21
-
You can remove multiple IP addresses in the same command. Use the
--ip=<ip address>
option multiple times. -
To remove the list of exempt IPs, you can use the command
bin/magento maintenance:enable --ip=none
.
3. Enable maintenance mode in Magento 2
To enable maintenance for a Magento 2 store, add the following command:
bin/magento maintenance:enable
The visitors will receive a Service Temporarily Unavailable
message in their web browser. The store is normally displayed if you can access it from an authorized IP address.
For multiple stores with different localization and layout, pass the $_GET['skin']
parameter to the specific processor. Ensure that you have Nginx installed for those stores.
4. Template file for localized content.
For localized content, you can use a 503 type error template file. The constructor of the r_Processor cla
class accepts a GET skin parameter to change the layout:
if (Isset($_GET['skin']))) {
$this -> _setSkin($_GET['skin']);
}
Add the rewrite rule in your .htaccess file to add a skin parameter to the URL.
$_GET['skin'] parameter
Check whether the Magento exists or not
b exists before using this skin parameter. Note the server address, a reference to HTTP_HOST
, or variables such as the ENV variable.
- Check if the
skin
parameter exists - Set parameters using the rewrite rules below.
Examples of rewrite rules include:
- 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 the files shown below:
-
pub/errors/default/503.phtml
topub/errors/sub/503.phtml
-
pub/errors/default/css/styles.css
topub/errors/sub/styles.css
You can edit these files for localized content in the 503.phtml
file and custom styles in the styles.css
file.
The paths should point to your errors
directory. The directory name should match the URL parameter specified in the RewriteRule
. In the above example, a sub directory is used, specified as a parameter in the RewriteRule (skin = sub)
.
Note: Add the Nginx setting for multi-store setups.
5. Check Magento 2 maintenance status.
You can review your Magento website maintenance mode status. Use the following command:
bin/magento maintenance:status
6. Disable maintenance mode in Magento 2
You can turn off maintenance mode and re-enable the store for all visitors. Use the following command:
bin/magento maintenance:disable
Check if var/.maintenance.flag
is present or not. The Magento website is working properly, that is the maintenance mode is off when the magento maintenance.flag
doesn’t exist.
Summary
You can enable the maintenance mode in Magento 2 to test your site before going live. It is also used for upgrades, bug fixes and configurations. Set a custom maintenance mode page to encourage customers to come back later. You can also allow visitors from authorized IP addresses to view the store normally.
The tutorial shows how to enable or disable the maintenance mode in Magento 2. For more details on Magento, check out MGT Commerce tutorials.