How to Install Magento 2 with Nginx and Let’s Encrypt SSL on Ubuntu 20.04 LTS

How to Install Magento 2 with Nginx and Let’s Encrypt SSL on Ubuntu 20.04 LTS

How to Install Magento 2 with Nginx and Let’s Encrypt SSL on Ubuntu 20.04 LTS

Magento is a widely-used open source e-commerce software and content management system for e-commerce websites based on the PHP Zend Framework. It uses MySQL or MariaDB as the database backend. The development of Magento was started in 2008 by Varien.eval(ez_write_tag([[728,90],’howtoforge_com-box-3′,’ezslot_6′,106,’0′,’0′]));

In this tutorial, we will show you step by step how to install Magento 2 on the latest version Ubuntu 20.04. We will install Magento under the LEMP stack, the Nginx web server, PHP-FPM 7.3, and MariaDB server.eval(ez_write_tag([[580,400],’howtoforge_com-medrectangle-3′,’ezslot_2′,121,’0′,’0′]));


For this tutorial, we will install the Magento 2 on the Ubuntu 20.04 system with 2 GB RAM, 50 GB free disk space and 2 CPUs.

What we will do?

  • Install Nginx Webserver
  • Install and Configure PHP-FPM 7.3
  • Install and Configure MySQL Server
  • Install PHP Composer
  • Download and Install Magento
  • Generate SSL Letsencrypt
  • Setup Nginx Virtualhost for Magento
  • Magento Post-Installation

Step 1 – Install Nginx Webserver

First, we will install the Nginx webserver to the Ubuntu 20.04 system.

Install Nginx using the apt command below.

sudo apt install nginx

Once all installation is complete, start the Nginx service and add it to the system boot.

systemctl start nginx
systemctl enable nginx

After that, check the Nginx service status using the following command.

systemctl status nginx

The Nginx web server is up and running on the Ubuntu 20.04 Server.eval(ez_write_tag([[580,400],’howtoforge_com-medrectangle-4′,’ezslot_1′,108,’0′,’0′]));

Install Nginx

Next, we will open the HTTP and HTTPS ports on our Ubuntu system.

Run the ufw command below.

for svc in ssh http https
ufw allow $svc

As a result, the SSH, HTTP, and HTTPS services have been added to the ufw firewall rule.

Now run the enable the ufw firewall using the following command.

ufw enable

Type ‘y’ and press Enter, and you’ve enabled the ufw firewall.

Step 2 – Install and Configure PHP-FPm 7.3

After installing the Nginx web server, we will install the PHP-FPM to our Ubuntu 20.04 Server.

By default, the Ubuntu 20.04 comes with the newest version of PHP/PHP-FPM 7.4. But for now, the Magento only works with PHP/PHP-FPM 7.3. And in this step, we will install the PHP-FPM 7.3 from the PPA repository.

Before going any further, install the ‘software-properties-common’ package and add the PPA repository for PHP 7.3 using the following command.

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

The ‘add-apt-repository’ will automatically update all repositories on the system.

Now install PHP-FPM 7.3 packages from the PPA repository using the apt command below.

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

And the PHP-FPM 7.3 installation has been completed.

After that, go to the ‘/etc/php/7.3’ directory and edit the ‘php.ini’ configuration for PHP-FPM.

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

Uncomment and change the configuration as below.

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

opcache.save_comments = 1

Save and close.

Next, start the PHP-FPM service and add it to the system boot.

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

Install and configure PHP-FPM 7.3

The PHP-FPM service is up and running, check it using the command below.

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

As a result, the PHP-FPM is up and running on the Ubuntu 20.04 server, it’s running under the sock file ‘/run/php/php7.3-fpm.sock’.

Check PHP-FPM Service

Step 3 – Install and Configure MariaDB Server

In this step, we will install the MariaDB database server, setup the default root password for MariaDB, and create a new database and user for Magento.

Install MariaDB Server packages to the Ubuntu 20.04 using the apt command below.

sudo apt install mariadb-server

Once all installation is completed, start the MariaDB service and add it to the system boot.

systemctl start mariadb
systemctl enable mariadb

The MariaDB service is up and running.

Next, we will set up the MariaDB root password using the ‘mysql_secure_instalaltion’ utility.

Run the ‘mysql_secure_instalaltion’ command below.


Type your strong password for MariaDB root user and type ‘Y’ for all configuration.

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

As a result, the MariaDB root password has been configured.

Setup MariaDB root password

Next, we will create a new database and user for the Magento installation.

Log in to the MariaDB/MySQL shell using the mysql command below.

mysql -u root -p

Now create a new database for our Magento installation. Create the database ‘magentodb’ and the user ‘magentouser’ with the password ‘magentodbpass’.

Run the MySQL queries below.

create database magentodb;
create user [email protected]'localhost' identified by 'magentopassdb';
grant all privileges on magentodb.* to [email protected]'localhost';
flush privileges;

Now type ‘exit’ to log out from the MySQL shell.

Create Database for Magento

As a result, the new database and user for Magento installation has been completed.

Step 4 – Install Composer

Composer is a dependency manager for PHP. it’s an application-level package manager for PHP that allows you to download additional PHP dependencies and libraries to your PHP project.

In this step, we will install the PHP Composer from the Ubuntu repository.

Install Composer using the apt command below.

sudo apt install composer -y

Once the installation is completed, check the composer version below.

composer --version

Below is the response you will get.

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

As can be seen, the Composer 1.10 has been installed on the ubuntu 20.04 Server.

Step 5 – Download and Install Magento 2

In this step, we will download and install the Magento 2, and all PHP packages needed using the Composer.

First, go to the ‘/var/www/’ directory and download the Magento 2.3.5 CE (Community Edition) source code using wget command as below.

cd /var/www/
wget -q

Once all is completed, extract the Magento source code and rename the extracted directory to ‘magento2’ directory.

tar -xf 2.3.5.tar.gz
mv magento2-*/ magento2/

Next, go to the Magento installation directory ‘/var/www/magento2’ and install all PHP packages dependencies for Magento using the Composer command as below.

cd /var/www/magento2/
composer install

Download and Install Magento 2

Once all installation is completed, fix the ownership of the Magento 2 installation directory to the user ‘www-data’.

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

As a result, the installation of Magento 2 and all packages dependencies has been completed.

Step 6 – Generate SSL Letsencrypt

For this tutorial, we will secure the Magento 2 installation using the SSL Letsencrypt. And in this step, we will generate the SSL Letsencrypt using the certbot command. Also, make sure that your domain name is resolved to the server IP address.

Install the certbot tool using the apt command below.

sudo apt install certbot

Once the installation is completed, stop the Nginx service.

systemctl stop nginx
certbot certonly --standalone --agree-tos --no-eff-email --email [email protected] -d

Once all is completed, you will get your SSL certificates at the ‘/etc/letsencrypt/live/’ directory.

As a result, the SSL Letsencrypt for Magento 2 installation has been generated.

Step 7 – Set up Nginx Virtualhost for Magento 2

In this step, we will add a new Nginx virtualhost configuration for Magento 2.

Go to the ‘/etc/nginx/sites-available/’ directory and create a new configuration ‘magento’ using vim editor.

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

Paste the following configuration into it.

upstream fastcgi_backend {
        server  unix:/run/php/php7.3-fpm.sock;

server {
    listen 80;
    listen [::]:80;
    return 301 https://$server_name$request_uri;

server {

        listen 443 ssl http2;

        ssl_certificate /etc/letsencrypt/live/;
        ssl_certificate_key /etc/letsencrypt/live/;

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

Save and close.

Next, activate the Magento Nginx virtualhost and test the nginx configuration, and make sure there is no error.

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

Now restart the Nginx service using the following command.

systemctl restart nginx

As a result, the Nginx virtual host configuration for Magento 2 has been created and enabled.

Setup Nginx virtualhost for Magento

Step 8 – Magento Post-Installation

Open your web browser and type your Magento installation domain name on the address bar.

Now you will be redited to HTTPS secure connection and you will get the Magento welcome page and Terms of Service.

Install Magento 2

Click ‘Agree and Setup Magento‘ button to continue.

On the ‘Readiness check’ page, make sure all requirements are green and click the ‘Next‘ button.

System Requirement Readiness

Type in the details of the MariaDB database and the user that we created above and click ‘Next‘.

Setup Database

For the ‘Web configuration’, change all HTTP protocol to HTTPS, and make sure to change the ‘Magento Admin Address’.

Setup HTTPS Magento

Now clik the ‘Advanced Options’ and mark all HTTPS options, then unmark the ‘Apache Rewrites’ option and click the ‘Next‘ button again.

Next, on the Magento ‘Customize Your Store’ configuration, leave it as default and click ‘Next‘ button.

Magento Stor Cuztomization

Now type details admin user, email, and password. Then click the Next button.

Magento Setup user

Clcik the ‘Install Now’ button to install your Magento 2 store.

Install Magento

Now make all installation has no error.

Install Magento

Once all installation is finished, you will get ‘Success‘ page as below.

Magento Install is Finished

Click the ‘Launch Magento Admin‘ to log in to your Magento Admin Dashboard.

Magento Login Admin

Now type your Magento Admin user and password, then click the ‘Sign In’ and you wil get the Magento Admin Dashboard as below.

Magento Admin Dashoard

For the Magento default index page, open new tab and type your domain name on the address bar.

Below is the Magento default index page.

Magento Index

As a result, the installation of Magento 2 has been completed.

Next, back to your server shell and we will set up the Magento Cron and remove the permission to write of some Magento installation directory.

To set up the Magento cron, run the command below.

cd /var/www/magento2
sudo -u www-data php bin/magento cron:install --force

The Magento cron will run under the user ‘www-data’, check it using the following command.

crontab -u www-data -l

As a result, the Magento Cron has been added to the system.

Magento Setup Cron and Remove Write Permission

Next, we need to remove the write permission of the ‘/var/www/magento2/app/etc’ directory.

Run the chmod command below.

chmod u-w /var/www/magento2/app/etc

And finally, the installation of Magento 2 with Nginx web server, MariaDB server, PHP-FPM 7.3 on the latest Ubuntu 20.04 has been completed successfully.


About the Author

Leave a Reply