Step-by-Step Guide to Installing Magento 2 with Nginx and Let’s Encrypt SSL on Ubuntu 20.04 LTS

Magento is a popular open-source e-commerce software and content management system (CMS) for creating online stores. Built on the PHP Zend Framework, it uses MySQL or MariaDB as its database backend. The development of Magento was initiated in 2008 by Varien.

This tutorial will guide you through the process of installing Magento 2 on the latest version of Ubuntu 20.04. We will be setting it up under the LEMP stack, which includes Nginx, PHP-FPM 7.3, and MariaDB.

Requirements

You’ll need a system running Ubuntu 20.04 with the following specifications:

  • 2 GB RAM
  • 50 GB free disk space
  • 2 CPUs

What We Will Do?

  • Install Nginx Web Server
  • Install and Configure PHP-FPM 7.3
  • Install and Configure MariaDB Server
  • Install PHP Composer
  • Download and Install Magento
  • Generate SSL with Let’s Encrypt
  • Setup Nginx Virtual Host for Magento
  • Magento Post-Installation Steps

Step 1: Install Nginx Web Server

The first step involves installing the Nginx web server on your Ubuntu 20.04 system. You can achieve this using the following command:

sudo apt install nginx

After installation completes, start the Nginx service and enable it to launch at system boot using:

systemctl start nginx
systemctl enable nginx

Verify that Nginx is running:

systemctl status nginx

Install Nginx

Now, open HTTP and HTTPS ports on the Ubuntu system with:

for svc in ssh http https
do
ufw allow $svc
done

Enable the UFW firewall:

ufw enable

Type ‘y’ to confirm.

Step 2: Install and Configure PHP-FPM 7.3

Next, let’s install PHP-FPM 7.3, which is required for Magento. Start by adding the PHP 7.3 PPA repository:

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

Once the repository is updated, install PHP-FPM 7.3 and the related packages:

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

Edit the PHP-FPM configuration:

cd /etc/php/7.3/
vim fpm/php.ini

Modify the following settings:

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

Save and close the file. Then, start and enable PHP-FPM:

systemctl start php7.3-fpm
systemctl enable php7.3-fpm

Install and configure PHP-FPM 7.3

Verify PHP-FPM is operational:

ss -xa | grep php
systemctl status php7.3-fpm

Check PHP-FPM Service

Step 3: Install and Configure MariaDB Server

Now, install MariaDB server:

sudo apt install mariadb-server

Start and enable MariaDB service:

systemctl start mariadb
systemctl enable mariadb

Secure MariaDB installation:

mysql_secure_installation

Answer ‘Y’ for all questions. Set a strong root password.

Setup MariaDB root password

Create a database and user for Magento:

mysql -u root -p
create database magentodb;
create user magentouser@'localhost' identified by 'magentopassdb';
grant all privileges on magentodb.* to magentouser@'localhost';
flush privileges;

Exit MySQL:

exit

Create Database for Magento

Step 4: Install Composer

Composer is a PHP dependency manager essential for Magento. Install Composer using:

sudo apt install composer -y

Confirm the installation by checking the version:

composer --version
Composer 1.10.1 2020-03-13 20:34:27

Step 5: Download and Install Magento 2

Proceed to download and set up Magento 2:

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

Install necessary PHP packages:

cd /var/www/magento2/
composer install

Download and Install Magento 2

Set directory permissions:

chown -R www-data:www-data /var/www/magento2

Step 6: Generate SSL with Let’s Encrypt

Secure your Magento installation with Let’s Encrypt SSL:

sudo apt install certbot
systemctl stop nginx
certbot certonly --standalone --agree-tos --no-eff-email --email your-email@example.com -d magento.your-domain.com

Step 7: Set Up Nginx Virtual Host for Magento 2

Create the following Nginx configuration:

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

Add the following configuration:

upstream fastcgi_backend {
        server  unix:/run/php/php7.3-fpm.sock;
}
server {
listen 80;
listen [::]:80;
server_name magento.hakase-labs.io;
return 301 https://$server_name$request_uri;
}

server {

listen 443 ssl http2;
server_name magento.hakase-labs.io;

ssl_certificate /etc/letsencrypt/live/magento.hakase-labs.io/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/magento.hakase-labs.io/privkey.pem;

set $MAGE_ROOT /var/www/magento2;
set $MAGE_MODE developer;
include /var/www/magento2/nginx.conf.sample;
}

Activate and test the configuration:

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

Setup Nginx virtualhost for Magento

Step 8: Magento Post-Installation Steps

Open a browser and navigate to your Magento domain (https://magento.your-domain.com/) to access the Magento setup wizard. Follow the on-screen instructions to complete the installation.

Install Magento 2

Reference

Frequently Asked Questions (FAQ)

1. Can I install Magento 2 on a lower version of Ubuntu?

While it’s technically possible to install Magento 2 on lower versions of Ubuntu, it’s recommended to use Ubuntu 20.04 to ensure compatibility and to receive updates and patches.

2. Why is PHP-FPM 7.3 necessary when PHP 7.4 is available?

Magento 2 has compatibility issues with PHP 7.4, thus requiring the use of PHP-FPM 7.3 for a stable environment.

3. What should I do if I face issues with SSL certificate during setup?

Verify that your domain name is correctly pointed to your server’s IP address. Ensure that all necessary ports are open and try regenerating the SSL certificate.

4. Is it possible to secure the installation without using Let’s Encrypt?

Yes, you can use a paid SSL certificate from a trusted certificate authority of your choice if preferred.