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 |
---|---|
|
When no given mode is given, this is explicitly set and has the following benefits:
|
|
It has the following benefits:
|
|
It has the following benefits:
|
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.
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:
.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.
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
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.
pub/static
directory. Run the following command on the shell:php bin/magento setup:static-content:deploy
--skip-compilation
option, as shown in the following command:php bin/magento deploy:mode:set developer --skip-compilation
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.
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.
18.116.80.34