How Magento 2 Deployment Modes Can Impact Your Site
As a Magento user, you may have wondered what the different deployment modes do, and if switching to a different deployment mode could help optimize your site. The different modes are optimized for different uses, so it may merit a second look!
A Brief History of Magento Compilers
The various modes and compilation implementations included with the eCommerce CMS have changed over the years and across versions. Previously in Magento 1.x, a user would only need to utilize the Compiler in environments where PHP accelerators were not available.
As processing power increased in speed and the parsing of PHP code improved, the Magento 1.x model was no longer applicable to newer versions of PHP (especially PHP 7.x).
In Magento 2, the compiler was moved to a new roll and Deployment modes were created to help streamline the development, production, and general CMS installation processes. In the current release of Magento 2, the following deployment modes are available: Default, Developer, and Production. Each of these have a specific use case intended as outlined below.
As the name implies, this is the original mode when initially installed. This mode allows you to deploy the Magento application on a single server without changing any settings. Default Mode is not as optimized for production as the actual Production Mode is.
- This mode logs errors but does not visibly display them to the user. This can lead to unexpected errors when moving to Production Mode after a long period of time.
- Symbolic links to the static view files are published to ‘pub/static’ for each requested file.
- Default Mode is not optimized for production environments, primarily because of the adverse performance impact of static files being dynamically generated rather than materialized. This means that creating static files and caching them has a greater performance impact than generating them using the status files creation tool.
The intended use case for this mode is when you are extending or customizing the installation, such as changing themes or adding extensions and products.
- In Developer Mode, symbolic links are used for static file views in the same way as Default Mode. This places the files in ‘pub/static’ for each requested file.
- Uncaught exceptions display in the web browser, giving the user a more direct method of error reporting for the store.
- System logging is enabled and written verbosely to ‘var/report’.
- An exception is thrown in the error handler, rather than being logged.
- An exception is thrown when an event subscriber cannot be invoked.
Production mode is intended for deployment on a production system. When running Magento 2 in Production mode, the following should be noted:
- Exceptions are not shown to users but are written to the exception log in ‘var/report’
- Static view files are not materialized – URLs for them are composed on the fly rather than fallback mechanisms. (Serves static view files from Cache only)
- Prevents automatic code file compilation. New or updated files are not written to the file system.
- Does not allow you to enable or disable cache types in the Magento admin.
You should run Magento 2 in Production Mode when it has been deployed to your production server. After optimizing the site and extensions, you should run the “static view files” deployment tool which will write static files to ‘pub/static’ for ALL files with the installation rather than relying on their request.
How to Change Deployment Mode
You may change the current Magento deployment mode with access to SSH and the installation directory.
Once you have logged into SSH, navigate to the installation directory. If you are utilizing our shared hosting or a dedicated/cloud solution, this is generally located in /home/$username/public_html/. You can locate a particular directory with the list command “ls” to see if there is a public_html directory (or directory with the domain name which you are seeking).
After locating the directory you wish to check, the “cd” command will move you to the location. In our case, cd public_html. Next we will utilize “ls” once more to verify the file structure matches that of Magento. In the below screen capture, you will see the expected Magento file structure which includes an “app”, “var”, “vendor”, and “pub” directory.
As Magento 2 requires PHP 7 or 7.1 we will now want to set the current shell sessions PHP version with the following command: source /opt/cpanel/ea-php70/enable
You can now run ‘php -v’ to verify that you are utilizing PHP 7.
Finally, we can check and change deployment modes with the Magento binary as shown below.
Get Current Deployment Mode:
- php bin/magento deploy:mode:show
Change Deployment Mode:
- Production: php bin/magento deploy:mode:set production
- Developer: php bin/magento deploy:mode:set developer
Once you have completed the production mode deployment, it is highly recommend that you verify caching is enabled for your store and the cache is cleaned. This can be accomplished with the following commands:
- Verify Cache is Enabled. A value of “0” is disabled: php bin/magento cache:status
- Enable Cache: php bin/magento cache:enable
- Clean Cache: php bin/magento cache:clean
If you have been utilizing your store in Default Mode, it is expected to see an improvement in site speed after changing to Production Mode and the cache is built (usually within a few hours).
In the event you observe the site slow down, we recommend reviewing the ‘var/log’ directory to see if errors are generated each time the page is loaded. Troubleshooting any errors observed at this point should rectify the issue.