Magento 2 Speed Optimization: Step-by-Step Instructions

With a wide range of Magento 2 performance optimization tips and tricks on the Internet, e-commerce owners shouldn’t be worried about slow-loading websites. However, even a robust and highly scalable platform like Magento 2 can annoy its users with the timing issue. Moreover, retailers often experience negative results from implementing random techniques without investigating the core problems. That’s when the need for speed optimization arises, and business owners start to look for the right services. Before diving deep into the technical details of Magento 2 speed optimization, let’s investigate the main signs and causes of slow website loading time.

Magento 2 Speed Optimization: Step-by-Step Instructions

Top 3 Wake Up Calls of Magento Slow Website Performance

According to research, an e-commerce company that sells $100,000 per day can potentially lose $2.5 million a year just because of a one-second page delay. So, what are the reasons behind the loss? Check out the red flags that should be taken into consideration:

  • Low Google Ranking. It is well-known that website speed is one of the main aspects Google uses to rank sites. In other words, poor performance can hurt your entire SEO strategy that is developed for moving your online store up in search results.
  • Low Sales KPIs. If your efforts put into developing and promoting your company don’t pay off, as well as seeing the traffic and conversion rates are low, it is recommended to check the website speed. Today’s potential clients are impatient to wait until the website loads, so they leave without any engagement.
  • Poor Customer Experience. If surfing through your own website provokes a hint of irritation at slow loading pages, the visitors will most likely have the same experience.

All in all, if you notice some of the above-mentioned issues while running the online business or feel that your shop loses in speed to competitors, it is definitely the time to accelerate your Magento performance.

How to Determine the Magento Speed Problem

When you decide to identify the speed of your online shop and ascertain if there is a problem, you can use different speed tools to translate guesses into precise figures. For instance, you can take advantage of WebPageTestMage Speed Test, web.devGTmetrix, or other alternatives. The tools offer you an opportunity to receive a speed report with page load time, analyzed bottlenecks, and optimization suggestions by entering your website URL. Also, speed testing tools enable you to schedule daily, weekly or monthly website performance monitoring, as well as set up email alert notifications in case of detected issues. Of course, the recommendations are not targeted specifically for every site, but the information will be based on best practices. Usually, this part of the service is free of charge. If you want to benefit from other functionality like running custom tests, monitoring several URLs, etc., a number of business owners go for paid packages.

Magento 2 Speed Optimization: 7 Hints to Boost Site Performance

Now, when you determined the speed problem of your Magento 2 e-store, your first task is to understand that optimizing the Magento performance doesn’t mean changing just a few general settings. The process starts with identifying the reasons for slow performance and finding the right solution to fix the issues. Among the variety of tools and services floating around the web, you can take advantage of Magento 2 Speed Optimization Service provided by Plumrocket. It allows you to enhance search rankings, increase user engagement and boost conversion rates by analyzing the problems and applying the following tactics to your website:

  1. Choose the right hosting.
  2. Perform PHP and MySQL tuning.
  3. Minify and Merge JS, HTML, and CSS.
  4. Delete unnecessary extensions.
  5. Manage the cache.
  6. Configure a Content Delivery Network (CDN).
  7. Optimize images.

1. Choose the Right Hosting

Selecting fast hosting is one of the most fundamental aspects to consider before even trying to optimize your Magento online store. So, an efficient web host should satisfy the conditions like quick support, high security, and fast connection, as well as fit the following Magento 2 technology requirements:

  • Operating systems. The hosting of your Magento online business should be working on Linux, and Linux distributions like RedHat Enterprise Linux (RHEL), CentOS, Ubuntu, Debian, and others.
  • Memory restrictions. Upgrading the Magento 2 extensions can require up to 2GB of RAM. If you are running a system with less than required memory size, there is a possibility of upgrade failure.
  • Latest composer version. If you want to contribute to the Magento codebase, develop or install Magento modules, the composer should be included in your hosting.
  • Web servers. Regarding the web servers, you can choose between Apache and NginX 1.x. Also, you should enable the Apache mod_rewrite and mod_version modules. More info here.
  • Database. Magento is compatible with MySQL 5.6, 5.7. Magento is also compatible with MySQL NDB Cluster 7.4.*, MariaDB 10.0, 10.1, 10.2, Percona 5.7, and other binary-compatible MySQL technologies.
  • PHP. The recommended php version for Magento 2.1 and 2.2 versions is PHP 7.1 and for Magento 2.3 is PHP 7.2.
  • PHP extensions. In order to expand PHP functionality, you can install different extensions. For example, Magento 2.3 should include the following extensions:
Magento 2 speed optimization - required PHP extensions
  • PHP OPcache. It is suggested to verify if PHP OPcache is enabled for performance reasons. The OPcache is enabled in many PHP distributions. In case you want to verify if it is installed, check out the PHP documentation. To find out the instructions on how to install it separately, consider the PHP OPcache documentation.
  • PHP settings. PHP configuration settings can be memory_limit that can help you avoid the common issues while using Magento.
  • SLL. If you want to provide the customers with better security, SSL certificates are required to be installed, and https website version is enabled. SSL is required for an efficient work of payment systems such as PayPal. Also, SSL is used to download the extensions via composer from Magento Marketplace or other platforms.
  • System Dependencies. Some Magento operations require the following system tools: bashgziplsofmysqlmysqldumpnicephpsedtar.

By and large, the above-mentioned recommendations are just the minimum advice for successful Magento website optimization. Just like different aspects can slow down your website performance, there is a variety of additional software that will improve the Magento speed. In case you want to provide the online shop with faster and more stable speed performance, you can benefit from the following software and plugins: Redis, Varnish, Elasticsearch, and CDN. Also, it is recommended to use VPS (Virtual Private Server) or Cloud based hosting to gain total control of the server, scale resources, and save costs.

2. PHP and MySQL Tuning

After selecting and configuring the desired hosting, the next step is to perform PHP and MySQL tuning. Thus, in order to run a process efficiently, check out the instructions below.

How to Perform PHP Tuning

If you want to execute PHP tuning, the setup of optimal values in php.ini’ (PHP configuration file) is one of the tasks to consider.

1. The first step is to find php.ini’There are 2 different ways to perform the process:

a) Open the console, run php –ini and look for Loaded Configuration File in output for the location of php.ini used by your CLI. Example of the output:

Magento 2 Speed Optimization

b) Create a file phpinfo.php’  (for example, you can create this file in the Magento root folder) and insert the code:

<!--?php phpinfo(); ?-->

c) Then, open this file in the browser, e.g. https://<your_site_url>/phpinfo.php

d) After that, find the location of your php.ini’ file in the opened window. For instance:

Magento 2 Speed Optimization - php.ini file location

2. Once the path to php.ini’  is found, you need to open it and insert the new values. Check out an example of php.ini’  setup for Magento 2:

memory_limit = 2048M
max_input_time = 300
max_execution_time = 18000
output_buffering = 4096
zlib.output_compression = on
zlib.output_compression_level = -1
always_populate_raw_post_data = -1

How to Perform My SQL Tuning

MySQL performance tuning can help quickly determine bottlenecks, target insufficient operations through review of query execution plans and eliminate any guessing games. So, take the following steps to complete the task:

1) If you want to start MySQL tuning, open my.cnf file. To find this file, use the next command in the Console:

‘mysqld –help –verbose’

You should get similar output:

Magento 2 Speed Optimization 2

2) You will see a list of all ‘my.cnf’ in the first row, for example, Default options are read from the following files in the given order:

/etc/my.cnf /etc/mysql/my.cnf ~/.my.cnf

3) Then, you can configure ‘my.cnf’, e.g.:

long_query_time =1
innodb_buffer_pool_size = [50% of RAM]
innodb_thread_concurrency = 2 * [numberofCPUs] + 2
innodb_flush_log_at_trx_commit = 2
thread_concurrency = [number of CPUs] * 3
thread_cache_size = 32
table_cache = 1024
query_cache_size = 64M
query_cache_limit = 2M
join_buffer_size = 8M
tmp_table_size = 256M
key_buffer = 32M
innodb_autoextend_increment=512
max_allowed_packet = 16M
max_heap_table_size = 256M
read_buffer_size = 2M
read_rnd_buffer_size = 16M
bulk_insert_buffer_size = 64M
myisam_sort_buffer_size = 128M
myisam_max_sort_file_size = 10G
myisam_max_extra_sort_file_size = 10G
myisam_repair_threads = 1

3. Minify and Merge JS, HTML, CSS

One of the essential steps in the Magento 2 performance improvement is minifying and merging JS, HTML, CSS, as well as bundling JS. So, in order to perform these tasks and reduce Magento 2 page loading time, you can follow the instructions below.

How to minify Javascript and CSS in Magento 2

1. First, open the Console and put Magento 2 in production mode:

./bin/magento deploy:mode:set production

2. Then, go to Stores > Configuration > Advanced > Developer.

3. Set Minify Javascript Files or CSS Files option to Yes.

4. After that, click Flush Cache on the System > Cache Management page.

Minify JS and CSS Magento 2

How to minify HTML in Magento 2

  1. First, put Magento 2 into production mode by executing the following command:
./bin/magento deploy:mode:set production
  1. Then, go to Stores > Configuration > Advanced > Developer > Template Settings
  2. Set Minify HTML option to Yes.
  3. After that, click Flush Cache on the System > Cache Management page.
Magento 2 Template Settings - Minify HTML

How to merge Javascript in Magento 2

  1. First, go to Stores > Configuration > Advanced > Developer.
  2. Open the JavaScript Settings section, and continue with the following:
  3. In the Merge JavaScript Files field, select Yes.
  4. When complete, click Save Config.
Magento 2 merge JavaScript Files

How to merge CSS in Magento 2

  1. First, go to Stores > Configuration > Advanced > Developer.
  2. Then, open the CSS Settings section, and continue with the following:
  3. In the Merge CSS Files field, select Yes
  4. When complete, click Save Config.
Magento 2 Merge CSS Files

The following visual shows the results of bundling after all commands are executed:

Magento 2 Speed Optimization 3

How to Bundle JS in Magento 2

Open the Console and execute the commands below:

1. From the Magento root directory, switch to production mode:

php bin/magento deploy:mode:set production

2. Enable JavaScript bundling:

php -f bin/magento config:set dev/js/enable_js_bundling 1

3. Optimize bundling by minifying JavaScript files:

php -f bin/magento config:set dev/js/minify_files 1

4. Enable cache busting on static file URLs. This ensures users get the latest version of the assets anytime they update:

php -f bin/magento config:set dev/static/sign 1
Magento 2 Speed Optimization 4

So, if you perform minifying, merging, and bundling activities on your Magento 2 website, the process of code optimization can help you improve the e-store performance, enhance user experience and increase sales.

4. Delete Unnecessary Extensions

If your Magento 2 platform includes a lot of extensions that are hardly or not used at all, it can not only affect your budget but also slow down the website speed. In this case, it is recommended to disable the unused plugins by taking the following steps:

1. To easily find a list of installed modules on your Magento, open the file located here – <magento_directory>/app/etc/config.php. This file includes all extensions installed on your website, for example:

Magento 2 Speed Optimization 5

2. Then, choose the extensions that need to be disabled and select one of the ways to perform the task:

a) To disable the extension, open the file  <magento_directory>/app/etc/config.php and set value “0” next to the extension name. For instance:

'Plumrocket_SocialLoginFree' =&amp;gt; 1 is enabled module;
'Plumrocket_SocialLoginFree' =&amp;gt; 0 is disabled module.

b) Also, you can turn off the desired modules from the Console with command:

php bin/magento module:disable Plumrocket_SocialLoginFree

3. After that, in order to complete disabling the extensions, you should execute the following commands in the console:

php bin/magento setup:upgrade
php bin/magento setup:di:compile
php bin/magento setup:static-contend:deploy -f
php bin/magento cache:flush

Thus, it is recommended to avoid turning off the Magento default plugins (starts with prefix Magento_ ), as well as making any other changes if you don’t have experience working with Magento.

5. Cache Management

Caching is one of the main parts of any web server. With a platform like Magento, it is essential to obtain the cache as it drastically reduces the site load time and speeds up the page response that leads to a better user experience. Since there are different forms of caching that can be implemented with Magento, installation of Varnish, Redis and Memcached software can help you optimize the website performance. So, let’s look closer at every technology and find out the setup process.

Varnish

Known as a reverse caching proxy, Varnish is a piece of software that you put in front of the website server to diminish the site loading time by caching the server’s output. Before setting up Varnish with your Magento 2, make sure that Varnish is installed on your server. Then, consider the following steps to set up Varnish with your Magento 2 online store:

  1. Sign in to the Magento Admin as an administrator.
  2. Click STORES > Settings > Configuration > ADVANCED > System > Full Page Cache.
  3. After that, press Varnish Caching from the Caching Application list
  4. Then, enter a value in the TTL for public content file
  5. In order to expand Varnish Configuration, provide the following info:
Magento 2 Varnish

6. Click Save Config.

7. If you want to export a Varnish configuration file from the Admin panel:

  • Click one of the export buttons to create a varnish.vcl’  you can use with Varnish.
  • For instance, if you run Varnish 4, you can press Export VCL for Varnish 4.

8. Back up your current default.vcl’. After that, rename the default.vcl’ file you just exported to default.vcl’. Then, copy the file to the /etc/varnish/. directory.

cp /etc/varnish/default.vcl /etc/varnish/default.vcl.bak2
mv <download_directory>/varnish.vcl default.vcl
cp <download_directory>/default.vcl /etc/varnish/default.vcl

9. It is suggested to open default.vcl’ and change the value of acl purge to the IP address of the Varnish host. (Specify multiple hosts on separate lines or use CIDR notation as well.)

acl purge {
"localhost";
}

10. Restart Varnish and your web server:

service varnish restart

For Apache servers:

service httpd restart

For Nginx servers:

service nginx restart

Redis

Redis is an open-source, in-memory data structure store that can be used as a database, cache, and message broker. If you want to combine Redis as caching software with Magento 2, check out these step-by-step instructions:

1. Install Redis on website hosting.

  • If you are using Ubuntu, use the following commands to setup Redis:
sudo apt update sudo apt install redis-server
  • Follow the path /etc/redis/redis.conf to start Redis configuration. Find how to set up Redis here.
  • Enable Redis service with command sudo systemctl restart redis.service
  • Check Redis status with command sudo systemctl status redis

2) To set up Magento 2 with Redis, open file: <Magento install dir>app/etc/env.php and add the following configurations to save Magento cache in Redis:

'cache' =&amp;gt; [
'frontend' =&amp;gt; [
'default' =&amp;gt; [
'backend' =&amp;gt; 'Cm_Cache_Backend_Redis',
'backend_options' =&amp;gt; [
'server' =&amp;gt; '127.0.0.1',
'database' =&amp;gt; '0',
'port' =&amp;gt; '6379'
],
],
'page_cache' =&amp;gt; [
'backend' =&amp;gt; 'Cm_Cache_Backend_Redis',
'backend_options' =&amp;gt; [
'server' =&amp;gt; '127.0.0.1',
'port' =&amp;gt; '6379',
'database' =&amp;gt; '1',
'compress_data' =&amp;gt; '0'
]
]
]
],

3) In order to set up caching sessions in Redis, open <Magento install dir>app/etc/env.php and insert the following code:

'session' =&amp;gt;
array (
'save' =&amp;gt; 'redis',
'redis' =&amp;gt;
array (
'host' =&amp;gt; '127.0.0.1',
'port' =&amp;gt; '6379',
'password' =&amp;gt; '',
'timeout' =&amp;gt; '2.5',
'persistent_identifier' =&amp;gt; '',
'database' =&amp;gt; '2',
'compression_threshold' =&amp;gt; '2048',
'compression_library' =&amp;gt; 'gzip',
'log_level' =&amp;gt; '3',
'max_concurrency' =&amp;gt; '6',
'break_after_frontend' =&amp;gt; '5',
'break_after_adminhtml' =&amp;gt; '30',
'first_lifetime' =&amp;gt; '600',
'bot_first_lifetime' =&amp;gt; '60',
'bot_lifetime' =&amp;gt; '7200',
'disable_locking' =&amp;gt; '0',
'min_lifetime' =&amp;gt; '60',
'max_lifetime' =&amp;gt; '2592000'
)
),

Note, ‘server’ => ‘127.0.0.1’ і ‘port’ => ‘6379’ is the default value, yet it can differ in some cases.

4) Check whether Redis works with your Magento 2 with the help of the following command: redis-cli ping or redis-cli monitor. The response to your request should be “PONG.”

Memcached

Memcached is a distributed memory cache that speeds up large-scale web applications having dynamic databases. So, to reduce the load of database requests on a server with Redis software, follow a short guide to complete the procedure:

1) Open <your Magento install dir>/app/etc/env.php in a text editor. Locate the following:

'session' =&amp;gt;
array (
'save' =&amp;gt; 'files',
),

Change it as follows:

'session' =&amp;gt;
array (
'save' =&amp;gt; 'memcached',
'save_path' =&amp;gt; ':'
),

2) After the code is inserted, you should execute the following command in the Console:

rm -rf var/cache/ *var/page_cache/* var/session/*

6. Configure a Content Delivery Network (CDN)

Like all the above-mentioned aspects, the end purpose of CDN for Magento is to improve your website performance and enhance the customers’ experience. A CDN is a system of distributed servers that deliver web content to users from multiple locations. CDN is best implemented for sites that desire to reach global traffic. Copies of static assets like images, CSS style sheets, and Javascript files will be stored in different data centers and spread out globally. Among the variety of CDNs on the market, one of the most popular ones is considered Cloudflare. It provides you with an opportunity to protect the online business from harmful bots. The built-in security blocks the threats before they “hit” the server. As a result, you can prevent the server from losing traffic and resources that lead to Magento 2 speed improvement. So, if you want to set up Cloudflare on your Magento 2 website successfully, check out the useful information in the “Magento and Cloudflare: How to Make It Work Right” article.

7. Optimize the Images

It is well-known that images are an inevitable part of making your Magento 2 store’s performance more vivid. However, image sizes that are too heavy to upload on your site are one of the crucial reasons for Magento slow performance. In order to avoid this, you should compress the images before submitting them that can decrease the file size up to 70% without compromising its quality. As an example, you can use a compressing tool like Optimizilla. As you can see, the process of Magento 2 speed improvement can take a lot of time and effort. In this case, you can benefit from Magento 2 Speed Optimization Service provided by Plumrocket. A team of professionals will help you increase website speed significantly, enhance search rankings, boost sales, and much more. Select one of the plans to fit your business requirements and budget:

Magento 2 Speed Optimization Plans

Bottom Line

Getting your Magento 2 website to the highest performance level can be a challenging and time-consuming task. The above-mentioned tactics can help you simplify the speed optimization process of the online shop and accelerate your business processes. Also, you can take advantage of Magento 2 Speed Optimization Service to save time and effort, as well as open new company opportunities today!

About The Author: Anastasiia Tomashivska

Deeply passionate about making a good copy. Now exploring new horizons of SEO and content marketing. Apart from the digital world, she is obsessed with travelling, films and caffeine. View more posts