On AWS its preferred to have ubuntu server and install Nginx for Magento 2 websites

Below are commands to run to install Nginx and mysql

sudo apt update
sudo apt install nginx

sudo apt install mysql-server

After installing Mysql change default password  of root user and create new user

sudo mysql

SELECT user,authentication_string,plugin,host FROM mysql.user; 

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'YOUR PASSWORD HERE'; 

FLUSH PRIVILEGES; 

SELECT user,authentication_string,plugin,host FROM mysql.user;

Now install PHP 7.2 for Magento 2.3 and PHP7.4 for Magento 2.4

sudo add-apt-repository ppa:ondrej/php
sudo apt-get update
sudo apt-get install awscli php7.4-fpm php7.4-mysql php7.4-bcmath php7.4-curl php7.4-gd php7.4-common php7.4-intl php7.4-mbstring php7.4-soap php7.4-xsl php7.4-zip php7.4-xml

The next Step creates an apache user which will allow the server for the automatic file generation process and to above file permissing issues.

sudo adduser apacheuser
sudo passwd apacheuser
sudo usermod -g www-data apacheuser
sudo usermod -aG sudo,adm,root,ubuntu apacheuser
sudo chsh apacheuser -s /bin/bash

Enabled New Apache user to access files through FTP or SFTP

sudo nano /etc/ssh/sshd_config
comment line 
PasswordAuthentication no
sudo /etc/init.d/ssh restart

Install composer

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

Install elasticsearch

wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo gpg --dearmor -o /usr/share/keyrings/elasticsearch-keyring.gpg
sudo apt-get install apt-transport-https
echo "deb [signed-by=/usr/share/keyrings/elasticsearch-keyring.gpg] https://artifacts.elastic.co/packages/8.x/apt stable main" | sudo tee /etc/apt/sources.list.d/elastic-8.x.list
sudo apt-get update && sudo apt-get install elasticsearch
sudo service elasticsearch start
// only if needed to reset password
cd /usr/share/elasticsearch
bin/elasticsearch-reset-password -u elastic

//test elasticsearch
curl -X GET --user user:password "http://localhost:9200/_cluster/health?pretty"

Install opensearch

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

sudo apt-key add -

echo "deb https://artifacts.opensearch.org/releases/bundle/opensearch/2.x/apt stable main" | sudo tee /etc/apt/sources.list.d/opensearch-2.x.list

sudo apt update

sudo apt list -a opensearch
sudo apt -y install opensearch=2.5.0
sudo systemctl enable opensearch
sudo systemctl start opensearch

Check opensearch port.

curl -X GET https://localhost:9200 -u 'admin:admin' --insecure
curl -X GET https://localhost:9200/_cat/plugins?v -u 'admin:admin' --insecure

 

Next step would be go to folder /var/www/html or domain root folder and install magento to configure domain

composer create-project --repository-url=https://repo.magento.com/  magento/project-community-edition

Next step would be point domain to server IP and arrange virtual host setup

sudo chown -R apacheuser:www-data /var/www/
sudo chown -R apacheuser:www-data /etc/nginx/sites-available/
sudo nano /etc/nginx/sites-available/sitename
sudo ln -s /etc/nginx/sites-available/sitename /etc/nginx/sites-enabled/
sudo service nginx restart

Install certbot for freeSSL

sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d domain.com

Configure your virtual host file and upload Magento files to the folder

Next step is to install Magento using command line

bin/magento setup:install \
--base-url=https://domain.com/ \
--db-host=localhost \
--db-name=dbname \
--db-user=dbuser \
--db-password=dbpassword \
--admin-firstname=ngage \
--admin-lastname=ngage \
--admin-email=contact@ngagestudios.com \
--admin-user=ngage \
--admin-password=password \
--language=en_US \
--currency=USD \
--timezone=America/Chicago \
--use-rewrites=1 \
--elasticsearch-host=localhost \
--elasticsearch-port=9200 \
--elasticsearch-enable-auth=1 \
--elasticsearch-username=elastic \
--elasticsearch-password="password"

// incase of opensearch

bin/magento setup:install \ 
--base-url=https://domain.com/ \ 
--db-host=localhost \ 
--db-name=dbname \ 
--db-user=dbuser \ 
--db-password=dbpassword \ 
--admin-firstname=ngage \ 
--admin-lastname=ngage \ 
--admin-email=contact@ngagestudios.com \ 
--admin-user=ngage \ 
--admin-password=password \ 
--language=en_US \ 
--currency=USD \ 
--timezone=America/Chicago \ 
--use-rewrites=1 \
--search-engine=opensearch \ 
--opensearch-host=localhost:9200 \ 
--opensearch-port=9200 \ 
--opensearch-enable-auth=1 \ 
--opensearch-username=admin \ 
--opensearch-password=admin


That’s it.