Maintenance mode is a mode in which same error page view will be displayed for all the request sent to the application. This mode is useful when we are making changes in production server.
Laravel official documentation says,
When your application is in maintenance mode, a custom view will be displayed for all requests into your application. This makes it easy to “disable” your application while it is updating or when you are performing maintenance. A maintenance mode check is included in the default middleware stack for your application. If the application is in maintenance mode, a MaintenanceModeException will be thrown with a status code of 503.
Enabling and disabling maintenance mode in Laravel 5 is very simple, you can execute following artisan commands to enable or disable the mode.
To enable mode,
php artisan down
To disable mode,
php artisan up
If we create a view with name 503.blade.php inside resources/view/errors/ directory then enabling maintenance mode will display 503 error page for every request in default.
How to achieve such mode in shared hosting ?
In case of server with shared hosting like cpanel, we might not have ssh access of the server. In such case, we can’t execute above commands directly. However, we can execute these commands from few lines of code.
Let’s create simple get route in which we will execute artisan down and up commands in routes/web.php
$exitCode = Artisan::call('down');
$exitCode = Artisan::call('up');
NOTE: These routes are temporary and we should remove these as soon as maintenance completes.
When we enter /down url, site will be in the maintenance mode and we can make changes in the live server without any issues.
In order to go back in to production mode, simply entering /up route will not work. This is due to application is in maintenance mode and routes will not work and artisan up command code inside route will not work.
What we need to do ?
In order to make application live by switching it in to production mode, we will see how these commands actually works.
Execution of php artisan down command creates a file named down inside storage/framework/ directory. This file is responsible for the maintenance mode. Exection of the php artisan up command will delete down file generated in above processvand application will be in production mode.
Now, In shared hosting we have to do the above process manually. We can switch to the maintenance mode by executing artisan down command from code. In order to switch back to production mode, we have to delete down file at storage/frameworks directory manually.
If we have console ssh access to the server, then we can execute artisan up and down commands easily. This allows us to switch modes easily. However, In the shared hosting we can’t execute the commands directly. So, we can switch to maintenance mode by running artisan down command from code. In order to go back in production mode, we have to delete a down file created at storage/frameworks directory.