First, find out the Magento version you need to install and the required versions of software dependencies and system requirements.

Here I assume the installation version of Magento is 2.4.6-p4

First update the local package index, for that you need to execute
sudo apt update -y

Then upgrade the currently installed packages to their latest available versions
sudo apt upgrade -y


Install Nginx

As per the official guidelines, the specified NGINX version is 1.24. Therefore, you must determine the available versions of Nginx in the Ubuntu package repositories. This can be accomplished by employing the apt-cache madison nginx command.

sudo apt-cache madison nginx

It will list the following nginx packages, where only version 1.18 is available.



This version of Magento can also be installed with NGINX version 1.18. To proceed with this version, you can simply execute the following command:

sudo apt install nginx -y

To install NGINX version 1.24, please follow these steps:

First Install the required packages:
sudo apt install curl gnupg2 ca-certificates lsb-release ubuntu-keyring -y

Import the Nginx signing key:
sudo wget -O- https://nginx.org/keys/nginx_signing.key | sudo gpg --dearmor \ | sudo tee /etc/apt/trusted.gpg.d/nginx.gpg > /dev/null

Make a new directory for gpg:
sudo mkdir -m 600 /root/.gnupg

Verify that the downloaded file contains the proper key:
sudo gpg --dry-run --quiet --import --import-options import-show /etc/apt/trusted.gpg.d/nginx.gpg



Execute the command below to establish the apt repository for stable nginx packages:
sudo echo "deb http://nginx.org/packages/ubuntu `lsb_release -cs` nginx" \ | sudo tee /etc/apt/sources.list.d/nginx.list

Update the package index again
sudo apt update -y

If you check the available packages now using the following command.

sudo apt-cache madison nginx

Now, you can see the following result.



Now that NGINX version 124 is available in the package list, you can run the following command to install NGINX version 1.24.

sudo apt install nginx -y


Install PHP

Execute the following command:

sudo apt install php8.1 php8.1-mysql php8.1-soap php8.1-bcmath php8.1-xml php8.1-mbstring php8.1-gd php8.1-common php8.1-cli php8.1-fpm php8.1-curl php8.1-intl php8.1-zip zip unzip -y

Then we need to configure php-fpm configurations. For that, open the php.ini file from a text editor. 
php.ini file location: /etc/php/8.1/fpm/php.ini

Update the following configurations according to your preferences. 

date.timezone = Asia/Colombo
memory_limit = 4G 
realpath_cache_size = 10M 
realpath_cache_ttl = 7200 
max_execution_time = 18000 
post_max_size = 64M 
upload_max_filesize = 64M

Then restart the php-fpm service by the following command:
sudo systemctl restart php8.1-fpm

Install Elasticsearch

For the Elasticsearch instance to function, Java must be installed on your system. For that, we need to install Java before installing Elasticsearch.

sudo apt install openjdk-11-jdk -y

According to the official guidelines, we can install Elasticsearch version 7.17 or 8.11. Here we select 7.17.
Execute the following set of commands one by one.

sudo wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.17.0-amd64.deb
sudo wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.17.0-amd64.deb.sha512
sudo shasum -a 512 -c elasticsearch-7.17.0-amd64.deb.sha512
sudo dpkg -i elasticsearch-7.17.0-amd64.deb
sudo /bin/systemctl daemon-reload
sudo /bin/systemctl enable elasticsearch.service
sudo systemctl start elasticsearch

Then check the Elasticsearch instance is up and running by the following command.

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

You will get a similar response as follows.



Install the Database Server and PhpMyAdmin

According to the official guidelines, you can install MySQL 8.0 or MariaDB 10.6.

Follow the below steps to install MySQL 8.0

Check the available MySQL version for installation.
sudo apt-cache policy mysql-server

As shown in the result, MySQL version 8.0 is available for installation.


Then install the MySQL Server 
sudo apt install mysql-server -y

Then execute the following command to secure the MySQL installation. 
sudo mysql_secure_installation

To change the MySQL root user password use the following command:
sudo mysql -e "ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'yourPassWord'"


Follow the below steps to install MariaDB 10.6

Check the available MariaDB version for installation.
sudo apt-cache policy mariadb-server

As shown in the result, MariaDB version 10.6 is available for installation.

Then install the MariaDB Server 
sudo apt install mariadb-server -y

Then execute the following command to secure the MariaDB installation. 
sudo mysql_secure_installation

Follow the below steps to install PhpMyAdmin

To avoid the PhpMyAdmin installation interactive window, install the dbconf package and configure it as follows. 

sudo apt install debconf -y
sudo apt install debconf-utils -y 

Then set the configurations by running lines of commands and make sure to change the "{dbRootPass}" with your database root user password. 

sudo debconf-set-selections <<< "phpmyadmin phpmyadmin/dbconfig-install boolean true"
sudo debconf-set-selections <<< "phpmyadmin phpmyadmin/app-password-confirm password {dbRootPass}"
sudo debconf-set-selections <<< "phpmyadmin phpmyadmin/mysql/admin-pass password {dbRootPass}"
sudo debconf-set-selections <<< "phpmyadmin phpmyadmin/mysql/app-pass password {dbRootPass}"
sudo debconf-set-selections <<< "phpmyadmin phpmyadmin/reconfigure-webserver multiselect ''"

Then run the following command to install PhpMyAdmin
sudo apt install phpmyadmin

Once PhpMyAdmin is successfully installed, configuring Nginx involves creating a phpmyadmin.conf file within the /etc/nginx/snippets/ directory. Then, populate the file with the following content:

location /phpmyadmin {
    root /usr/share/;
    index index.php index.html index.htm;
    location ~ ^/phpmyadmin/(.+\.php)$ {
        try_files $uri =404;
        root /usr/share/;
        fastcgi_pass unix:/run/php/php8.1-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include /etc/nginx/fastcgi_params;
    }

    location ~* ^/phpmyadmin/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
        root /usr/share/;
    }
}


After that create the magento.conf file in the location of /etc/nginx/conf.d/magento.conf. You can use any filename here, but it needs to end with .conf extension. Then add the following content in that file. 

upstream fastcgi_backend {
        server unix:/run/php/php8.1-fpm.sock;
}

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

        index index.php index.html index.htm index.nginx-debian.html;

        server_name _;

        include snippets/phpmyadmin.conf;

}


Now check the nginx configuration is correct by running:
sudo nginx -t

If it didn't return any error, you need to restart the nginx server to apply these configurations. 
sudo systemctl restart nginx


Finally, check the browser by appending /phpmyadmin path to your IP address or domain name. 


Composer installation

sudo curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer


Magento installation

For security reasons, we need to create a user for Magento installation, therefore here we create user called magento and add that user to the www-data group. 

sudo adduser magento
sudo usermod -g www-data magento
sudo chown -R magento:www-data /var/www/html/

Then move into the installation directory which is /var/www/html and empty the directory if there is any file or directory. Then run the following command to download the necessary files for Magento installation. Before running this command make sure, you have credentials for Magento installation. You can get them from your Magento account. After running this command, it will ask username and password to proceed with the download. The username is your public key and the password is the private key of your Magento account. 

Also, make sure to run these commands by magento user which we created. 
composer create-project --repository-url=https://repo.magento.com/ magento/project-community-edition=2.4.6-p4 .


After the download is completed, log into the PHPMyAdmin and create a database and database user for this Magento site. 

Then run the following command to start the Magento installation, also, make sure to replace the values with the curly brackets with your preferences. 

bin/magento setup:install \
--base-url={host} \
--db-host=localhost \
--db-name={db_name} \
--db-user={db_user} \
--db-password={db_user_password} \
--admin-firstname={admin_first_name} \
--admin-lastname={admin_last_name} \
--admin-email={email} \
--admin-user={username} \
--admin-password={admin_password} \
--language={language} \
--currency={currency} \
--timezone={time_zone}  \
--use-rewrites=1


After installation is completed, it will give the path to the admin panel and if you visit the browser you can see the following home page.