The MAGE_MODE Trap




You can run Magento 2 in three different modes: default, developer and production. Magento 2 allows you to define which mode to run via a server variable (MAGE_MODE). This can be seen in the below example configuration provided for Nginx with Magento v2.0.10:

# Magento Vars
# set $MAGE_ROOT /path/to/magento/root;
# set $MAGE_MODE default; # or production or developer

...

location ~ (index|get|static|report|404|503)\.php$ {
    try_files $uri =404;
    fastcgi_pass   fastcgi_backend;

    fastcgi_param  PHP_FLAG  "session.auto_start=off \n suhosin.session.cryptua=off";
    fastcgi_param  PHP_VALUE "memory_limit=256M \n max_execution_time=600";
    fastcgi_read_timeout 600s;
    fastcgi_connect_timeout 600s;
    fastcgi_param  MAGE_MODE $MAGE_MODE;

The problem is that the above only affects requests that are served via the web server. Additionally Magento 2's mode can be changed via the command line php -f bin/magento deploy:mode:set X which is:

  1. not aware of the web server variable and
  2. does not update the server variable for you as part of changing the mode

This can lead to a disconnect between the two ways of setting the mode. If one is set to 'production' and the other to 'developer' things stop working, like the on-demand compiling of static content under the pub/static directory.

My advice: don't bother setting MAGE_MODE on the server environment level unless you are absolutely sure you'll never need to change the mode. Comment out the lines mentioning MAGE_MODE in your nginx.conf (for Nginx) or .htaccess (for Apache) files if they are present and exclusively use
php -f bin/magento deploy:mode:set production or
php -f bin/magento deploy:mode:set developer.

Kristof Ringleff

Kristof Ringleff

Founder and Lead Developer at Fooman

Join the Fooman Community

We send our popular Fooman Developer Monthly email with Magento news, articles & developer tips, as well as occasional Fooman extension updates.