What are Modes in Magento 2?
Modes are different settings in Magento 2 that affect the behavior of the application depending on the environment it is in.
The complexity of the Magento 2 application makes it troublesome to maintain performance while catering to the different ways users are using the application.
Developers want to constantly change the application through the use of custom modules.
Merchants want the best possible speed.
So how can we accommodate these different use cases?
What Modes Are Available?
There are 3 main modes in Magento 2.
What Does Each Mode Do?
Production mode is the mode that should be set in all production, staging, testing, and integration environments for your site.
Production mode sets the application to behave in the most performant way possible. It does not attempt to evaluate and create dynamic classes on the fly. It instead assumes all classes exist in the places they should belong. It looks for them in those places and creates them. If it is not present, it will fail.
Whenever an error occurs in production mode, a generic error is displayed on the screen. This is for security reasons. It will give a generic error message and a report number. You can find the details of the error in the file with this number inside of your var/report/ folder.
Developer mode is the mode that is used on a local machine. It is used for active development of new features.
Developer mode is the least performant of the modes. It will attempt to resolve classes by looking in their respective locations. If a class does not exist, however, it will attempt to create that class before it is returned. This leads to a fair amount of overhead generating these classes.
Whenever an error occurs in developer mode, all details are given on screen. It is for this reason that developer mode should never be set on an environment with any sort of access to the public.
Default mode is the mode that Magento 2 begins in when you first install the application. This is the ‘worst’ of the 3 modes.
Default mode’s intention is to have the features necessary to run the application on a server with limited access to advanced features such as command line. This is common among shared and economy hosting platforms.
When in Default mode, the application will still attempt to try and dynamically create all classes if it cannot find them. This causes slow performance while this process is taking place.
When in Default mode, no errors or warnings are displayed on screen. This is for security reasons.
How Do You Set The Mode in Magento 2?
The mode in Magento 2 can be set in 3 different ways.
- Command Line
- Environment Variables
The first way is through the command line.
To see what mode you are in, enter the following:
php bin/magento deploy:mode:show
This should show you what mode you are in.
To set your mode, enter the following:
php bin/magento deploy:mode:set developer
This will set your mode from production or default to developer mode.
To set your application to production mode, enter the following:
php bin/magento deploy:mode:set production
This will set your application to production mode. It will also kick off the compilation process and compile all of your generated code, preprocessed CSS, and assets in the pub/static/ folder. If you do not wish to run this compilation when you set production mode, you can specify a skip-compilation flag.
php bin/magento deploy:mode:set -s production
the -s flag will skip the compilation.
It’s important to note that once you enter production or developer mode from default, the application will not let you go back into default mode.
If you want to set the mode and don’t want to use (or don’t have access to) the command line, you can edit the app/etc/env.php file and set the mode manually. Be careful, as this file can be overwritten by other processes in Magento 2.
To set the mode in the file, open app/etc/env.php and locate the following:
'x-frame-options' => 'SAMEORIGIN',
'MAGE_MODE' => 'production',
'save' => 'files',
Edit the MAGE_MODE setting with your mode value. Clear your caches and refresh your site to have the settings take place.
If you set the mode to production this way, you will need to compile your site via other means because the application will think it’s already been done.
The final way to set the mode in your application is to do it on the server level. Magento accepts environment variables and passes them to the application. So if you set the MAGE_MODE variable inside of Apache or NGINX, it will apply that mode to the application.
For example, in an Apache hosts file:
SetEnv MAGE_MODE production
Now you know what the modes are in Magento 2 and how to set them!