Magento 2: Maintenance Mode

The question:

I’ve configured Magento 2 website as maintenance mode from .maintenance.flag it’s working, at the same time I have configured released IPs from .maintenance.ip file.

For multiple IPs not working while for single IP it’s working fine.

How can I configure to release selected multiple IPs?

For this I have written I have created.

1) .maintenance.flag file from var.

2) .maintenance.ip file from var and written code for release single IP

192.168.0.52

For single I have written above it’s working.

192.168.0.52,192.168.0.53

For multiple, I have written above it’s not working.

Can you please let me know how can I write to release multiple IP’s and also how can I configure to redirect particular page while maintenance Mode?

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

For activating maintenance mode with IP addresses execute below command in CLI

php bin/magento maintenance:enable --ip="192.168.0.52" --ip="192.168.0.86"

the above command will automatically create a .maintenance.flag and .maintenance.ip file under root/var folder.

and .maintenance.ip the file contains the above two IP addresses separated by a comma.

For more Information, you can find from devdocs.

The above information is for maintenance mode.

To redirect a custom page while maintenance mode writes below code in root/index.php.

$maintenanceFile =  __DIR__ . '/var/.maintenance.flag';

if (file_exists($maintenanceFile)) {
    header('Location: http://127.0.0.1/m2ee/Error.php');
    die();
}

If anyone has better approach please update this answer.

Method 2

The Maintenance Mode check-in lib/internal/Magento/Framework/App/Bootstrap.php.
This file calls to lib/internal/Magento/Framework/App/MaintenanceMode.php. If .maintenance.flag file exists in var folder, it returns false.

If you want the redirect to a new page when maintenance enables, you need change lib/internal/Magento/Framework/App/Bootstrap.php file. Add this code below after line 288:

header('Location: http://127.0.0.1'); //your page
die();

Method 3

If you want to design your maintenance page you need to edit file pub/errors/default/503.phtml and for css pub/errors/default/css/styles.css design accordingly.

And in .htaccess page need to comment ## SetEnv MAGE_MODE developer or production for execute this maintenance page.

and if you are in production mode then this page will excute other then below code error will show.

Unable to proceed: the maintenance mode is enabled. 
#0 /backup/html/magento2/vendor/magento/framework/App/Bootstrap.php(256): MagentoFrameworkAppBootstrap->assertMaintenance()
#1 /backup/html/magento2/index.php(39): MagentoFrameworkAppBootstrap->run(Object(MagentoFrameworkAppHttp))
#2 {main}

if you want to come back in your website you need to delete var/.maintenance file or run below command in terminal

//For run your website back online.
php bin/magento maintenance:disable  
//For on maintenance mode.
php bin/magento maintenance:enable 

Also set in .htaccess
SetEnv MAGE_MODE developer

follow up my youtube and block

https://www.youtube.com/channel/UChb7DM9SspzrUVh4hnWL50A

https://php-scripts-form.blogspot.com/


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment