Home Technology peripherals It Industry 7 Easy Ways to Make a Magento 2 Website Faster

7 Easy Ways to Make a Magento 2 Website Faster

Feb 08, 2025 am 10:49 AM

7 Easy Ways to Make a Magento 2 Website Faster

Magento 2 e-commerce platform has been criticized for its speed problems, with slow product catalog pages and slow checkout process being common problems. This article will share seven practical tips to help you improve the running speed of the Magento 2 online store.

1. Use Varnish as cache application

Varnish is an HTTP proxy server that caches content and installs it in front of a web server to significantly improve website performance. Magento 2 has built-in support for Varnish. The activation method is as follows:

  1. Enter the admin panel > Store > Configuration > Advanced > System > Full-page cache and set "Cache Application" to Varnish Cache.

    7 Easy Ways to Make a Magento 2 Website Faster

  2. Expand the Varnish configuration tab and export the VCL file.

    7 Easy Ways to Make a Magento 2 Website Faster

Talk this file to your system administrator or host support team for Varnish daemon configuration.

2. Install the cache preheating tool

Magento 2 uses full page cache (FPC) to reduce server response time, but FPC's first request is usually slower. A cache warm-up tool (script or extension) can make these requests in advance, populating the cache storage, thereby reducing first-byte time (TTFB). You can install Magento 2 module (paid or free) as a cache preheating tool, or create a simple PHP script that warms up all categories and most popular pages:

ini_set('memory_limit','12000M');
use Magento\Framework\App\Bootstrap;
require __DIR__.'/app/bootstrap.php';
$params = $_SERVER;
$bootstrap = Bootstrap::create(BP,$params);
$obj = $bootstrap->getObjectManager();
$state = $obj->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');
$categories = $obj->create('Magento\Catalog\Model\ResourceModel\Category\Collection');
$categories->addIsActiveFilter()
           ->joinUrlRewrite();
foreach($categories as $cat){
   $st = microtime(true);
   $dd = file_get_contents_ssl($cat->getUrl());
   $fn = microtime(true);
   if(($fn - $st) > 0.9)
    echo $cat->getUrl()." : time: ".($fn - $st)."\n";
   sleep(3);
}
$open = fopen("1000-popular-pages.csv","r");
while(($data = fgetcsv($open,4000,",")) !== FALSE){
    if(filter_var($data[0],FILTER_VALIDATE_URL) !== FALSE && strpos($data[0],".pdf") === FALSE && strpos($data[0],"/blog/") === FALSE){
      $st = microtime(true);
      $dd = file_get_contents_ssl($data[0]);
      $fn = microtime(true);
      if(($fn - $st) > 0.9)
       echo $data[0]." : time: ".($fn - $st)."\n";
      sleep(3); 
    }
}
fclose($open);

function file_get_contents_ssl($url) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_REFERER, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3000); 
    curl_setopt($ch, CURLOPT_TIMEOUT, 10000); 
    $result = curl_exec($ch);
    if($result === FALSE)
       $result = curl_error($ch);
    curl_close($ch);
    return $result;
}
Copy after login
Copy after login

You can export a list of popular pages from Google Analytics.

3. Move JavaScript code to the bottom of the page

Moving JavaScript code to the bottom of the page can improve the speed of drawing content on the first screen. Magento 2.4 provides corresponding management settings, or use the command line:

php bin/magento config:set  dev/js/move_script_to_bottom 1
php bin/magento cache:flush
Copy after login

4. Convert the image to WebP format

WebP images take up less disk space than JPEG and PNG. Converting website images to WebP format can reduce page size and improve performance. You can use the cwebp command line tool to convert:

cwebp -q 80 image.png image.webp
Copy after login

(-q parameter setting quality, here is 80). Magento 2 also has some modules that can implement this transformation.

5. Enable HTML compression

HTML compression helps reduce page size and speed up. Magento 2.4 Compress HTML without additional modules. Enable method:

php bin/magento config:set dev/template/minify_html 1
php bin/magento deploy:mode:set production
Copy after login

6. Compress and merge JavaScript and CSS files

Compressing and merging JS and CSS files helps reduce page size and reduce HTTP requests, thus speeding up your website. Enable method:

php bin/magento config:set dev/js/merge_files 1
php bin/magento config:set dev/css/merge_css_files 1
php bin/magento config:set dev/js/minify_files 1
php bin/magento config:set dev/css/minify_files 1
php bin/magento deploy:mode:set production
Copy after login

7. Cache ElasticSearch query results

Magento 2.4 Use the ElasticSearch engine for indexing and directory management. For large directories, cache query results can improve ElasticSearch performance. Open the vendor/elasticsearch/elasticsearch/src/Elasticsearch/Connections/Connection.php file and add the following code near about 365 lines:

ini_set('memory_limit','12000M');
use Magento\Framework\App\Bootstrap;
require __DIR__.'/app/bootstrap.php';
$params = $_SERVER;
$bootstrap = Bootstrap::create(BP,$params);
$obj = $bootstrap->getObjectManager();
$state = $obj->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');
$categories = $obj->create('Magento\Catalog\Model\ResourceModel\Category\Collection');
$categories->addIsActiveFilter()
           ->joinUrlRewrite();
foreach($categories as $cat){
   $st = microtime(true);
   $dd = file_get_contents_ssl($cat->getUrl());
   $fn = microtime(true);
   if(($fn - $st) > 0.9)
    echo $cat->getUrl()." : time: ".($fn - $st)."\n";
   sleep(3);
}
$open = fopen("1000-popular-pages.csv","r");
while(($data = fgetcsv($open,4000,",")) !== FALSE){
    if(filter_var($data[0],FILTER_VALIDATE_URL) !== FALSE && strpos($data[0],".pdf") === FALSE && strpos($data[0],"/blog/") === FALSE){
      $st = microtime(true);
      $dd = file_get_contents_ssl($data[0]);
      $fn = microtime(true);
      if(($fn - $st) > 0.9)
       echo $data[0]." : time: ".($fn - $st)."\n";
      sleep(3); 
    }
}
fclose($open);

function file_get_contents_ssl($url) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_REFERER, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3000); 
    curl_setopt($ch, CURLOPT_TIMEOUT, 10000); 
    $result = curl_exec($ch);
    if($result === FALSE)
       $result = curl_error($ch);
    curl_close($ch);
    return $result;
}
Copy after login
Copy after login

This will enable the ElasticSearch internal query caching mechanism.

Summary

This article introduces seven ways to improve the speed of Magento 2 websites: using Varnish as a full-page cache, setting up cache preheating tool, lazy loading of JavaScript, converting all images to WebP, enabling HTML compression, compressing and merging JS and CSS Files and cache ElasticSearch query results. These steps will improve server response time and core network metrics.

The above is the detailed content of 7 Easy Ways to Make a Magento 2 Website Faster. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1267
29
C# Tutorial
1239
24
CNCF Arm64 Pilot: Impact and Insights CNCF Arm64 Pilot: Impact and Insights Apr 15, 2025 am 08:27 AM

This pilot program, a collaboration between the CNCF (Cloud Native Computing Foundation), Ampere Computing, Equinix Metal, and Actuated, streamlines arm64 CI/CD for CNCF GitHub projects. The initiative addresses security concerns and performance lim

Serverless Image Processing Pipeline with AWS ECS and Lambda Serverless Image Processing Pipeline with AWS ECS and Lambda Apr 18, 2025 am 08:28 AM

This tutorial guides you through building a serverless image processing pipeline using AWS services. We'll create a Next.js frontend deployed on an ECS Fargate cluster, interacting with an API Gateway, Lambda functions, S3 buckets, and DynamoDB. Th

Top 21 Developer Newsletters to Subscribe To in 2025 Top 21 Developer Newsletters to Subscribe To in 2025 Apr 24, 2025 am 08:28 AM

Stay informed about the latest tech trends with these top developer newsletters! This curated list offers something for everyone, from AI enthusiasts to seasoned backend and frontend developers. Choose your favorites and save time searching for rel

See all articles