Magento 2 requires a set of commands to be run after various changes to its installation, whether it be introducing a new extension or upgrading a current one. The following commands required to be run, and commonly run one after the other, are:
bin/magento setup:upgrade
bin/magento setup:static-content:deploy
bin/magento setup:di:compile
In the past I’ve been able to run these commands with little issue, but recently on one particular client’s multi-website Magento 2 installation, which has large cron jobs and higher traffic, the commands caused extremely high CPU usage. It seemed that running the commands with the other regular Magento 2 processes occurring in the background caused severe slow downs and regular site outages until PHP was restarted. In turn, this caused much frustration from both myself, the hosting company, and most importantly the client, who was losing dollars each time customers were unable to make purchases through their sites.
After doing much research and working with the hosting company, a protocol was developed in hopes of running the commands with no adverse effects on the server. I now follow the steps below in sequential order each time I need to install or upgrade an extension, modify a theme’s code, etc.
Step 1
Disable the 3 cron jobs Magento 2 requires via the crontab. With the Magento 2 cron jobs disabled, essentially eliminating any CPU usage and memory it that it would typically need. Those cron jobs are the following:
* * * * * /usr/bin/php /var/www/html/magento2/bin/magento cron:run
* * * * * /usr/bin/php /var/www/html/magento2/update/cron.php
* * * * * /usr/bin/php /var/www/html/magento2/bin/magento setup:cron:run
Step 2
Enable maintenance mode using the following command in the Magento 2 root directory:
bin/magento maintenance:enable
An even more effective tactic for a maintenance mode is to edit the .htaccess file in the root directory and create a rule to redirect all traffic to a simple maintenance page. In this approach, Magento 2 will not be loaded at all by any users, as the maintenance page displayed in the above command still requires Magento 2 to process. The following rules can be added after the line RewriteEngine On in your .htaccess file:
# maintenance.php is the filename that the traffic is being redirected to
RewriteCond %{REQUEST_URI} !/maintenance.php$ [NC]
# make exceptions for any images that may be used on the maintenance page
RewriteCond %{REQUEST_URI} !\.(jpe?g?|png|gif) [NC]
RewriteRule .* /maintenance.php [R=302,L]
Step 3
Install any new code, such as a new extension or upgrade to an existing one, then any necessary commands to introduce the changes into Magento 2, such as:
bin/magento setup:upgrade
bin/magento setup:static-content:deploy
bin/magento setup:di:compile
bin/magento cache:clean
You can monitor the CPU usage with commands such as top and ps while running the above processes to see if they are causing detrimental effects to the server speed.
Step 4
Disable Magento 2 maintenance mode using bin/magento maintenance:disable or remove the redirect to the maintenance page if this was the maintenance option you decided to use. I also like to make sure that that the Magento 2 caches are all enabled, as I’ve had them become disabled in the past. I use command bin/magento cache:enable. Finally, I test the Magento 2 site(s), both frontend and backend, as well as make sure the changes I’ve made to the code base are indeed implemented.
If you’re having efficiency issues with the Magento 2 platform, feel free to reach me at pmgephart@gmail.com.