How to Install Magento 2.4.2 with LEMP on Ubuntu 20.04 – Magento is an open-source e-commerce platform written in PHP. It uses multiple other PHP frameworks such as Laminas and Symfony. Magento source code is distributed under Open Software License (OSL) v3.0. Magento was acquired by Adobe Inc in May 2018 for $1.68 Billion USD.

In this guide we are going to discuss how to install Magento 2.4 CE on Ubuntu 20.04LTS server with LEMP (Linux, Nginx, MySQL & PHP)

Prerequisites
Getting Started
Install Nginx
Install MySQL
Install PHP
Install Elasticsearch
Create a Database for Magento 2
Install Magento 2
Create an Nginx Configuration File
Install an SSL Certificate
Install Cron Job

Prerequisites

  • Ubuntu 20.04 LTS VPS with root access enabled.
  • 4GB of RAM or higher
  • Nginx
  • Percona Server 5.7 or higher
  • Elasticsearch 7.6.x
  • PHP 7.4 with bcmath, ctype, curl, dom, gd, hash, iconv, intl, libxml, mbstring, openssl, pdo_mysql, simplexml, soap, xsl and zip extensions enabled. Also, for performance reasons it is a good idea to install and enable the opcache extension.
  • A valid domain name for accessing the Magento 2 website.
  • Valid SSL certificate is required to enable HTTPS.

Time needed: 25 minutes.

How to Install Magento 2.4.2 with LEMP on Ubuntu 20.04

  1. Getting Started with Ubuntu Update

    First step, we need to logon to our server using root access. You can use Putty application as a SSH client. Visit Putty Website to download client
    Enter your IP address and port > click open to connect to the server.

    Next we need to update Ubuntu distribution, please run below commands.
    sudo apt-get update
    sudo apt-get upgrade

  2. Install Nginx

    Next we need to install Nginx webserver. Ubuntu 20.04 default Nginx version is 1.18 and you can install using below command.

    sudo apt-get install nginx

    If need to install latest version of Nginx you need to follow below instructions.

    Add Nginx signature to the Ubuntu

    sudo wget https://nginx.org/keys/nginx_signing.key
    sudo apt-key add nginx_signing.key

    After that, we have to edit the /etc/apt/sources/list and add the NGINX repositories.

    nano /etc/apt/sources.list

    At the bottom, add the following lines. The example below is for Ubuntu 20.04 LTS.

    deb [arch=amd64] https://nginx.org/packages/mainline/ubuntu focal nginx deb-src https://nginx.org/packages/mainline/ubuntu focal nginx

    Change release code name according to your distribution

    Ubuntu 20.10: groovy
    Ubuntu 20.04.1 LTS: focal
    Ubuntu 18.04.5 LTS: bionic
    Ubuntu 16.04.7 LTS: xenial
    Ubuntu 14.04.6 LTS: trusty
    Ubuntu 12.04.5 LTS: precise

    Once done please run Nginx install command

  3. Install MySQL

    As a next step, we are going to install MySQL server. I strongly recommend to install Percona Server as your DB server. You need to follow below steps to install percona server.

    Installing Percona Server for MySQL from Percona apt repository

    Install GnuPG, the GNU Privacy Guard:

    sudo apt-get install gnupg2

    Fetch the repository packages from Percona web:

    wget https://repo.percona.com/apt/percona-release_latest.$(lsb_release -sc)_all.deb

    Install the downloaded package with dpkg. To do that, run the following commands as root or with sudo:

    sudo dpkg -i percona-release_latest.$(lsb_release -sc)_all.deb

    Remember to update the local cache:

    sudo apt-get update

    After that you can install the server package:

    sudo apt-get install percona-server-server-5.7

  4. Install PHP

    Magento all versions based on PHP and we need to install required PHP modules to work with Nginx.

    Open the php.ini files in an editor:

    nano /etc/php/7.2/fpm/php.ini
    nano /etc/php/7.2/cli/php.ini

    Edit both files to match the following lines:

    memory_limit = 2G
    max_execution_time = 1800
    zlib.output_compression = On

    Save and exit the editor.

    Restart the php-fpm service:

    systemctl restart php7.2-fpm

    Additional PHP modules;

    apt install php7.4-pdo php7.4-mysqlnd php7.4-opcache php7.4-xml php7.4-gd php7.4-mysql php7.4-intl php7.4-mbstring php7.4-bcmath php7.4-json php7.4-iconv php7.4-soap php7.4-zip php7.4-curl

  5. Install Elasticsearch

    Elasticsearch is a must to setup Magento and next we need to install the elastic engine.

    curl -fsSL https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo apt-key add -

    Next, add the Elastic source list to the sources.list.d directory, where APT will search for new sources:

    echo "deb https://artifacts.elastic.co/packages/7.x/apt stable main" | sudo tee -a /etc/apt/sources.list.d/elastic-7.x.list

    Next, update your package lists so APT will read the new Elastic source:

    sudo apt update
    sudo apt install elasticsearch

    Use your preferred text editor to edit Elasticsearch’s configuration file. Here, we’ll use nano:

    sudo nano /etc/elasticsearch/elasticsearch.yml

    Change network host as below

    . . .
    ---------------------------------- Network -----------------------------------
    #
    Set the bind address to a specific IP (IPv4 or IPv6):
    #
    network.host: localhost
    . . .


    Start and enable Elastisearch

    sudo systemctl start elasticsearch
    sudo systemctl enable elasticsearch


    By now, Elasticsearch should be running on port 9200. You can test it with cURL and a GET request.

    curl -X GET 'http://localhost:9200'

    Output;

    {
    "name" : "localhost",
    "cluster_name" : "elasticsearch",
    "cluster_uuid" : "YGumSH9bSBWIBj7cIGOdwg",
    "version" : {
    "number" : "7.13.3",
    "build_flavor" : "default",
    "build_type" : "deb",
    "build_hash" : "5d21bea28db1e89ecc1f66311ebdec9dc3aa7d64",
    "build_date" : "2021-07-02T12:06:10.804015202Z",
    "build_snapshot" : false,
    "lucene_version" : "8.8.2",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
    },
    "tagline" : "You Know, for Search"
    }

  6. Create a Database for Magento 2

    Next we need to create database for Magento setup. First we need to logon to the Percona server

    mysql -u root -p

    Enter your password you entered during DB setup & create database & database user for Magento.

    CREATE DATABASE secureserver;
    CREATE USER 'db_user'@'localhost' IDENTIFIED BY 'your_password';
    GRANT ALL PRIVILEGES ON secureserver . * TO 'db_user'@'localhost';
    FLUSH PRIVILEGES;

  7. Install Magento 2.4.2

    We need to download Magento 2.4.2CE source code from Magento website. Visit Magento site to download your copy.

    Normally Ubuntu web root path is /var/www/html and I recommend you to create Magento site document root inside the default path.

    Example : /var/www/html/yourdomain.com/public_html

    Upload your downloaded Magento copy to above directory. You can WinSCP to upload files using SSH protocol.

    Once you uploaded you need to extract zip file to the place Magento setup files under the directory.

    For more info you can read: How to create tar.gz file in Linux using command line

    Next change your Magento file permission.

    find . -type f -exec chmod 664 {} \;
    find . -type d -exec chmod 775 {} \;
    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 {} +

    Set the ownership to Magento user and web user using

    sudo chown -R <Magento user>:<web server group> .

    Default Ubuntu webserver group is “www-data”

    For more info please refer , File systems access permissions

    Now we are ready to install Magento on your newly configured LEMP server. you can use below example code to setup Magento. Change required fields based on your environment.

    bin/magento setup:install \
    --base-url=http://localhost/magento2ee \
    --db-host=localhost \
    --db-name=magento \
    --db-user=magento \
    --db-password=magento \
    --backend-frontname=admin \
    --admin-firstname=admin \
    --admin-lastname=admin \
    --admin-email=admin@admin.com \
    --admin-user=admin \
    --admin-password=admin123 \
    --language=en_US \
    --currency=USD \
    --timezone=America/Chicago \
    --use-rewrites=1


    Once finish with the installation you can access your Magento front & back ends after next step.

  8. Create an Nginx Configuration File

    Next we need to configure Nginx virtual host to tell web serve to path and the directives which Magento needs to run.

    You need to create your config file inside the sites-available directory

    nano /etc/nginx/sites-available/your_domain.com.conf

    Paste below code to the config file and restart the Nginx server

    upstream fastcgi_backend {
    server unix:/run/php/php7.2-fpm.sock;
    }
    server { listen 80; server_name yourdomain.com;
    set $MAGE_ROOT /var/www/html/magento2;
    include /var/www/html/magento2/nginx.conf.sample; }


    Enable your configuration using below command;

    ln -s /etc/nginx/sites-available/your_domain.com.conf /etc/nginx/sites-enabled/

    service nginx restart

  9. Install an SSL Certificate

    Now we need secure your Magento installation with SSL/TLS support.

    We can use Let’s Encrypts cert authority & Let’s Encrypt is a Certificate Authority (CA) that provides an easy way to obtain and install free TLS/SSL certificates, thereby enabling encrypted HTTPS on web servers.

    Install Certbot and it’s Nginx plugin with apt:

    sudo apt install certbot python3-certbot-nginx

    Certbot provides a variety of ways to obtain SSL certificates through plugins. The Nginx plugin will take care of reconfiguring Nginx and reloading the config whenever necessary. To use this plugin, type the following:

    sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com

  10. Install Cron Job

    Several Magento features require at least one cron job, which schedules activities to occur in the future. A partial list of these activities follows:
    Catalog price rules
    Newsletters
    Generating Google sitemaps
    Customer Alerts/Notifications (product price change, product back in stock)
    Reindexing
    Private sales (Adobe Commerce only)
    Automatic updating of currency rates
    All Magento e-mails (including order confirmation and transactional)

    To setup Magento cron job you need to change to Magento file system owner & change to your Magento root directory (please read step 7 Magento file system permission two user section from Magento support document) using command below.

    su - magento_file_owner

    Setup cron job automatically using;

    bin/magento cron:install

Hooray, you are finish with the Magento 2.4 setup and don’t forgot comment your idea in the comment section.

Leave a comment

Your email address will not be published. Required fields are marked *