As one of best solution when it comes to e-commerce websites, Magento is often known for slowness on processing pages and this doc explains how to achieve Magento Server Optimization with Redis, Varnish and Nginx for faster performance.
A few common methods that will help to Optimize any web application can be used in Magento as well. Like;

  • Latest Magento Version
  • CDN
  • Image optimization
  • Magento Cache management
  • Page caching extensions
  • Combine CSS and JS files
  • Reducing HTTP requests

One will definitely see performance improvement once the above tweaks are applied. But that is not the end of what a System Admin can do to make your website faster.

Welcome to the world of Magento Server Side Optimization

Magento being an e-commerce solution handles a lot of data- information about the products, pricing and the framework itself is resource intensive. The good news is, a lot of this can be cached for faster serving. Three of the best solutions are:

  • Varnish – “is a web application accelerator also known as a caching HTTP reverse proxy. You install it in front of any server that speaks HTTP and configure it to cache the contents. Varnish Cache is really, fast. It can also be used to load balance the backend web application. “
  • Redis Cache – is another caching mechanism with an advantage of structured data store.
  • Ngnix – is the ‘Thor’s hammer’. It is an HTTP and reverse proxy server which provides amazing results when comes to serving high traffic/content websites.

1.Cache backend and Session storage

By default, Magento stores it’s cache entries in the file system. That is fine with low traffic websites. But read/writes will become an overhead, if you expect high traffic on your magento store, as the cache grows from time to time.
The options we have is to use APC, MemCache or Redis for the cache.
I would recommend Redis, as it is a very fast cache backend with full cache tag support, and therefore no slow level file system cache is needed and better in terms of memory usage as well.

Install Redis

We should install/enable EPEL repo and get Redis server installed and configured
#wget http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
#rpm -Uvh epel-release-6*.rpm
#rpm --import http://dl.fedoraproject.org/pub/epel/RPM-GPG-KEY-EPEL-6
#yum install redis
#chkconfig redis on
#service redis start
#service httpd restart
The integration between Redis and Magento is already done in Magento CE 1.8 and EE 1.13 and later versions, it just need to be configured.

To configure Magento to use Redis

  • Enable the Cm_RedisSession module
  • In app/etc/modules/Cm_RedisSession.xml change the value of active to true
  • In app/etc/local.xml make the below changes

<!– Sample config for redis cache –>

   <cache>

       <backend>Cm_Cache_Backend_Redis</backend>

       <backend_options>

           <server>127.0.0.1</server> <!– or absolute path to unix socket for better performance –>

           <port>6379</port>

           <database>0</database>

           <password></password>

           <force_standalone>0</force_standalone>  <!– 0 for phpredis, 1 for standalone PHP –>

           <connect_retries>1</connect_retries>    <!– Reduces errors due to random connection failures –>

           <automatic_cleaning_factor>0</automatic_cleaning_factor> <!– Disabled by default –>

           <compress_data>1</compress_data>  <!– 0-9 for compression level, recommended: 0 or 1 –>

           <compress_tags>1</compress_tags>  <!– 0-9 for compression level, recommended: 0 or 1 –>

           <compress_threshold>20480</compress_threshold>  <!– Strings below this size will not be compressed –>

           <compression_lib>gzip</compression_lib> <!– Supports gzip, lzf and snappy –>

           <persistent>1</persistent> <!– persistence value, 0: not in use, > 0 used as persistence ID –>

       </backend_options>

   </cache>

  • Flush the Magento cache

2.Full Page cache

Varnish is a web application accelerator, which can work as a load balancer and caching reverse proxy. If cached in Varnish it will reduce the load time significantly as the cached page is served directly from RAM. Varnish can serve static content faster even when there is high traffic on the website. But to serve dynamic content Varnish is not a good choice as it fetches the content from the backend server, and we have to deal with SSL as well.
Varnish+Nginx would be the best choice here as Varnish doesn’t support SSL connections. Nginx is used to handle SSL termination and HTTPS redirection.

Install Nginx

Install Nginx, and configure the domain’s configuration file. A sample entry will look like;

upstream fastcgi_backend {
server unix:/run/php/php7.1-fpm.sock;
}
server {
server_name livedomain.com www.livedomain.com;
listen 80;
set $MAGE_ROOT /var/www/livedomain.com;
set $MAGE_MODE developer; # developer/production
access_log /var/log/nginx/livedomain.com-access.log;
error_log /var/log/nginx/livedomain.com-error.log;
include /var/www/livedomain.com/nginx.conf.sample;
}

Enable and Start Nginx Server

#systemctl enable nginx
#service nginx start

Install Varnish

We have already enabled EPEL repo in the first step. So just use yum to install varnish.
#yum install varnish
From Magento Admin dashboard set Varnish Cache as Full Page Cache and export the VCL.
Configuration -> ADVANCED -> System -> Full Page Cache
Replace the /etc/varnish/default.vcl file with the contents of exported VCL.
By default Varnish listens on port 6081
Change VARNISH_LISTEN_PORT from 6081 to 80, in /etc/varnish/varnish.params
In /etc/varnish/default.vcl tell Varnish to get the content on port 8080.

backend default {
.host = “127.0.0.1”;
.port = “8080”;
}

Configure Nginx for SSL termination

Change Nginx listening port from 80 to 8080 and enable Nginx SSL termination with HTTP2

upstream fastcgi_backend {
server unix:/run/php/php7.1-fpm.sock;
}
server {
server_name livedomain.com www.livedomain.com;
listen 8080;
set $MAGE_ROOT /var/www/livedomain.com;
set $MAGE_MODE developer; # developer/production
access_log /var/log/nginx/livedomain.com-access.log;
error_log /var/log/nginx/livedomain.com-error.log;
include /var/www/livedomain.com/nginx.conf.sample;
}
server {
listen 443 ssl http2;
server_name livedomain.com www.livedomain.com;
ssl_certificate /etc/ssl/certs/livedomain.com.pem; # Domain’s ssl cert
ssl_certificate_key /etc/ssl/private/livedomain.com.key; # Domain’s ssl key
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers ‘AES128+EECDH:AES128+EDH:!aNULL’;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 24h;
keepalive_timeout 300s;
location / {
proxy_pass http://127.0.0.1;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Ssl-Offloaded “1”;
proxy_set_header X-Forwarded-Proto https;
Proxy_set_header X-Forwarded-Port 443;
#proxy_hide_header X-Varnish;
#proxy_hide_header Via;
proxy_set_header X-Forwarded-Proto $scheme;
}

Next, you have to restart Nginx and Varnish.
#service nginx restart
#service varnish restart
For the best e-commerce experience it not only means to have good product lineup but also serving the visitors with quality contents in a faster manner. This is where a seasoned server admin can help to Optimize the server load, install and configure plugins and tools to make the experience seamless compared to any solutions out-there.
Get in touch with us for a quick chat about how your Magento store can be optimized.

Get new insights right to your inbox

How can our experts help you?

Schedule your consultation

You may also like

  • By admin
  • in DevOps

Agile vs DevOps: What’s the difference

  • Nov 18, 2022 .
  • 9 min min
Read More
  • By admin
  • in DevOps

DevOps as a Service: All You Should Know

  • Aug 9, 2022 .
  • 9 min min
Read More
  • By admin
  • in Containerization

Containerization VS Virtualization: Understanding the Differences

  • Aug 4, 2022 .
  • 8 min min
Read More

Be in the know

Techno tips served hot! Subscribe now and stay atop.