How to Install Magento eCommerce Software on Ubuntu 22.04

Magento is an open-source and well know eCommerce platform written in PHP. Magento is a robust and powerful eCommerce solution that use by more than 240.000 merchants worldwide. Initially, Magento is created as a fork of osCommerce in 2007, and in May 2018 Magento is acquired by Adobe Inc and become Adobe eCommerce.

Magento is a versatile and scalable platform for building an eCommerce store. It’s suitable for building small, medium, and big online stores. Magento allows you to create and host your online stores on your server. You can set up online stores without any programming knowledge, it enables your business and take your business to next level.

In this tutorial, you will install Magento eCommerce on Ubuntu 22.04. You will install and configure Magento dependencies such as Elasticsearch, Redis, PHP-FPM with Nginx web server, MySQL Server, and Composer. You’ll also secure the Magento eCommerce installation with SSL certificates from Letsencrypt.

Prerequisites

To complete and following with this tutorial, you will need some prerequisites listed below:

  • An Ubuntu 22.04 server – This example uses an Ubuntu server with hostname ‘magento-server‘ and IP address ‘192.168.5.100‘.
  • A non-root user with sudo/root administrator privileges.
  • A domain name pointed to the server IP address – This example uses the domain name ‘hwdomain.io’ for the Magento eCommerce installation.

Also, if you plan to install Magento on your production, ensure that you have a server with high resources of CPUs, memory, and disk. This testing of Magento eCommerce installation used 6GB of memory.

Prepare System

Before you start the Magento installation, you’ll now prepare your Ubuntu system by updating repositories, upgrading packages, then installing some basic packages.

Run the below apt command to update and refresh Ubuntu repositories. Then, upgrade packages to the latest version.

sudo apt update
sudo apt upgrade

Next, install some basic dependencies via the apt command below.

sudo apt install gnupg2 apt-transport-https curl wget

When prompted, input y to confirm and press ENTER to proceed.

install basic dependnecies

With basic dependencies installed, you’ll then start the installation of package dependencies for Magento eCommerce.

Installing and Configuring Elasticsearch 7.x

The first dependency that you’ll install is Elasticsearch. Modern Magento eCommerce required a search engine to provide effective real-time search results for the customers. Magento supports the search engine Elasticsearch and OpenSearch.

In this step, you’ll install set up and install Elasticsearch 7.x on an Ubuntu 22.04 server. The latest version of Magento required specific Elasticsearch version 7.x.

To start, run the below command to add the GPG key and the Elasticsearch repository to your system

wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch \
| sudo gpg --dearmor -o /usr/share/keyrings/elasticsearch-keyring.gpg

echo “deb [signed-by=/usr/share/keyrings/elasticsearch-keyring.gpg] https://artifacts.elastic.co/packages/7.x/apt stable main” \
| sudo tee /etc/apt/sources.list.d/elastic-7.x.list

After the repository is added, run the below apt command to update and refresh your Ubuntu package index.

sudo apt update

You’ll see the Elasticsearch repository is added to your Ubuntu system.

add elasticsearch repo

Next, run the below apt command to install Elasticsearch on your system. The jq package can be used to parse json output format.

sudo apt install elasticsearch jq

Input y when prompted and press ENTER to proceed.

install elasticsearch

After the Elasticsearch is installed, create a new config file ‘/etc/elasticsearch/jvm.options.d/memory.options’ using the below nano editor command.

sudo nano /etc/elasticsearch/jvm.options.d/memory.options

Add the following lines to the file. With these lines, you’ll specify the memory usage for Elasticsearch. This example will use 1GB of memory for Elasticsearch, be sure to change the max memory depending on your system memory.

-Xms1g
-Xmx1g

Save the file and exit the editor when finished.

Now run the below systemctl command utility to start and enable the Elasticsearch service.

sudo systemctl start elasticsearch
sudo systemctl enable elasticsearch

start enable elasticsearch

Verify the Elasticsearch service using the below systemctl command utility.

sudo systemctl is-enabled elasticsearch
sudo systemctl status elasticsearch

You’ll receive the Elasticsearch service is enabled and will be run automatically upon the bootup. And the current status of the Elasticsearch service is running.

verify elasticsearch

Lastly, run the below curl command to verify the Elasticsearch and ensure that it’s working. The default Elasticsearch installation is running on localhost with port 9200.

curl http://127.0.0.1:9200/ | jq .

You’ll receive an output similar to this on your terminal – The installed version of Elasticsearch is v7.17.8, which is based on Lucene 8.11.1.

verify elasticsearch via curl

With Elasticsearch is installed and configured, you’ll next install and configure the PHP-FPM and Nginx web server.

Installing Nginx and PHP-FPM

At the time of this writing, Magento eCommerce required PHP 8.1 for its installation. In this step, you’ll install and configure the PHP-FPM 8.1 on your Ubuntu system. And at the same time, you’ll also install the Nginx web server.

You’ll also set up the max memory allocation for PHP that will be used to run Magento, and enable the OPcache extension.

Run the below apt command to install PHP-FPM 8.1 and Nginx web server.

sudo apt install nginx unzip php8.1-fpm php8.1-bcmath php8.1-common php8.1-mbstring php8.1-xmlrpc php8.1-soap php8.1-gd php8.1-xml php8.1-intl php8.1-mysql php8.1-cli php8.1-ldap php8.1-zip php8.1-curl php-imagick

Input y when prompted for confirmation. Then press ENTER to proceed.

install nginx and php-fpm

After PHP-FPM is installed, open the config file ‘/etc/php/8.1/fpm/php.ini‘ using the below nano editor command.

sudo nano /etc/php/8.1/fpm/php.ini

Change the default php.ini configuration with the following lines. Be sure to adjust the value of the option ‘date.timezone‘ and ‘memory_limit‘, which is depending on your system environment.

In this example, you’ll allocate 1GB of memory for the PHP-FPM service. You’ll also enable the OPcache extension, which is required for Magento.

date.timezone = Europe/Stockholm
memory_limit=1G

realpath_cache_size=10M
realpath_cache_ttl=7200

opcache.enable=1
opcache.max_accelerated_files=3000
opcache_revalidate_freq = 100
opcache.memory_consumption=512
opcache.save_comments=1

Save the file and exit the editor when you’re done.

Next, run the below systemctl command utility to restart the PHP-FPM and Nginx services. This will apply the changes to both services.

sudo systemctl restart php8.1-fpm
sudo systemctl restart nginx

Now verify the PHP-FPM service via the following command.

sudo systemctl is-enabled php8.1-fpm
sudo systemctl status php8.1-fpm

You should receive the output that the PHP-FPM service is enabled and will be run automatically upon the bootup. And the current status of the PHP-FPM service is running.

verify php-fdpm

For the Nginx service, you can verify using the below command.

sudo systemctl is-enabled nginx
sudo systemctl status nginx

Output – The Nginx service is enabled and will be run automatically upon the bootup. And the current status of the Nginx service is running.

verify nginx

Lastly, run the below command to verify the PHP version that is installed on your system. Then verify the Opcache extension to ensure that it’s enabled.

php -v
php -i | grep opcache

You’ll receive the output like this on your terminal – The PHP 8.1 is installed on your system and the OPcache extension is enabled.

verify php version

verify opcache

Now that you’ve installed and configured the PHP-FPM and Nginx web server for the Magento eCommerce. In the next steps, you’ll install and set up the MySQL Server.

Installing and Configuring MySQL Server 8

By default, Magento supports both MySQL and MariaDB as the database backend. At the time of this writing, the Magento eCommerce required MySQL v8 or MariaDB at last v10.4. And for this guide, you’ll be using the MySQL Server for your Magento deployment.

You’ll now install MySQL Server 8 on your Ubuntu system. Then, you’ll set up the root password for the MySQL server, secure the MySQL via ‘mysql_secure_installation‘, then you’ll create a new MySQL database and user that Magento will use.

The default Ubuntu 22.04 repository provides the MySQL Server v8. Run the below apt command to install the MySQL Server packages.

sudo apt install mysql-server

Input y when prompted and press ENTER to proceed.

install mysql server

After the MySQL Server is installed, run the below systemctl command utility to verify the MySQL service and ensure that the service is running.

sudo systemctl is-enabled mysql
sudo systemctl status mysql

You’ll see the output like this – The MySQL Server is enabled and will be run upon the bootup automatically. And the status of MySQL Server is running.

verify mysql server

Next, run the below command to access the MySQL shell.

sudo mysql

Run the below query to change and set up the MySQL ‘root’ password. Be sure to change the password on the following query.

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password by 'r00tP@ssw0rd-*-';
quit

setup mysql root password

Now that the MySQL Server ‘root‘ password is configured, you’ll then secure the MySQL server via the utility ‘mysql_secure_installation‘.

Run the below command to start securing the MySQL deployment.

sudo mysql_secure_installation

You’ll now be asked about the following configuration.

  • Enable the VALIDATE PASSWORD component. Input y to confirm.
  • Choose the password level strength. Input 1 for MEDIUM.
  • Change the root password? Input n for no.
  • Remove the default anonymous user. Input Y.
  • Disallow remote login for the MySQL root user. Input Y.
  • Remove the default database test? Input Y.
  • Reload tables privileges to apply changes? Input Y

Now the MySQL Server is secured and the root password is configured. Next, you’ll create a new MySQL database and user that Magento will use.

Log in to the MySQL shell via the command below. When prompted for the password, input the MySQL root password.

sudo mysql -u root -p

After logging in, run the following queries to create a new MySQL database and user. In this example, you’ll create the MySQL database and user ‘magento‘. And be sure to change the default password in the below query.

CREATE DATABASE magento;
CREATE USER 'magento'@'localhost' IDENTIFIED BY 'M@gentoP4ssw0rd__';
GRANT ALL PRIVILEGES ON magento.* to 'magento'@'localhost';
FLUSH PRIVILEGES;

create database and user

Now run the below MySQL query to verify the privileges for the MySQL user ‘magento@localhost‘. Then, type quit exiting from the MySQL shell.

SHOW GRANTS FOR magento@localhost;
quit

You’ll receive the output similar to this – The MySQL user ‘magento@localhost’ has privileges to access the ‘magento‘ database.

verify user

In the next steps, you’ll install Redis which will be used by Magento for session management.

Installing Redis Server

Magento supports multiple ways for storing sessions, you can save sessions with the built-in PHP-FPM, using the MySQL Server, or using the Redis Server. For the Magento session management, it’s recommended to use Redis Server, which is built for storing key-value temporary databases and sessions for your application.

At the time of this writing, the Magento eCommerce required Redis v6, which is available by default on Ubuntu 22.04 repository.

Run the below apt command to install Redis on your system. Input y when prompted and press ENTER to proceed.

sudo apt install redis-server

install redis server

After Redis Server is installed, run the below systemctl command utility to verify the Redis Server and ensure that the service is enabled and running.

sudo systemctl is-enabled redis-server
sudo systemctl status redis-server

You’ll then receive the output similar to this – The Redis Service is enabled and will be run automatically upon the bootup. And the status of Redis Server is running, which is by default running on localhost with port 6379.

verify redis service

With the Redis Server is installed, you’ll next install the Composer for the PHP dependencies management.

Installing Composer PHP Dependencies Management

In this step, you’ll install the Composer that will be used to install and manage PHP dependencies for Magento eCommerce. On the default Ubuntu repository, the Composer package v2.2 is available, which is suitable for the Magento latest version.

Run the below apt command to install the Composer to your system.

sudo apt install composer

Input y when prompted and press ENTER to proceed. The Composer installation should begin.

install composer

After the Composer is installed, run the below command to verify the Composer version.

sudo -u www-data composer -v

You’ll receive the output similar to this – The Composer v2.2 is installed and you’re ready to start the Magento installation.

verify composer

Installing Magento on Ubuntu Server

Magento can be installed in many ways, you can install Magento via Git, via the Metapacakge, or by downloading the source code manually via GitHub, especially for the Magento Open Source edition.

In this step, you’ll download the Magento source code manually from the GitHub of Magento release page, then install PHP dependencies via Composer, and lastly install and configure the Magento via the ‘magento’ command line.

Go to the GitHub of the Magento release page and grab the link to the Magento version that you want to install. In this example, you’ll install Magento 2.4.5.

Move the working directory to the ‘/var/www’ directory and download the Magento source code via wget.

cd /var/www
wget https://github.com/magento/magento2/archive/refs/tags/2.4.5.tar.gz

After downloading the Magento source code, extract it and rename the extracted directory to ‘magento2‘. Now your Magento installation directory should become ‘/var/www/magento2‘.

tar -xf 2.4.5.tar.gz
mv magento2-* magento2

Next, run the below command to create new directories that will be used for storing Composer configuration and cache. Then, change the ownership of the ‘/var/www‘ directory to user ‘www-data‘.

sudo mkdir -p /var/www/{.config,.cache}
sudo chown -R www-data:www-data /var/www

Run the below command to ensure that the owner of the ‘/var/www/magento2‘ directory can read, write, and execute files within that directory.

sudo chmod u+rwx /var/www/magento2

download and setup magento

After that, move to the Magento installation directory ‘/var/www/magento2‘. Then install PHP dependencies for Magento via the ‘composer‘ command.

cd /var/www/magento2
sudo -u www-data composer install

Output during the installation of PHP dependencies for Magento.

installing dependneices

dpendneices installed

After installing Magento PHP dependencies, run the below command to make the ‘/var/www/magento2/bin/magento’ binary file executable.

sudo chmod u+x /var/www/magento2/bin/magento

Now within the ‘/var/www/magento2’ directory, run the below command to start the Magento installation. This will execute the ‘magento‘ command line via the www-data user.

Also, be sure to change the domain name, default Magento admin user and password, the MySQL database details, default timezone, and currency that you will be using.

sudo -u www-data bin/magento setup:install \
--base-url=http://hwdomain.io --use-secure=1 \
--base-url-secure=https://hwdomain.io --use-secure-admin=1 \
--db-host=localhost --db-name=magento --db-user=magento --db-password=M@gentoP4ssw0rd__ \
--admin-firstname=admin --admin-lastname=Wonderland [email protected] --admin-user=admin --admin-password=Adm1n_p4ssw0rd \
--language=en_US --currency=USD --timezone=Europe/Stockholm --use-rewrites=1 \
--session-save=redis --elasticsearch-host=http://127.0.0.1 --elasticsearch-port=9200 --elasticsearch-enable-auth=0

Output during the Magento installation.

install magento via cli

Then after the Magento installation is finished, you’ll receive the output like this – At the bottom of the message, you can see the generated Magento admin URL and the suggestion to remove the write access to the directory ‘/var/www/magento2/app/etc‘ directory.

magento installation finished

Run the below chmod command to disable write access to the directory ‘/var/www/magento2/app/etc’.

sudo chmod ug-w /var/www/magento2/app/etc

At this point, the Magento eCommerce is installed, but you still need to set up the Nginx server block that will be used to run Magento. This you’ll do in the next steps, including how to secure Magento with SSL from Letsencrypt.

Setting up Nginx Server Block

In this step, you’ll set up and create a new Nginx server block configuration that will be used to run the Magento eCommerce. Also, Magento provides a complete Nginx configuration that is available in the ‘/var/www/magento2/nginx.conf.sample‘ file.

Create a new Nginx server block configuration ‘/etc/nginx/sites-available/magento.conf’ using the below nano editor command.

sudo nano /etc/nginx/sites-available/magento.conf

Add the following lines to the file. Be sure to change the domain name with your domain.

upstream fastcgi_backend {
   server unix:/var/run/php/php8.1-fpm.sock;
}
server {
listen 80;
listen [::]:80;
server_name hwdomain.io;
set $MAGE_ROOT /var/www/magento2/;
include /var/www/magento2/nginx.conf.sample;
client_max_body_size 2M;

access_log /var/log/nginx/magento.access;
error_log /var/log/nginx/magento.error;
}

Save the file and exit the editor when finished.

Next, run the below command to activate the server block configuration ‘/etc/nginx/sites-evailable/magento.conf‘. Then, verify Nginx configurations to ensure that you have the proper configuration.

sudo ln -s /etc/nginx/sites-available/magento.conf /etc/nginx/sites-enabled/
sudo nginx -t

You’ll then receive the output ‘test successful – syntax ok‘, which means that you’ve proper and correct Nginx configuration.

Lastly, run the below systemctl command utility to restart the Nginx service and apply the changes.

sudo systemctl restart nginx

setup nginx

Now that Magento is running and the Nginx server block is configured. Your Magento installation is now accessible, but you still need to set up HTTPS via Letsencrypt to secure your Magento installation.

Securing Magento with SSL Letsencrypt

In this step, you’ll install the certbot tool with the Nginx plugin to your system. Then, you’ll generate SSL certificates for your Magento domain name installation. Also, ensure that you have the domain name pointed to your server IP address an email address that will be used to register to Letsencrypt.

Run the below apt command to install certbot and python3-certbot-nginx packages.

sudo apt install certbot python3-certbot-nginx

Input Y when prompted and press ENTER to proceed.

install certbot

Next, run the below certbot command to generate SSL certificates via Letsencrypt. Be sure to change the domain name with your Magento installation domain name, and the email address with your email.

sudo certbot --nginx --agree-tos --no-eff-email --redirect --hsts --staple-ocsp --email [email protected] -d hwdomain.io

After the certbot process is finished, your Magento is now accessible via a secure HTTPS connection.

Accessing Magento eCommerce

Open your web browser and visit the domain name of your Magento installation (i.e: https://hwdomain.io/).

If your Magento installation is successful, you will see the Magento default homepage like the following screenshot.

magento homepage

Now input the generated admin path URL and you should get the Magento login page. Log in with your username and password, then click Sign In.

magento admin login

You should now get the Magento administration dashboard.

magneto index

You’ve finished the Magento eCommerce installation with the Nginx web server, MySQL Server, PHP-FPM 8.1, Redis Server, and Elasticsearch 7.x. Also, you’ve secured the Magento deployment with SSL certificates from Letsencrypt.

Magento eCommerce Post Installation

In this step, you’ll set up the cron for Magento eCommerce via the ‘magento‘ command line. Then, you’ll also delete and clear Magento cache after the first installation.

Move the working directory to the ‘/var/www/magento2‘.

cd /var/www/magento2

Run the below command to set up and install cron for Magento eCommerce. Then, run the cron immediately. This will create a new cron for the user www-data.

sudo -u www-data bin/magento cron:install
sudo -u www-data bin/magento cron:run --group index

Run the below command to verify the list of cron jobs for the user www-data. You should see that the Magento cron is added.

crontab -u www-data -l

Below is the output of the cron generated by Magento.

setup and verify cron magento

Lastly, run the below command to clean and flush the cache on your Magento eCommerce.

sudo -u www-data bin/magento cache:clean

You’ll receive an output similar to the following screenshot.

magento flush cache

With this, you have fully completed the Magento eCommerce installation on an Ubuntu 22.04 Server.

Conclusiion

In this tutorial, you set up your Magento eCommerce store on an Ubuntu 22.04 server. This included setting up Elasticsearch as the search engine for Magento, MySQL Server as the database server, and the PHP-FPM and Nginx web server. Finally, you have secured your Magento eCommerce with SSL/TLS via Certbot and Letsencrypt.

In the last step, you’ve finished the Magento eCommerce installation by logging in to the Magento administration dashboard to ensure the installation is successful. Then, you’ve also configured cron for Magento that will be running in the background, and also cleanup Magento cache via the ‘magento’ command line.

To get better performance for your Magento eCommerce, you may use multiple servers for your Magento deployments. Each component of Magento can be installed on a different server. Also, you can add other components such as Varnish that can be used to store cache static files of Magento, add RabbitMQ as the message broker, or add the SMTP servers for email notifications.