How to Install Magento on Debian with Nginx

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 Magento development was started in 2008 by Varien.

This tutorial will show you how to install Magento 2.3.4 on the Debian Buster 10. We will install Magento2 using the Nginx web server, PHP-FPM 7, and MariaDB Server on the latest Debian Buster 10.

Prerequisites

For this guide, we will install Magento on Debian 10 with 2GB of RAM, 25 free disk space, and 2 CPUs.

What we will do?

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

Step 1 – Install Nginx Webserver

First, we will install the Nginx web server in the Debian system.

Update the Debian repositories and install Nginx using the apt command below.

sudo apt update
sudo apt install nginx -y

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

systemctl start nginx
systemctl enable nginx

The Nginx web server is up and running on the Debian system. Check it using the command below.

systemctl status nginx

Below is the result you will get.

Step 2 – Install MariaDB Server

In this step, we will install MariaDB on the Debian server. After that, we will set up the MariaDB password for the root user, and create a new database and user for Magento installation.

Install MariaDB Server using the apt command below.

sudo apt install mariadb-server mariadb-client

After that, start the MariaDB server and add it to the system boot.

systemctl start mariadb
systemctl enable mariadb

The MariaDB server is up and running in the Debian system.

Next, we will set up the password for the root user. Run the ‘mysql_secure_installation’ command below.

mysql_secure_installation

Type a new password for the MariaDB root user and type ‘Y’ for all configurations that will be asked.

aaa

The MariaDB root password has been created.

Now log in to the MariaDB shell using the root user as below.

mysql -u root -p

Create a new database for the Magento installation. We will create a new database named ‘magentodb’ with the user ‘magentouser’ and the password ‘hakase321@#’.

Run the MariaDB queries below.

create database magentodb;
create user magentouser@localhost identified by 'hakase321@#';
grant all privileges on magentodb.* to magentouser@localhost identified by 'hakase321@#';
flush privileges;

Type ‘exit’ to log out from the MariaDB shell.

As a result, the MariaDB installation on the Debian server has been completed. And you’ve successfully set up the password for the user root and created the database and user for Magento.

Step 3 – Install and Configure PHP-FPM

This step will install and configure PHP-FPM on the Debian server. We will install PHP-FPM 7.3 with additional packages needed by Magento.

Install PHP-FPM using the apt command below.

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

Once the installation is completed, go to the ‘/etc/php/7.3/’ directory.

cd /etc/php/7.3/

Edit the ‘php.ini’ configuration for both PHP-FPM and php-cli.

vim fpm/php.ini
vim cli/php.ini

Change the configuration as below.

memory_limit = 512M
max_execution_time = 180
zlib.output_compression = On
cgi.fix_pathinfo=0

Save and close.

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

systemctl restart php7.3-fpm
systemctl enable php7.3-fpm

As a result, the PHP-FPM service is up and running. By default, it’s running under the system sock file.

Check the PHP-FPM service using the command below.

netstat -pl | grep php
systemctl status php7.3-fpm

Below is the result you will get.

Step 4 – Install Composer

A composer is a command-line tool for managing the dependency in PHP. It’s an application-level package manager for PHP programming language, allows you to manage and integrate external PHP packages to your project.

In this step, we will install the PHP Composer from the official Debian repository. The Composer will be used by Magento for installing its packages dependencies.

Install Composer using the apt command below.

sudo apt install composer -y

Once the installation is complete, check the Composer version using the following command.

composer --version

You will get the response below.

Composer 1.8.4 2019-02-11 10:52:10

As a result, the PHP Composer has been installed to the Debian Server.

Step 5 – Generate SSL Letsencrypt

In this step, we will generate the SSL Letsencrypt that will be used for securing the Magento installation. We will generate the SSL Letsencrypt using the ‘certbot’ tool.

Install the certbot tool using the apt command below.

sudo apt install certbot -y

Befor generating the SSL Letsencrypt, stop the Nginx service.

systemctl stop nginx

Now you can generate the SSL Letsencrypt for your domain name using the certbot command below.

certbot certonly --standalone -d magento.your-domain.com

Type your email address for registering, type ‘Y’ to aggress the Letsencrypt TOS (Term of Services), and type ‘N’ to not sharing an email address with EFF.

Once the certbot process is completed, your certificates will be located at the ‘/etc/letsencrypt/live/magento.your-domain.com/’ directory.

Step 6 – Download Magento 2

In this step, we will download Magento from the GitHub repository. We will download the Magento 2.3.4 source code, download the additional PHP dependencies with the PHP Composer, and fix the permission and ownership of the Magento source code.

Go to the ‘/var/www/’ directory and download Magento 2.3.4 source code using the following commands.

cd /var/www/
wget https://github.com/magento/magento2/archive/2.3.4.tar.gz

Extract the Magento source code and rename the extracted directory to ‘magento2’.

tar -xf 2.3.4.tar.gz
mv magento2.3.4/ magento2/

Next, go to the ‘magento2’ directory and install additional PHP dependencies using the composer command as below.

cd /var/www/magento2/
composer install -v

Once all installation is completed, fix permissions and ownership of the Magento installation directory ‘/var/www/magento2’ by running the command below.

find var generated vendor pub/static pub/media app/etc -type f -exec chmod g+w {} +
find var generated vendor pub/static pub/media app/etc -type d -exec chmod g+ws {} +
chown -R www-data:www-data .
chmod u+x bin/magento

As a result, you’ve downloaded the Magento source code and install PHP dependencies needed for Magento. The Magento installation directory is located at the ‘/var/www/magento2’ directory.

Step 7 – Set up Nginx Virtualhost for Magento2

In this step, we will add a new Nginx virtual host configuration for Magento.

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

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

Change the domain name and path of SSL certificates with your own, then paste the configuration into it.

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;
server_name magento.hakase-labs.io;

ssl on;
ssl_certificate /etc/ssl/magento/fullchain.pem;
ssl_certificate_key /etc/ssl/magento/privkey.pem;

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

Save and close.

Next, activate the Magento virtualhost, 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 command below.

systemctl restart nginx

As a result, the Nginx virtual host configuration for Magento has been activated. It’s now accessible from your web browser.

Step 8 – Magento Post-Installation

Now open your web browser and type the Magento installation URL on the address bar.

https://magento.your-domain.com/

Now you will get the Magento Terms and Agreement and click the ‘Agree and Setup Magento‘ button to continue.

Now you will get the Magento ‘Readiness Check‘ page. Make sure all Magento requirements are checked as green, then click the ‘Next‘ button.

Type your MariaDB database details for the Magento and click ‘Next‘ again.

For the ‘Web Configuration‘, change the Magento store URL with secure HTTPS connection and change the Magento admin page URL as you want.

Click the ‘Advanced Options‘ section, check all ‘HTTPS Options‘ and uncheck the ‘Apache Rewrites’, then click ‘Next‘ button to continue.

Now you will get the ‘Store Customization‘ page, change details timezone, currency, and language with your own, then click the ‘Next‘ again.

Create the Magento admin user and type details about your user, then click the ‘Next‘ button.

Now click the ‘Install Now’ button to start the Magento installation.

And the Magento installation will begin.

After the installation is finished, you will get the summary ‘Success‘ page as below.

Click the ‘Launch Admin‘ button to Log in to the Magento Admin page.

Type your admin user and password, then click the ‘Sign In‘ button.

Now you will get the Magento Admin page as below.

Below is the default index page of the Magento Store.

As a result, the installation of Magento 2.3.4 on the Debian Buster 10 with the Nginx web server, MariaDB database, and PHP-FPM has been completed successfully.