How to install nginx in Amazon Linux 2023

Amazon Linux 2023 is a popular operating system developers use to build applications on the Amazon Web Services (AWS) cloud platform. Nginx is a powerful web server known for its high performance, scalability, and flexibility. In this tutorial, we will walk you through installing Nginx on Amazon Linux 2023.

Step 1: Connect to your Amazon Linux Instance


To get started, you need to connect to your Amazon Linux instance via SSH or via Session Manager. To connect via SSH run the following command

ssh -i "youkeyfile.pem" ec2-user@{instancePublicIP}

Replace “instancePublicIP” with the public IP address of your Amazon Linux instance.

Step 2: Update the Package Repository


Before installing Nginx, it’s a good practice to update the package repository on your Amazon Linux instance. To do this, run the following command:

sudo dnf update -y

This will update all the packages on your instance to the latest version.

Step 3: Install Nginx

Next, we need to install Nginx on Amazon Linux 2023. To do this, run the following command:

sudo dnf install nginx -y

This command will install the Nginx web server and all its dependencies.

Step 4: Start Nginx Once the installation is complete, start the Nginx service using the following command:

sudo systemctl start nginx

This command will start the Nginx service on your Amazon Linux instance.

Step 5: Verify Nginx Installation

To verify that Nginx is installed and running correctly, use the following command to check the status of the Nginx service:

sudo systemctl status nginx

If Nginx is running correctly, you will see output similar to the following:

● nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
   Active: active (running) since Fri 2023-04-28 14:29:09 UTC; 1 day 8h ago
 Main PID: 30676 (nginx)
   CGroup: /system.slice/nginx.service
           ├─30676 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
           ├─30677 nginx: worker process
           └─30678 nginx: worker process

Step 6: Enable Nginx at Boot

To ensure that Nginx starts automatically when your Amazon Linux instance boots up, use the following command:

sudo systemctl enable nginx

Setup Server Block For Nginx

Please create a server block into this path sudo vi /etc/nginx/conf.d/sameple.com.conf

server {
    listen 80;
    server_name gcptips.com;
    
    root /var/www/wordpress;
    index index.html;
    charset UTF-8;
}

SSL Setup

To install an SSL certificate use Letsencrypt. To do that, first, install Certbot

sudo dnf install python3 augeas-libs

Then Set up a Python virtual environment

sudo python3 -m venv /opt/certbot/
sudo /opt/certbot/bin/pip install --upgrade pip

Run this command on the command line on the machine to install Certbot.

sudo /opt/certbot/bin/pip install certbot certbot-nginx

Execute the following instruction on the command line on the machine to ensure that the certbot command can be run.

sudo ln -s /opt/certbot/bin/certbot /usr/bin/certbot

Run this command to get a certificate and have Certbot edit your nginx configuration automatically to serve it, turning on HTTPS access in a single step.

sudo certbot --nginx

To renew the Certificate automatically you need to set a cronjob via crontab. The commands are given below.

sudo dnf install cronie -y
sudo systemctl start crond.service
sudo systemctl enable crond.service

sudo crontab -e

After running this command, you will see a new blank screen for editing. Over there you need to give the commands which need to be executed and also time and free frequency.

0 3 * * * sudo certbot renew >/dev/null 2>&1

The command above will run this renewal command everyday morning at 3 AM.

That’s it! You have successfully installed Nginx on Amazon Linux 2023. You can now use Nginx to host websites, reverse proxy, or load balance traffic to multiple servers.

Atiqur Rahman

I am MD. Atiqur Rahman graduated from BUET and is an AWS-certified solutions architect. I have successfully achieved 6 certifications from AWS including Cloud Practitioner, Solutions Architect, SysOps Administrator, and Developer Associate. I have more than 8 years of working experience as a DevOps engineer designing complex SAAS applications.

This Post Has One Comment

Leave a Reply