Managing Magento 2 set mode (MAGE_MODE)

Magento 2 comes with a new feature set called MAGE_MODE. This option gives you the configuration to run Magento in either developer, default, or production mode.

This feature is very important during development and product phases. It gives a developer the tool to debug or created optimized caches for high performance needs.

By default, the default mode is set.

The following table describes the modes in which we can run Magento:

Mode name

Description

default

When no given mode is given, this is explicitly set and has the following benefits:

  • Static view file caching is enabled
  • Enables automatic code compilation (code optimization)
  • Exceptions are written to the log files.
  • Hides the custom X-Magento-* HTTP response header

developer

It has the following benefits:

  • Disables static view file caching
  • Enables automatic code compilation (code optimization)
  • Shows the custom X-Magento-* HTTP response header
  • Verbose logging
  • Slowest performance state

production

It has the following benefits:

  • Optimized caches available
  • Exceptions are written to the log files.
  • Static files are not cached

Tip

When using a Development Test Acceptance Production (DTAP) environment, it is important to run your DTA in development mode and production on P.

Getting ready

Always check what web server you are using; the settings for Apache and NGINX are not the same. In this recipe, we will be showing you how to set this on both of them.

How to do it...

For the purpose of this recipe, let's assume that we need to manage the Magento 2 production or development setup. The following steps will guide you through this:

  1. In this recipe, the setup of the MAGE_SET option is different for NGINX and Apache. In Apache, we can use either the .htaccess file or configure this in the vhost file. We will first look into the Apache setup. While all recipes of this chapter are based on NGINX, it's best to skip this part and continue to the next listed topic or retrieve an DigitalOcean Droplet that we had set up in Chapter 1, InstallingMagento 2 on Apache and NGINX.

    Go to the .htaccess file in your web root directory and remove the # (hash or pound sign) at the fifth line from the top:

    SetEnv MAGE_MODE developer
    

    Change it to the following:

    SetEnv MAGE_MODE production
    

    If you are using a server-based configuration instead of the .htaccess file, use the following then:

    SetEnv MAGE_MODE "developer"

    The following code is for the current 000-default.conf:

    <VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html
    SetEnv MAGE_MODE "developer"
    <Directory /var/www/html>
    Options Indexes FollowSymLinks
    AllowOverride All
    Order allow,deny
    allow from all
    </Directory>
    
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
    
    ProxyPassMatch ^/(.*.php(/.*)?)$ fcgi://127.0.0.1:9000/var/www/html/$1
    </VirtualHost>

    Now continue to step 3.

  2. Now let's do the setup for NGINX. Go to your vhost file. If you are using the NGINX setup from Chapter 1, Installing Magento 2 on Apache and NGINX, you will find it in /etc/nginx/conf.d/default.conf.

    Open the default.conf file and go to the following rule:

    set $MAGE_MODE developer;

    Change this to the following:

    set $MAGE_MODE production;

    Restart your NGINX server now using the following command:

    service nginx restart
    
  3. Check the current status in Magento using the following command:
    php bin/magento deploy:mode:show
    

    By default, it shows the default status. We now switch the status using the following:

    php bin/magento deploy:mode:set production
    

    We can also use the following command:

    php bin/magento deploy:mode:set developer
    

    This will trigger the maintenance mode and start creating all necessary optimized static files needed.

  4. We can also run this manually. However, first we will need to create static files in the pub/static directory. Run the following command on the shell:
    php bin/magento setup:static-content:deploy
    
  5. If you want to skip the code compilation, use the --skip-compilation option, as shown in the following command:
    php bin/magento deploy:mode:set developer --skip-compilation
    
  6. Remember to check your permissions and ownership of the newly created files.

    Tip

    Code compilation consists of caches, optimized code, optimized dependency injection, proxies, and so on. Magento 2 needs these files to serve an optimized code base to the client's browser.

How it works…

Let's recap and find out what we did throughout this recipe. In steps 1 through 6, you learned configuring the development and production modes in Magento 2.

In step 1, we configured the SetEnv parameter in the .htaccess file of Apache to set the correct mode. There is also an option to configure this in the Apache configuration file instead.

In step 2, we configured the set $MAGE_MODE parameter in NGINX to use the correct mode.

In step 3, we used the bin/magento deploy option to tell Magento to start using the selected mode in NGINX or Apache, and create additional static files when running in production mode or show the correct debug headers in the developer mode.

In step 4, you learned how to deploy static content in the pub/static directory when running in production mode. This option will trigger the whole process of merging and compiling the correct code in the public folder.

There's more…

Use curl to check your HTTP response header to see what current state you are running, as shown in the following:

curl -I http://mage2cookbook.com/

HTTP/1.1 200 OK
Server: nginx/1.9.6
X-Magento-Cache-Debug: HIT

Always check the current status in your HTTP header and Magento shell. Only setting the web server configuration will not automatically trigger the Magento configuration and can mislead you.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.80.34