Magento is an open-source and well-known eCommerce platform written in PHP. Magento is a robust and powerful eCommerce solution used by more than 240.000 merchants worldwide. 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.

In this guide, we’ll walk you through the installation of Magento eCommerce on the Ubuntu 22.04 server.

Prerequisites

To start with this guide, make sure you have prepared the following:

  • An Ubuntu 24.04 with at least 4GB of memory.
  • A non-root user with sudo privileges.
  • A domain name pointed to a server IP address.

Installing Dependencies

Before you jump to the installation, you must ensure that Magento dependencies are installed. In this section, you will install software packages for Magento, which include OpenSearch, Nginx, MySQL server, PHP-FPM, Redis, Composer, and Git.

First, install the ‘apt-transport-https‘ and ‘gnupg‘ packages with the following:

sudo apt install apt-transport-https gnupg -y

Run the following command to add the OpenSearch GPG key and repository for Debian/Ubuntu-based distributions.

curl -o- https://artifacts.opensearch.org/publickeys/opensearch.pgp | 

sudo gpg --dearmor --batch --yes -o /usr/share/keyrings/opensearch-keyring
echo "deb [signed-by=/usr/share/keyrings/opensearch-keyring] https://artifacts.opensearch.org/releases/bundle/opensearch/2.x/apt stable main" | 

sudo tee /etc/apt/sources.list.d/opensearch-2.x.list

<img alt="add repo" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/05/echo/1-add-repo-opensearch.png66558cf852181.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="196" loading="lazy" src="data:image/svg xml,” width=”750″>

Now update your Ubuntu package index and install dependencies with the following apt command. With this, you will install the OpenSearch, Nginx, MySQL, PHP-FPM, Redis, Composer, and Git packages.

sudo apt update && sudo apt install opensearch nginx mysql-server composer git redis-server php-fpm php-bcmath php-common php-mbstring php-xmlrpc php-soap php-gd php-xml php-intl php-mysql php-cli php-redis php-ldap php-zip php-curl php-imagick

Input Y to confirm the installation.

<img alt="install deps" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/05/echo/2-install-deps.png66558cf86cf60.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="133" loading="lazy" src="data:image/svg xml,” width=”750″>

After the installation, check all the services to ensure it is running.

Check the Nginx service with the following. You will see the Nginx service is running and enabled.

sudo systemctl is-enabled nginx

sudo systemctl status nginx

<img alt="check nginx" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/05/echo/3-check-nginx.png66558cf894766.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="216" loading="lazy" src="data:image/svg xml,” width=”750″>

Now check the MySQL server with the command below. You can see in the output below that the MySQL server is running.

sudo systemctl is-enabled mysql

sudo systemctl status mysql

<img alt="check mysql" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/05/echo/6-check-mysql-server.png66558cf8c692f.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="224" loading="lazy" src="data:image/svg xml,” width=”750″>

Check the PHP-FPM service using the following command. The PHP-FPM should be running and enabled automatically.

sudo systemctl is-enabled php8.3-fpm

sudo systemctl status php8.3-fpm

<img alt="check php-fpm" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/05/echo/4-check-php-fpm.png66558cf8ef7c7.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="214" loading="lazy" src="data:image/svg xml,” width=”750″>

Check the Redis server using the command below. You should see that Redis is running and enabled.

sudo systemctl is-enabled redis-server

sudo systemctl status redis-server

<img alt="check redis" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/05/echo/5-check-redis.png66558cf943262.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="240" loading="lazy" src="data:image/svg xml,” width=”750″>

Lastly, check the Composer version with the following. You should have Composer v2.7 installed on your system.

sudo -u www-data composer -v

<img alt="check composer" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/05/echo/7-check-composer.png66558cf97385b.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="215" loading="lazy" src="data:image/svg xml,” width=”629″>

Setting up OpenSearch

After you have installed dependencies, you need to configure OpenSearch. Magento used OpenSearch for advanced search pri=ducts and indexing. In this section, you will increase the default max heap memory and disable OpenSearch’s security plugin (only for a development environment).

Open the file ‘/etc/opensearch/jvm.options‘ using the following nano editor.

sudo nano /etc/opensearch/jvm.options

Change the default memory allocation for OpenSearch as you need. In this case, we’ll be using 2GB for OpenSearch.

-Xms2g

-Xmx2g

Save the file and exit.

Now open the default OpenSearch configuration ‘/etc/opensearch/opensearch.yml‘ with the nano editor.

sudo nano /etc/opensearch/opensearch.yml

Add the following configuration to disable the security plugin in OpenSearch. For demonstration, you can disable this, but for production, must enable the security plugin and set up TLS certificates for your OpenSearch installation.

plugins.security.disabled: true

When done, save and exit the file.

Next, run the command below to reload the systemd manager. After you have installed OpenSearch, you must reload the systemd manager.

sudo systemctl daemon-reload

Now start, enable, and verify the Opensearch service with the following command:

sudo systemctl enable --now opensearch

sudo systemctl status opensearch

In the following output, you can see that OpenSearch is running and enabled.

<img alt="check opensearch servicer" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/05/echo/8-check-opensearch.png66558cf9b6dcf.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="269" loading="lazy" src="data:image/svg xml,” width=”750″>

Lastly, check your OpenSearch installation using the following ‘curl‘ command. This will show you basic information about your OpenSearch installation.

curl -X GET http://localhost:9200

Below you can see OpenSearch 2.14 is running on the Ubuntu server.

<img alt="test opensearch" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/05/echo/9-test-opensearch.png66558cf9e88dc.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="461" loading="lazy" src="data:image/svg xml,” width=”678″>

Setting up PHP-FPM

In this section, you will modify the default PHP-FPM configuration and enable the Opcache extension through the ‘php.ini’ file. You will increase the default memory_limit and adjust the date.timezone option.

Open the PHP-FPM configuration ‘/etc/php/8.3/fpm/php.ini’ with the following nano editor.

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

Change the default configuration with the following. Also, adjust the date.timezone, and memory_limit with your current server environment.

date.timezone = Europe/Amsterdam

When done, save the file and exit.

Now run the command below to restart the PHP-FPM service and apply your changes.

sudo systemctl restart php8.3-fpm

Setting up MySQL server

Now that you have configured PHP-FPM, in the next section you will set up the MySQL server installation and create a new database and user for Magento.

Secure your MySQL server installation by executing the command below.

sudo mysql_secure_installation

Below you you will be asked about the MySQL server configurations:

  • Input y to enable the VALIDATE PASSWORD component.
  • Input 1 to set up password level strength to MEDIUM.
  • Input y when asked to change your MySQL root password.
  • Input y to remove the default anonymous user.
  • Input y to disallow remote login for the MySQL root user.
  • Input y again to remove the default database test from MySQL.
  • Lastly, input y to reload MySQL tables privileges apply changes.

After you have configured MySQL, you need to create a new database and user for Magento.

Log in to the MySQL server with the following command. Type your MySQL root password when prompted.

sudo mysql -u root -p

Now run the following queries to create a new database and user for Magento. In this example, you will create a new user and database ‘magento‘ with the password ‘M4gentoPassw0rd__‘.

Make sure to change the following details with your information. Also, the MySQL password for Magento should have a special character, number, and Uppercase.

CREATE DATABASE magento;

CREATE USER 'magento'@'localhost' IDENTIFIED BY 'M4gentoPassw0rd__';

GRANT ALL PRIVILEGES ON magento.* to 'magento'@'localhost';

FLUSH PRIVILEGES;

<img alt="create database and user" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/05/echo/11-create-database.png66558cfa19ff9.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="255" loading="lazy" src="data:image/svg xml,” width=”750″>

Next, run the following query to verify privileges for user ‘magento‘. Make sure your user can access the database ‘magento‘.

SHOW GRANTS FOR magento@localhost;

Lastly, type quit to exit from the MySQL server.

<img alt="show mysql user priv" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/05/echo/12-show-privileges.png66558cfa33cf0.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="280" loading="lazy" src="data:image/svg xml,” width=”718″>

Installing Magento through the command line

In this section, you will download Magento source code and install PHP dependencies using Composer. Then, you will install Magento using the command line. In the last, you will generate a crontab for Magento.

Go to the ‘/var/www’ directory and download the Magento source code using the following wget command. Check the Magento GitHub page and grab the Magento latest version URL.

cd /var/www

wget https://github.com/magento/magento2/archive/refs/tags/2.4.7.tar.gz

After the download is complete, extract the Magento source code and rename the extracted directory to ‘magento2’. With this, your Magento installation should be located at ‘/var/www/magento2’.

tar -xf 2.4.7.tar.gz

mv magento2-* magento2

Now run the command below to create additional directories for Composer and 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

Then, run the following command to give the ‘www-data‘ user permission to read, write, and execute within the ‘/var/www/magento2‘ directory.

sudo chmod u rwx /var/www/magento2

Next, go to the ‘/var/www/magento2‘ directory and install PHP dependencies for Magento with the Composer command below. This will ensure your dependencies for Magento is installed

cd /var/www/magento2

sudo -u www-data composer install

After installing dependencies, run the command below to allow ‘www-data‘ user permission of ‘rwx‘ to both ‘generated‘ and ‘var‘ directories within the Magento web root.

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

sudo chmod u rwx /var/www/magento2/var /var/www/magento2/generated

sudo chmod g rw /var/www/magento2/var /var/www/magento2/generated

Now run the following command to start the Magento installation. Make sure to change the URL details, MySQL database, administrator user, and password for your Magento installation.

sudo -u www-data bin/magento setup:install 

--base-url=http://hwdomain.io

--db-host=localhost

--db-name=magento2

--db-user=magento

--db-password=M4gentoPassw0rd__

--admin-firstname=bob

--admin-lastname=admin

[email protected]

--admin-user=bob

--admin-password=BobAdm1nP4ssword

--currency=USD

--timezone=Europe/Amsterdam

--use-rewrites=1

--session-save=redis

--search-engine=opensearch

--opensearch-host=127.0.0.1

--opensearch-port=9200

--opensearch-enable-auth=0

--opensearch-index-prefix=magento22

--opensearch-timeout=30

Below you can see the Magento installation begin:

<img alt="install magento" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/05/echo/13-install-magento-cli.png66558cfa74aad.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="637" loading="lazy" src="data:image/svg xml,” width=”750″>

Once complete, you will see the output ‘Magento installation complete.’ and the admin URL path for your Magento installation. Make sure to save the generated URL for the Magento admin page.

<img alt="installation complete" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/05/echo/14-install-magento-success.png66558cfaab8a3.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="319" loading="lazy" src="data:image/svg xml,” width=”750″>

To finish the Magento installation, run the following command to generate crontab for Magento and run indexing for Magento.

sudo -u www-data bin/magento cron:install

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

You can check the list crontab for user ‘www-data’ with the following:

crontab -u www-data -l

You can see below the crontab for Magento is generated.

<img alt="setup cron" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/05/echo/19-magento-cron.png66558cfad4b9d.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="112" loading="lazy" src="data:image/svg xml,” width=”750″>

Lastly, run the command below to clear the cache of your Magento installation. With this, you’ve completed the Magento installation, next you need to set up the Nginx web server.

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

Setting up Nginx server block

At this point, your Magento installation is finished, now you will create a new Nginx server to run your Magento installation. Ensure you have a domain name pointed to your server IP address.

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

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

Insert the following configuration and make sure to change the server_name option with your Magento domain name.

upstream fastcgi_backend {

server unix:/run/php/php8.3-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;

}

When finished, save and exit the file.

Now run the command below to activate the server block ‘magento.conf‘ and verify your Nginx syntax. Ensure you get the output ‘syntax is ok – test is successful‘.

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

sudo nginx -t

Lastly, restart the Nginx service with the following to apply your new server block configuration. After the command is executed, your Magento installation will be available.

sudo systemctl restart nginx

<img alt="setup nginx" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/05/echo/15-test-nginx.png66558cfaefd2d.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="134" loading="lazy" src="data:image/svg xml,” width=”750″>

Securing Magento with HTTPS

If you’re running Magento in the public domain, you must secure it with HTTPS. In this section, you will secure your Magento installation with HTTPS through Certbot and Letsencrypt.

Install the Certbot and Certbot Nginx plugin using the following command. Enter Y to confirm the installation.

sudo apt install certbot python3-certbot-nginx

After the installation is complete, run the command below to generate SSL/TLS certificates from Letsencrypt. Make sure to change the email address and domain name below with your information.

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

Your Magento installation should be automatically secured with HTTPS when the process is finished. Your SSL/TLS certificates are in the ‘/etc/letsencrypt/live/domain.com‘ directory.

Accessing Magento

Open your web browser and visit your Magento domain name such as https://hwdomain.io. if your installation is successful, you will see the default Magento index page like the following.

<img alt="magneto home" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/05/echo/16-magento-home.png66558cfb1ec67.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="400" loading="lazy" src="data:image/svg xml,” width=”750″>

Now, visit the Magento administration URL, which is https://hwdomain.io/admin_ze87mmj. See the installation process.

Enter your admin user and password for Magento and press Login.

<img alt="login magento" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/05/echo/17-login-magento.png66558cfb40bb5.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="719" loading="lazy" src="data:image/svg xml,” width=”648″>

If successful, you should get the Magento administration dashboard like the following:

<img alt="magento dashboard" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/05/echo/18-magento-dashboard.png66558cfb6a2d3.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="363" loading="lazy" src="data:image/svg xml,” width=”750″>

Conclusion

Congratulations! You have completed the installation of Magento eCommerce on the Ubuntu 24.04 server. You have Magento installed with the LEMP Stack (Linux, Nginx, MySQL, and PHP-FPM), OpenSearch, and Redis. You have also secured Magento with HTTPS through Certbot and Letsencrypt.