How to install the magento 2.4 on Ubuntu 20.04

Step 1: Install Nginx HTTP Server

To install Nginx on Ubuntu, run the commands below:

sudo apt update
sudo apt install nginx

After installing Nginx, the commands below can be used to stop, start and enable Nginx service to always start up with the server boots.
sudo systemctl stop nginx.service
sudo systemctl start nginx.service
sudo systemctl enable nginx.service

To test whether Nginx is installed and functioning, open your web browser and browse to the server’s IP address or hostname.
http://localhost

If you see the above page in your browser, then Nginx is working as expected.

Step 2: Install MariaDB Database Server

To install MariaDB, run the commands below:

sudo apt-get install mariadb-server mariadb-client

After installing MariaDB, the commands below can be used to stop, start and enable MariaDB service to always start up when the server boots.

sudo systemctl stop mariadb.service
sudo systemctl start mariadb.service
sudo systemctl enable mariadb.service

Next, run the commands below to secure the database server with a root password if you were not prompted to do so during the installation.

 

sudo mysql_secure_installation

When prompted, answer the questions below by following the guide.

Enter current password for root (enter for none): Just press the Enter
Set root password? [Y/n]: Y
New password: Enter password
Re-enter new password: Repeat password
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
To verify and validate that MariaDB is installed and working, login to the database console using the commands below:

sudo mysql -u root -p

type the root password when prompted.

If you see a similar screen as shown above, then the server was successfully installed.

Step 3: Install PHP 7.4 and Related Modules

Magento is a PHP based application, and PHP is required to run it. Since some versions of Ubuntu don’t have the latest version of PHP, you can add a third-party PPA repository to install PHP from there.

The command below will add a third-party PPA to Ubuntu.

 

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

Then update and upgrade to PHP 7.4

sudo apt update

Next, run the commands below to install PHP 7.4 and related modules.

sudo apt install php7.4-fpm php7.4-common php7.4-mysql php7.4-gmp php7.4-curl php7.4-intl php7.4-mbstring php7.4-xmlrpc php7.4-gd php7.4-xml php7.4-cli php7.4-bcmath php7.4-soap php7.4-zip

After installing PHP 7.4, go and configure some basic settings that may be required for Magento to function properly.

Run the commands below to open PHP

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

Below are good settings to configure for most Magento websites.

file_uploads = On
allow_url_fopen = On
short_open_tag = On
memory_limit = 256M
cgi.fix_pathinfo = 0
upload_max_filesize = 100M
max_execution_time = 360
date.timezone = America/Chicago

That should get PHP 7.4 installed with some basic settings to allow Magento to function.

Step 4: Create Magento Database

When all the servers installed above, it’s now time to begin setting up Magento environment. First, run the steps below to create a blank database for Magento to use.

Logon to MariaDB database console using the commands below:

sudo mysql -u root -p

Then create a database called magento

CREATE DATABASE magento;

Next, create a database user called magentouser and set password

CREATE DATABASE magento;

Next, create a database user called magentouser and set password

CREATE USER 'magentouser'@'localhost' IDENTIFIED BY 'new_password_here';

Then grant the user full access to the database.

GRANT ALL ON magento.* TO 'magentouser'@'localhost' WITH GRANT OPTION;

Finally, save your changes and exit.

FLUSH PRIVILEGES;
EXIT;

Install and configure Elasticsearch

Install Elasticsearch
First, we will install Openjdk11 (Java) as Elasticsearch runs on Java

apt-get install -y openjdk-8-jdk

Next, verify if the java was installed properly and check its version with this syntax

java -version

Now we can start installing Elasticsearch

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

Add Elastic-7.x.list to sources.list.d folder.

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

Finally, install Elasticsearch:

sudo apt update
sudo apt install elasticsearch
sudo systemctl start elasticsearch

Run this line to verify if elasticsearch is working properly

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

nano /etc/elasticsearch/jvm.options

Minimum requirements: If your physical RAM is <= 1 GB
# Xms represents the initial size of total heap space

# Xmx represents the maximum size of total heap space

 

-Xms128m

-Xmx128m

 

Medium requirements: If your physical RAM is >= 2 GB but <= 4 GB

# Xms represents the initial size of total heap space

# Xmx represents the maximum size of total heap space

 

-Xms512m

-Xmx512m

 

 

Large requirements: If your physical RAM is >= 4 GB but <= 8 GB

# Xms represents the initial size of total heap space

# Xmx represents the maximum size of total heap space

 

-Xms1024m

-Xmx1024m

Step 5: Download Magento

At this point, Magento is ready to be downloaded and installed. Use the commands below to download the latest version of Magento.

To get Magento latest release you may want to use Github repository… Install Composer, Curl and other dependencies to get started…

sudo apt install curl git
curl -sS https://getcomposer.org/installer | sudo php -- --install-dir=/usr/local/bin --filename=composer

After installing curl and Composer above, change into the Nginx root directory and download Magento 2 packages from Github.

When prompted, enter your authentication keys. Your public key is your username; your private key is your password….  ( https://marketplace.magento.com/customer/accessKeys/ )

You’ll have to register for an account to create the key above.

The run the commands below to create a new project called magento.

cd /var/www/
sudo composer create-project --repository=https://repo.magento.com/ magento/project-community-edition magento

Copy and paste the authentication key… (Your public key is your username; your private key is your password)

Output:
Authentication required (repo.magento.com):
Username: 234f2343435d190983j0ew8u3220
Password:
Do you want to store credentials for repo.magento.com in /opt/magento/.config/composer/auth.json ? [Yn] Y

After downloading Magento packages, run the commands below to install Magento with the following options:

cd /var/www/magento

sudo bin/magento setup:install --base-url-secure=https://example.com/ --db-host=localhost --db-name=magento --db-user=magentouser --db-password=db_user_password_here --admin-firstname=Admin --admin-lastname=User --admin-email=admin@example.com --admin-user=admin --admin-password=admin123 --language=en_US --currency=USD --timezone=America/Chicago --use-rewrites=1

  • The Magento software is installed in the root directory on localhost…. Admin is admin;  therefore: Your storefront URL is https://exmaple.com
  • The database server is on the same localhost as the webserver….
  • The database name is magento, and the magentouser and password is db_user_password_here
  • Uses server rewrites
  • The Magento administrator has the following properties:
    • First and last name are: Admin User
    • Username is: admin
  •  and the password is admin123
  • E-mail address is: admin@example.com
  • Default language is: (U.S. English)
  • Default currency is: U.S. dollars
  • Default time zone is: U.S. Central (America/Chicago)

After that, run the commands below to set the correct permissions for Magento 2 to function.

Then run command below to allow www-data user to own the Magento directory.

sudo chown -R www-data:www-data /var/www/magento/
sudo chmod -R 755 /var/www/magento/

Step 6: Configure Nginx

Below is where you configure Nginx VirtualHost file for the Magento site you’re creating. This file defines how client requests are handled and processed.

Run the commands below to create a new VirtualHost file called magento in the /etc/nginx/sites-available/ directory.

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

A very good configuration settings for most magento site on Nginx server is below. This configuration should work great.

Copy the content below and save into the file created above.

upstream fastcgi_backend {
server fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}

server {
listen 80;
listen [::]:80;

server_name example.com www.example.com;
index index.php;

set $MAGE_ROOT /var/www/magento;
set $MAGE_MODE production;

access_log /var/log/nginx/example.com-access.log;
error_log /var/log/nginx/example.com-error.log;

include /var/www/magento/nginx.conf.sample;
}

Save the file and exit.

After saving the file above, run the commands below to enable the new site, then restart Nginx server.

sudo ln -s /etc/nginx/sites-available/magento /etc/nginx/sites-enabled/
sudo systemctl restart nginx.service

At this stage, Magento is ready and can be launched by going to the server’s IP or hostname.

https://example.com

However, if you want to enable SSL or accept web traffic over HTTPS, then you can continue below to install and configure Let’s Encrypt free SSL certificates.

 

nginx.conf

fastcgi_buffers 8 16k;

fastcgi_buffer_size 32k;

fastcgi_connect_timeout 90;

fastcgi_send_timeout 90;

fastcgi_read_timeout 90;