Set Environment Variable [Developer or Production Mode] (M2)

First, will be a good idea to check the current MODE:

php bin/magento deploy:mode:show

The main idea is to have this (environment) variable MAGE_MODE set to one of this values:

  • Default
  • Developer
  • Production

 

To change the MODE in Magento for the moment there are 3 ways:

  1. From CLI  (you’ll be 100% sure it changed)
    # we are using the Magento deploy in order to change the mode 
    # there are 3 possible values:
    #       * default
    #       * developer
    #       * production
    #
    # BUT there is a CATCH: once you changed
    # the DEFAULT mode you can NOT go back to it 
    
    
    # make it into Developer mode
    php bin/magento deploy:mode:set developer
    
    
    # for Production mode
    php bin/magento deploy:mode:set production
  2. Using the .htaccess file:
    # add the following line in the main .htaccess file
    
    SetEnv MAGE_MODE developer
  3. Through your WEB Server (Apache or Nginx)
    • This is an example for Apache , added in vhost file:
      #First 2 lines are just to give an idea where it should be located
      
      <VirtualHost 127.0.0.1:1900>
      	ServerName localhost.com
      
          ###    THE MAIN LINE    ###
      
      	SetEnv MAGE_MODE "developer"
      
          ###########################
      
              #If you're here you can add a 
              #couple more lines 
      	SetEnv MAGE_RUN_TYPE "website"
      	SetEnv MAGE_RUN_CODE "mage2_pro"
      </VirtualHost>
    • Nginx example
      location ~ \.php$ {
          fastcgi_param MAGE_MODE "developer";
      }
      

Alternatively you also can set (on UNIX systems), the environment variable can also be set by exporting it into a shell before restarting the service:

export MAGE_MODE=developer

 

NOTE: You can NOT go back to Default mode (in theory) once you changed it to production or developer but if you really want to go back then you could open this file:

    app/etc/env.php

and edit the following line:

<?php

return array (
  ...
  'MAGE_MODE' => 'default',
  ...
);