国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

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;
}

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

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

(-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

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

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;
}

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

Undress AI Tool

Undress AI Tool

Undress images for free

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.

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)

The Developer's Shortcut To Your Udemy-like Platform The Developer's Shortcut To Your Udemy-like Platform Jun 17, 2025 pm 04:43 PM

When developing learning platforms similar to Udemy, the focus isn't only on content quality. Just as important is how that content is delivered. This is because modern educational platforms rely on media that is accessible, fast, and easy to digest.

Cost Effective Reseller Platforms for Buying SSL Certificates Cost Effective Reseller Platforms for Buying SSL Certificates Jun 25, 2025 am 08:28 AM

In a world where online trust is non-negotiable, SSL certificates have become essential for every website. The market size of SSL certification was valued at USD 5.6 Billion in 2024 and is still growing strongly, fueled by surging e-commerce business

5 Best Payment Gateways for SaaS: Your Ultimate Guide 5 Best Payment Gateways for SaaS: Your Ultimate Guide Jun 29, 2025 am 08:28 AM

A payment gateway is a crucial component of the payment process, enabling businesses to accept payments online. It acts as a bridge between the customer and the merchant, securely transferring payment information and facilitating transactions. For

New study claims AI 'understands' emotion better than us — especially in emotionally charged situations New study claims AI 'understands' emotion better than us — especially in emotionally charged situations Jul 03, 2025 pm 05:48 PM

In what seems like yet another setback for a domain where we believed humans would always surpass machines, researchers now propose that AI comprehends emotions better than we do.Researchers have discovered that artificial intelligence demonstrates a

Hurricanes and sandstorms can be forecast 5,000 times faster thanks to new Microsoft AI model Hurricanes and sandstorms can be forecast 5,000 times faster thanks to new Microsoft AI model Jul 05, 2025 am 12:44 AM

A new artificial intelligence (AI) model has demonstrated the ability to predict major weather events more quickly and with greater precision than several of the most widely used global forecasting systems.This model, named Aurora, has been trained u

Your devices feed AI assistants and harvest personal data even if they’re asleep. Here's how to know what you're sharing. Your devices feed AI assistants and harvest personal data even if they’re asleep. Here's how to know what you're sharing. Jul 05, 2025 am 01:12 AM

Like it or not, artificial intelligence has become part of daily life. Many devices — including electric razors and toothbrushes — have become AI-powered," using machine learning algorithms to track how a person uses the device, how the devi

Would outsourcing everything to AI cost us our ability to think for ourselves? Would outsourcing everything to AI cost us our ability to think for ourselves? Jul 03, 2025 pm 05:47 PM

Artificial intelligence (AI) began as a quest to simulate the human brain.Is it now in the process of transforming the human brain's role in daily life?The Industrial Revolution reduced reliance on manual labor. As someone who researches the applicat

Advanced AI models generate up to 50 times more CO₂ emissions than more common LLMs when answering the same questions Advanced AI models generate up to 50 times more CO₂ emissions than more common LLMs when answering the same questions Jul 06, 2025 am 12:37 AM

The more precisely we attempt to make AI models function, the greater their carbon emissions become — with certain prompts generating up to 50 times more carbon dioxide than others, according to a recent study.Reasoning models like Anthropic's Claude

See all articles