Switching modes in magento2

Switching modes in magento2

An overview
To improve security and ease-of-use, magento has built-in commands that switches magento modes from developer to production and vice versa. Production mode also has better performance because static view files are populated in the pub/static directory and because of code compilation.
When you change to developer or production mode, the contents of following directories are cleared:

-      var/cache
-      generated/metadata
-      generated/code
-      var/view_preprocessed
-      pub/static

NOTE : These paths are with respect to the magento2 installation directory(generally public_html).

Exceptions:

  • .htaccess files are not removed
  • pub/static contains a file that specifies the version of static content; this file is not removed

Changing modes

To display the current mode, switch to magento installation directory as magento2 file system owner and run the above command -
bin/magento deploy:mode:show
A message similar to the following displays:
Current application mode: {mode}. (Note: Environment variables may override this value.)

where, {mode} can be either default, developer, or production.

Change modes

Command usage:

 bin/magento deploy:mode:set {mode} [-s|--skip-compilation]

where,
  • {mode} is required; it can be either default, developer, or production

  • --skip-compilation is an optional parameter you can use to skip code compilation when you change to production mode.


Change to production mode
      bin/magento deploy:mode:set production
Messages similar to the following will display -

=====================================================================
Enabled maintenance mode
Requested languages: en_US
=== frontend -> Magento/luma -> en_US ===
... more ...
Successful: 1884 files; errors: 0
---

=== frontend -> Magento/blank -> en_US ===
... more ...
Successful: 1828 files; errors: 0
---

=== adminhtml -> Magento/backend -> en_US ===
... more ...
---

=== Minify templates ===
... more ...
Successful: 897 files modified
---

New version of deployed files: 1440461332
Static content deployment complete
Gathering css/styles-m.less sources.
Successfully processed LESS and/or [Sass](https://glossary.magento.com/sass) files
[CSS](https://glossary.magento.com/css) deployment complete
Generated classes:
      Magento\Sales\Api\Data\CreditmemoCommentInterfacePersistor
      Magento\Sales\Api\Data\CreditmemoCommentInterfaceFactory
      Magento\Sales\Api\Data\CreditmemoCommentSearchResultInterfaceFactory
      Magento\Sales\Api\Data\CreditmemoComment\Repository
      Magento\Sales\Api\Data\CreditmemoItemInterfacePersistor
      ... more ...
Compilation complete
Disabled maintenance mode
Enabled production mode.
=====================================================================

Change to developer mode

When you change from production to developer mode, you should clear generated classes and Object Manager entities like proxies to prevent unexpected errors. After doing so, you can change modes. Use the following steps:

If you’re changing from production mode to developer mode, delete the contents of the generated/code and generated/metadata directories:

rm -rf <magento_root>/generated/metadata/* <magento_root>/generated/code/*

Set the mode : bin/magento deploy:mode:set developer

The following message displays:
====================
Enabled developer mode.
====================

Change to default mode

Run the above command from magento2 installation directory as magento file system owner :
      bin/magento deploy:mode:set default

The following message displays:
==================
Enabled default mode.
==================


    • Related Articles

    • How to configure base URLs for a Magento 1 website?

      In this article, we are going to change the base URLs of a Magento 1 website, using the swiss army knife for Magento developers, sysadmins and devops i.e. n98-magerun. Base URLs can be also changed from the Magento Site backend as well as from the ...
    • Magento Customer login does not work in Chrome browser

      Problem description Customers can not log in into Magento with Chrome browser, the login page just reloads without any errors or warnings. But when customer logins in other browsers and admin back-end login function properly. Cause Magento sets an ...
    • How to configure base URLs for a Magento 2 website?

      In this article, we are going to change the base URLs of a Magento 2 website, using the swiss army knife for Magento developers, sysadmins and devops i.e. n98-magerun2. Base URLs can be also changed from the Magento Site backend as well as from the ...
    • Compiling code and deploying static view files in magento2

      Code compilation To compile the code in magento2, we use setup:di:compile command which performs the following tasks : Application code generation – for factories, proxies Interceptor generation – improve code generation of interceptors Interception ...