Though most of the development tasks are done via developer Magento mode, a store should run in production mode to ensure a secure and fast environment.

In this guide, you'll learn how to set Magento 2 production mode correctly and explain how to do it without downtime.

How to Set Production Mode in Magento?

Switching Magento to production mode requires running one simple CLI command: 

php bin/magento deploy:mode:set production

However, there are some steps before and after that you should know about.

Step 1: Check the current mode

To see what Magento mode a store is in, run the following command:

php bin/magento deploy:mode:show

After running this command, you'll receive one of these two messages:

Current application mode: production
Current application mode: developer

Step 2: Set production mode in Magento

If a store is in developer mode, the following CLI command helps Magento set production mode:

php bin/magento deploy:mode:set production

To switch back to developer or default mode, run the same command, changing the name of the mode, like in:

php bin/magento deploy:mode:set developer

php bin/magento deploy:mode:set default

Step 3: Compile the code

The last step is to make Magento work faster in production, it's recommended to run the following command as well:

php bin/magento setup:di:compile

Step 4: Deploy static content

Magento 2 production mode cannot auto-generate static files when a page is visited (unlike in developer mode).

Therefore, when switching to production from developer mode, it's important to :

php bin/magento setup:static-content:deploy

Otherwise, a site may look broken.

Step 5: Flush the cache

After switching to production, the rule of thumb is to flush Magento cache to ensure visitors see the latest version of the store:

php bin/magento cache:flush

Magento: Set Production Mode Without Downtime

To switch to Magento 2 production mode with no downtime, merchants can use the following command:

bin/magento magefan:zero-downtime:deploy-mode-set --mode production

The process is also reversible. So if you need to switch back to developer or default without downtime, use the following commands:

bin/magento magefan:zero-downtime:deploy-mode-set --mode developer
bin/magento magefan:zero-downtime:deploy-mode-set --mode default

These commands comes from the Magento 2 Zero Downtime Deployment extension by Magefan. They are part of the series of zero downtime deployment commands that prevent dowtime in Magento.

So, you can avoid downtime not only when switching Magento to production mode, but also during static content and theme deployments. This way, merchants can customize their store as much as they need without affecting store visitors.

FAQs

How to check if Magento is running in production mode?
collapsible icon
To check if Magento is in production mode, run the following command: php bin/magento deploy:mode:show.
How to update files when in Magento production mode?
collapsible icon
Although you can edit the files when in the production mode, you can't deploy the changes. So you need to swtich to maintenance mode to update the files and run the following commands:

PHP bin/magento setup:upgrade
PHP bin/magento setup:di:compile
PHP bin/magento setup:static-content:deploy -f
Why is Magento slow in production mode?
collapsible icon
Magento could be extremely slow in the production mode after you switch from the default or developer mode because of disabled caches, long server response time or other browser-related speed factors.

First, try to enable all caches using the following command: php bin/magento cache:enable. Then use the code profiler to see what code slows down your Magento in production.