Skip to main content

Magento 2 allows you to put your online store in maintenance mode. It informs customers about your Magento website maintenance and avoids giving an unpleasant experience. Furthermore, you can set custom maintenance mode notifications to encourage customers to come back later. Thus, read on to learn how to enable Magento 2 maintenance mode!

What is Magento 2 maintenance mode?

What is Magento 2 maintenance mode?

Magento 2 maintenance mode is a state where your Magento website is off bootstrapping. When Magento is in maintenance mode, your visitors will see the message Service Temporarily Unavailable instead of your Magento eCommerce website in their browser. Magento detects maintenance mode with Magento maintenance.flag, which means that:

  • If var / .maintenance.flag doesn’t exist: Maintenance mode will turn off, and Magento will work normally.
  • If var / .maintenance.flag exists: Maintenance mode is on, except where var / .maintenance.ip is present.

In which, var / .maintenance.ip contains a list of IP addresses. If an HTTP or IP address enters the entries in that list, the maintenance mode is turned off. In other words, authorized IP addresses can still reach your website normally. Therefore, this feature allows you to test and develop your website before launching an online store or when you want to perform other maintenance tasks such as installing updates or reconfiguring your website.

Things you need before configuring Magento 2 maintenance mode

Things to prepare before setting up Magento 2 maintenance mode

Typically, your Magento app or website builder will perform all these Magento maintenance services for you. However, if you’re building yourself and about to go live, now is the time to prepare!

  • First, you have to log in to your Magento server with written permissions to the file system like a web server account.
  • Then, add the below command to your system path to run Magento commands from any directory:
<your Magento install dir> /bin
1
<your Magento install dir> /bin

Other ways to run commands:

  • Use 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 your web server’s dock root.

How to enable or disable Magento website maintenance

How to enable or disable Magento 2 maintenance mode

Step 1: Choose the correct working directory

Before you can enable Magento 2 maintenance mode, follow these steps:

  • Login to your SSH account
  • Enter this command at the command prompt: cd ~/public_html

If you’ve installed Magento 2 in a subdirectory, you need to point to that directory. For example, if you’ve installed Magento in the subdirectory named mag1, you should type:
cd ~/public_html/mag1

Step 2: Allow an IP address to access your store

To allow Magento storefront access from a specific IP address while your website is in maintenance mode, enter the command:

bin / magento maintenance: allow-ips xxx.xxx.xxx.xxx

In which, xxx.xxx.xxx.xxx is the IP address you want to allow access to.

Example 1: To enable Magento website maintenance with no IP address exemptions:

bin/magento maintenance:enable

Example 2: To 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
  • To remove multiple IP addresses in the same command, you can use the --ip=<ip address> option multiple times.
  • To remove the list of exempt IPs, you can use magento maintenance: enable --ip=none.

If you don’t know your IP address, you can go to IPfinder.

Step 3: Enable maintenance mode in Magento 2

To enable Magento website maintenance for a store, enter the following command:

bin/magento maintenance:enable

Then, all visitors will then receive a Service Temporarily Unavailable message in their web browser. However, if you access Magento from an authorized IP address, the store will display normally.

If you have multiple stores with different localization and layout, pass the $_GET['skin'] parameter to the intended processor. After that, make sure you have Nginx installed for those stores.

Step 4: Use maintenance template file for localized content

An example of maintenance mode layout template

For localized content, you can use a 503 type error template file. For example, the constructor of the Error_Processor class accepts a GET skin 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

In addition, you should check if Magento maintenance.flag exists or not before using this skin parameter. 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 or not
  • Set parameters using the rewrite rules below. Here are some examples of rewrite 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 the following files:

  • pub / error / default / 503.phtml to pub / error / sub / 503.phtml
  • pub / error / default / css /styles.css to pub / error / sub / styles.css

Then, you can edit these files to provide localized content in the 503.phtml file and custom styles in the styles.css file.

Ensure your paths point to your errors directory, and the directory name matches the URL parameter specified in the RewriteRule. In the previous example, a sub directory is used, specified as a parameter in the RewriteRule (skin = sub).

Note: You must add the Nginx setting for multi-store setups.

Step 5: Check Magento 2 maintenance status

You can review your Magento website maintenance mode status at any time. Enter the following command:

bin/magento maintenance:status

Step 6: Turn off maintenance mode in Magento 2

Disable Magento 2 maintenance mode

To turn off maintenance mode and re-enable the store for all visitors, enter the following command:

bin/magento maintenance:disable

You can double-check if var / .maintenance.flag is present or not. Remember, your Magento website is working properly (maintenance mode is off) when the magento maintenance.flag doesn’t exist.

Final thoughts

Now you understand how to work with maintenance mode in Magento 2. There are many ways to enable or disable Magento 2 maintenance mode, so use what you are most comfortable with.

In addition, you can find a partner who provides Magento maintenance services to help you with this. Then, you won’t be bothered with complicated technical tasks to focus more on your business.

Irene Luong

Author Irene Luong

More posts by Irene Luong

Leave a Reply