Teamitek

Configuration of Magento with Redis

Requirements:

  1. Ubuntu 18.04  , 2CPU, 4GB ram( t2.medium on AWS )
  2. Install Nginx Webserver
  3. Install and Configure PHP-FPM 7.4
  4. Install and Configure MySQL Server
  5. Install PHP Composer
  6. Download and Install Magento
  7. Generate SSL via Letsencrypt
  8. Setup Nginx Virtualhost for Magento
  9. Post-installation
  10. Install and configure redis for magento

Step1 – Launch an Ec2 instance of Ubuntu 18.04 (t2.medium).

Step2 – Install Nginx:

Run the following command to install and start it

  • sudo apt install nginx
  • sudo  systemctl enable –now nginx (to start and and enable the nginx service)

Open port 80 and 443 in security group of the instance

Or allow http and https in firewall traffic with the following command.

  • sudo ufw app list
  • sudo ufw allow in “Nginx Full”  # This will open both 80 and 443 port
  • sudo ufw enable

Step3 – Install and configure PHP:

Install the ‘software-properties-common’ package and add the PPA repository for PHP 7.4

  • sudo apt install software-properties-common
  • sudo add-apt-repository ppa:ondrej/php

Install PHP-FPM 7.4 packages from the PPA repository

  • sudo apt install php7.4-fpm php7.4-common php7.4-curl php7.4-cli php7.4-mysql php7.4-gd php7.4-xml php7.4-json php7.4-intl php-pear php7.4-dev php7.4-common php7.4-mbstring php7.4-zip php7.4-soap php7.4-bcmath php7.4-opcache -y

Go to the ‘/etc/php/7.4/fpm’ directory and edit the ‘php.ini’ 

Uncomment and change the configuration:

  • date.timezone = Asia/Singapore
  • memory_limit = 1G
  • max_execution_time = 1800
  • zlib.output_compression = On
  • cgi.fix_pathinfo = 0
  • opcache.enable=1
  • opcache.save_comments = 1

Next, start the PHP-FPM service and enable it.

  • systemctl enable –now php7.4-fpm

Step4 – Install and configure Database:

  • sudo apt install mariadb-server
  • systemctl enable –now mariadb

Set root password with “mysql_secure_installation”

  • Set a root password? [Y/n] Y
  • Remove anonymous users? [Y/n] Y
  • Disallow root login remotely? [Y/n] Y
  • Remove test database and access to it? [Y/n] Y
  • Reload privilege tables now? [Y/n] Y

Log in to the MariaDB/MySQL shell, create a new user, password and database for our Magento installation.

  • create database magentodb;
  • create user magentouser@’localhost’ identified by ‘magentopassdb’;
  • grant all privileges on magentodb.* to magentouser@’localhost’;
  • flush privileges;

Step5 – Install PHP Composer:

  • php -r “copy(‘https://getcomposer.org/installer’, ‘composer-setup.php’);”
  • php -r “if (hash_file(‘sha384’, ‘composer-setup.php’) === ‘756890a4488ce9024fc62c56153228907f1545c228516cbf63f885e036d37e9a59d27d63f46af1d4d07ee0f76181c7d3’) { echo ‘Installer verified’; } else { echo ‘Installer corrupt’; unlink(‘composer-setup.php’); } echo PHP_EOL;”
  • sudo php composer-setup.php –version=1.10.17 –install-dir=/usr/bin –filename=composer
  • composer –version

Step6 – Download magento:

Install the Magento 2, and all PHP packages needed using the Composer.

Go to the ‘/var/www/’ directory and download the Magento 2.3.5 CE (Community Edition) source code using wget command

  • cd /var/www/
  • sudo wget -q https://github.com/magento/magento2/archive/2.3.5.tar.gz
  • sudo tar -xf 2.3.5.tar.gz
  • sudo mv magento2-*/ magento2/
  • cd /var/www/magento2/
  • composer install

Run the commands below to set the correct permissions for Magento to function properly.

  • sudo chown -R www-data:www-data /var/www/html/magento/
  • sudo chmod -R 755 /var/www/html/magento/

Step7 – Generate ssl certificate  

Generate the SSL Letsencrypt using the certbot command. Also, make sure that your domain name is resolved to the server IP address.

  • sudo apt install certbot
  • systemctl stop nginx
  • certbot certonly –standalone –agree-tos –no-eff-email –email akashdaswani6@gmail.com -d magento.techakash.ml

You will get your SSL certificates at the ‘/etc/letsencrypt/live/magento.your-domain.com’ directory.

Step8 – Setup VirtualHost for Magento:

  • cd /etc/nginx/sites-available/
  • vim magento

Paste the following configuration into it

upstream fastcgi_backend {

server unix:/run/php/p

}

server {

listen 80;

listen [::]:80;

server_name magento.techakash.ml;

return 301 https://$server_name$request_uri;

}

server {

listen 443 ssl http2;

     server_name magento.techakash.ml;

     ssl_certificate /etc/letsencrypt/live/magento.techakash.ml/fullchain.pem;

     ssl_certificate_key /etc/letsencrypt/live/magento.techakash.ml/privkey.pem;

     set $MAGE_ROOT /var/www/magento2;

     set $MAGE_MODE developer;

     include /var/www/magento2/nginx.conf.sample;

}

Save and close the file:

Activate the Magento Nginx virtualhost, test it if there is any error.

  • ln -s /etc/nginx/sites-available/magento /etc/nginx/sites-enabled/
  • Nginx -t
  • systemctl restart nginx

Step9 post-installation:

  • Open the browser and type the domain you configured for magento,
  • And do the required setup from that console
  • At the end you will get the magento admin url

Step 10 Install and configure redis for magento:

1. Introduction

Redis is an application that runs on your web server and is designed to improve the performance of applications such as Magento.

2. Installing Redis:

Add and update repo then install redis

  • sudo add-apt-repository ppa:chris-lea/redis-server
  • sudo apt-get update
  • sudo apt-get install redis-server -y

3. Configure Redis:

  • sudo vim /etc/redis/redis.conf
  • Search for “supervisor no” in the file
  • Replace it with “supervisor systemd”

For changes to take effect with need to restart the redis service

sudo systemctl restart redis-server

4. Testing redis:

Connect to redis and test it with following command

  • Redis-cli
  • In redis terminal run: “ping”
  • Redis acknowledge us with “pong”

5. Setup password:

Generate a strong password with “openssl rand 60 | openssl base64 -A” copy password somewhere

Open redis configuration file and do the following:

  • sudo vim /etc/redis/redis.conf
  • Search for “requirepass” uncomment it and paste your password
  • requirepass “xxxx” (replace xxxx with your password that is generated above)
  • sudo systemctl restart redis-server

6. Test password:

  • redis-cli
  • set test “Hello!”

Redis will reject this command because we have not authorized ourselves, we need to authenticate once only.

  •  auth  XXXX (replace your password with xxxx)
  • Set test “hello” (output: ok)
  • Get test (output: hello)
  • Exit

7. Setup redis for magento:

  • Cd /var/www/magento2/

Configure redis for default caching:

Replace xxxx with your password in next commands

  • bin/magento setup:config:set –cache-backend=redis –cache-backend-redis-server=127.0.0.1 –cache-backend-redis-password=XXXX –cache-backend-redis-db=0

Configure redis for page caching:

  • bin/magento setup:config:set –page-cache=redis –page-cache-redis-server=127.0.0.1 –page-cache-redis-password=XXXX –page-cache-redis-db=1

Configure redis for session caching:

  • bin/magento setup:config:set –session-save=redis –session-save-redis-host=127.0.0.1 –session-save-redis-log-level=3 –session-save-redis-max-concurrency=12 –session-save-redis-password=XXXX –session-save-redis-db=2

If you need help in configuring this cluster, feel free to email us victor@teamitek.com or call us at +1 810 214 2572