Magento 2 Modes: Why You Should Enable Production Mode on Live Environment

February 11, 2021

Some of the clients running Magento 2, that have come to MageCloud for Magento work, have been operating an M2 site in  Developer   mode. Unfortunately, there isn’t enough awareness about this new feature – Magento 2 Modes. 

 

  • NOTE: Any live Magento 2 site should be in  Production  mode. This mode utilizes full page caching alongside a fully built   pub  folder to pull from, and is optimized to run smoothly and efficiently leading to the greatest customer experience.

 

So, if you operate a Magento 2 store and are uncertain what mode your site is in, we recommend you check and verify that your live site is in   Production  mode. To do so, check this article and follow the steps in one of the sections below.

 

If you’re not a Magento geek, you may have many questions to be answered:

 

  • What are the Magento modes?
  • How can I know the current mode?
  • How can I change the mode?

 

Let’s start with figuring out how the Magento modes differ.

 

Magento 2 Modes

What is the Difference Between Magento 2 Modes

One major difference between the Magento 1 and Magento 2 platforms is the introduction of development Modes. Magento 2 modes were introduced with the release of Magento 2 that reflect varying configurations of the Magento codebase which affect site operations and performance.

Magento 1 was operated with a single state for development and production, Magento 2 now boasts 3 separate Modes:   Default  ,   Developer  and  Production  .

 

Magento 2 Modes Overview

Magento has three modes of operation for deployment in a development or production environment:  Default  ,   Developer  and  Production  modes. The fastest one is production.  Default   and  Developer   modes are used for debugging purposes and should never be used for a live site.

 

  • Default – enables you to deploy the Magento application on a single server without changing any settings. However, the default mode is not optimized for production;
  • Developer – intended  for development only;
  • Production – intended for deployment on a production system;

 

And one mode for maintenance:

  • Maintenance – intended to prevent access to a Magento Commerce site while it is being updated or reconfigured.

 

 

Here is a brief description of 3 deploy modes in Magento 2. Let’s take a quick look at each of them!

Magento 2 Default Mode

As the name suggests, the  Default  mode is when no other mode is specified.  The  Default   mode is usually used when we launch the Magento 2 site for the first time.

This mode is a hybrid of  Developer  and  Production  modes and appears to be intended to ramp up the time-to-launch for base Magento 2 sites due to the ability to develop without compiling code as well as a standardized set of configurations.

  • In the  Default   mode, there are no main characteristics for production, that is why you need to switch to  Developer  mode or   Production  mode.
  • Errors are never shown to the user. In the  Default  mode errors are logged to the file reports at the server. So while debugging is possible, it is not ideal for development. 
  • After we switch out of the Default  mode to another one, it is impossible to go back to this mode, because  Default   mode is not intended for the long-term use of Magento and site support, it was introduced only to help launch sites.
  • Static view files are first materialized and then cached. Hence this mode is not ideal for the production environment.
  • For each requested file on the site, a symlink is created in the   pub/static  directory. This means that in  Default   mode full page cache is not being fully utilized as the site still serves content dynamically for each request rather than drawing from the cache.
  • In the  Default   mode, you can deploy the Magento application on a single server without any changes in the settings.

 

While this can lead to a quicker time-to-launch, new modifications and maintenance will ultimately save you time in  Developer   mode due to the more advanced development functionality outlined in the next section.

Magento 2 Developer Mode

The mode for the developers – to do all their development tasks! The  Developer   mode is intended only for development. This is the mode you should use to customize and expand Magento.

  • Caching can be utilized while in   Developer   mode it is typically disabled to speed up development testing. This in combination with the lack of a populated and utilized  /pub  folder leads to exceptionally slow load times on the frontend and Magento admin
  •  Developer   mode automatically compiles code and offers more advanced logging (in verbose) and debugging capabilities than of    Default   or  Production  modes.
    – Exceptions are seen in the error handler rather than being logged, what means the actual errors are displayed in the browser rather than displaying just the error number, which then has to be tracked down in the file reports at the server.
     – Exceptions are also thrown in the browser when event subscribers cannot be invoked which results in faster debugging of extension conflicts.
  • Static view files are written to the Magento    pub/static   directory every time they’re called and are not cached.
  • Uncaught exceptions and errors are displayed in the browser.

 

NOTE: Use this mode whenever you are customizing or extending the Magento 2’s default functionalities.

Production Mode

The  Production   mode is designed for deployment in a production system. All live sites must have   Production   mode since full caching of web pages is used in production mode, which ensures smooth and efficient work for clients on the site.

  • Static files are stored in cache only. This mode utilizes full page caching alongside a fully built  pub   folder to pull from and is optimized to run smoothly and efficiently leading to the greatest customer experience. 
  • Debugging is more difficult in  Production   mode as the errors are logged at the server level and never shown to the user on the frontend.
  • The  Production   mode uses full page caching with a fully built  pub  folder to pull from, which results in the smooth and efficient functioning of the store. If you are concerned about offering the best customer experience, you must switch your store to this mode. 
  • In terms of security, the  Production   mode is the most secure as well due to the fact that there is no symlink created for the  pub/static   folder. If an unauthenticated user gets access to the  pub/static   to change the data, it would only be the duplicate versions of those files,  overwritten during the next static deploy!
  • In terms of speed, 

 

Anytime you switch from  Developer   to  Production   mode two actions must first be taken:

1. Static Deploy – this populates everything in the  pub/static   folder which a  Production   M2 site will then build and serve content from.

2. Compilation – this takes your site’s code and merges it into bytecode which ultimately allows for faster retrieval of classes and results in faster page load times on the frontend. This means if a malicious user were to gain access to pub/static and make changes they would only be changing duplicate versions of those files which would be overwritten during the next static deploy.

 

 

That was all about the 3 Magento 2 Modes!

It must be clear that the live Magento 2 site must be in  Production  mode.  Check today itself if your Magento 2 site is in  Production   mode or not!

How to Check the Current Magento 2 Mode

You are not a tech person, but the business owner who wants to know the mode of his/her store? Following the steps below, you can easily check whether your site is in  Production   mode or not.

    • Partner With Us: Let’s discuss how MageCloud team can help to grow your eCommerce business. 

 

 

 

Step 1.

Log into your server via SSH as the Magento file system owner.

  • If you have shared hosting, this is the user your provider gives you to log in to the server.
  • If you have a private server, it’s typically a local user account on the Magento server.

 

You need an SSH client on your computer.

  • On Mac you can use Terminal which is included by default or a third-party application like Termius.
  • On Windows-based computers, you can install a third-party application like PuTTY.

 

When connecting via SSH use this info:

Host: ssh.example.com
Username: example.com
Password: the one you chose for SSH in your Control Panel
Port: 22

NOTE: Remember to replace example.com with your own domain.

 

Step 2.

Navigate to the root Magento directory. Typically it’s  public_html  , but not necessarily. You can always check with your hosting provider to find your site’s root.

 

Step 3.

Navigate to the  /bin  directory within the site root.

 

Step 4.

Run the following command:  php bin/magento deploy:mode:show   

 

A message similar to the following displays which mode your Magento 2 site is currently using:

 Current application mode: {mode}. (Note: Environment variables may override this value.) 

 

where:  {mode}  can be either   Default  ,  Developer   or    Production

OR

 

So, what is your website mode in the end?

If it’s a live site in the  Developer   mode, it might be an issue. So, let us know if you need any Magento help with your store!

 

 

How to Change the Magento Mode

NOTE: You should not change your site’s mode directly on the live site without first testing this fully in a development environment.

 

Step 1.

SSH into your site’s server.

 

Step 2.

Navigate to the site’s root- then to the  /bin  directory.

 

Step 3.

Run the following command:  php bin/magento deploy:mode:set {mode} [-s/-skip-compilation]   
where:

 

where:

    •  {mode}   is a required parameter (developer or production);
    •  [-s/-skip-compilation]    is an optional parameter(it’s used for skipping the compilation when switching to the developer mode).

 

In the command above you can use either the value  Developer  or  Production   mode to switch to the corresponding mode. Once you switch the site’s mode and everything works out well, you will see the following line in the console:

 

 Enabled developer mode.   or  Enabled production mode.

Now with this being so easy to do in theory, one would wonder why his/her site is not even in  Production   mode. One common issue is that there is a fault somewhere in the system that breaks the site when running this command and may take some time for a developer to identify the root cause of this.

Therefore it’s common that we see that developers tend to leave it in  Production   mode, as not to have the headache of fixing the issue as it may be a deep-rooted problem that’s not so easy to solve.

 

 

Hot Tips when Switching the Mode

The things you should consider when attempting to convert into  Production   mode:

  • Under no circumstances attempt to do it in the middle of a transactional day as it may completely ruin your site for a period of time.
  • Take a full backup of the Magento files and database before attempting to convert so you have a fallback if things go wrong.
  • Ideally, you would try to do it on staging first, so you can see the probability of it succeeding or failing in a live environment – obviously, there are always small variables that can be different on each server environment.
  • When switching to the  Developer  or  Production   mode, we recommend clearing the contents of the following folders (be careful not to delete the .htaccess files, they register configuration changes for each directory):

      •  var/cache 
      •  var/di 
      •  pub/static 
      •  var/generation 
      •  var/view_preprocessed 

     

  • Sometimes, while switching modes you will encounter errors that prevent the mode change. These errors must be tracked down and resolved before the mode can be changed.

 

Hopefully, the idea of Magento 2 modes is clear with this tutorial!

Do let us know if there are any other questions that you want us to be answered via our blog!

 

Why You Should Enable Production Mode on Live Environment

 Developer   mode optimises the website for the developer to quickly work on and deploy new changes with minimal time to see new changes on the site frontend – for example, there is not static content deployed in the cache which would allow the developer to make changes without having to run lengthy/time-consuming commands to flush the files out the browser reads, e.g. updating the website.

However, this means that the actual page load is also increased by around 20-30% due to exactly what makes it easier for a developer to see new changes: There is no static content which essentially would work as a cache for browsers not to have to load many files on the fly.

This is a huge disadvantage as we know the website is already slow and will be affecting your conversions due to users on slow connections exiting the site with it being so slow.

It must be clear that the live Magento 2 site must be in  Production  mode. Earlier you switch, the better! It is easier to switch a Magento 2 site to  Production   mode with less customization.

 

 

Recap

You should now have a better understanding of the differences between the three modes for Magento 2, as well as how to verify which mode your site is using and how to change modes. Using the different modes for Magento 2 presents new opportunities during development and ultimately offers greater performance and functionality for the platform now and for the future.

Magento 2 comes equipped with various performance optimization features. Your job is to configure them properly.

We are always looking for ways that we can help the Magento community grow, so please let us know any questions or feedback you have!

 

 

***

 

If you would like to know how MageCloud and can assist with your Magento store, please contact us to start discussing your requirements. 

 

 

Want to learn more?

Let us prove our skills and provide you video review for your ecommerce site. Feel free to schedule meeting with our team.
Cities we operate in