Ravindra BagaleCourses & study guides

Guides

How to Install Nginx on Amazon Linux 2023 (EC2)

To install Nginx on Amazon Linux 2023, connect to the EC2 instance and run sudo yum install -y nginx, then sudo service nginx start and sudo systemctl enable nginx. Allow HTTP (TCP port 80) in the instance's security group and open http://<PUBLIC_IP> in a browser to see the Nginx welcome page. The default web root on Amazon Linux 2023 is /usr/share/nginx/html.

Hello friends! In this guide we will install Nginx on an Amazon Linux 2023 EC2 server and open our first web page in the browser. Now a little story: suppose I want to propose to my GF. So I make a small static website – "Will you be my Valentine?" on index.html – host it on EC2 with Nginx, and then simply send her the link. Come on, type every command along with me.

Quick answer

On an Amazon Linux 2023 EC2 instance, run these four commands, allow HTTP (TCP port 80) in the instance's security group, and open http://<PUBLIC_IP> in your browser:

sudo yum install -y nginx
sudo service nginx start
sudo systemctl enable nginx
curl -I http://localhost        # expect HTTP/1.1 200 OK

What do I need before installing Nginx?

How does a web request reach Nginx on EC2?

How a web request reaches Nginx on EC2 A laptop and a mobile phone send an HTTP request over the internet. It passes the security group, which allows port 80, and reaches Nginx on the EC2 instance. Nginx sends the response page back the same way. Request ← Response Laptop Mobile Internet Security group Port 80 open EC2 instance Nginx index.html 200 OK Request Response Request Response

The browser on a laptop or mobile sends a Request over the internet. The security group lets it in on port 80, Nginx on EC2 answers with the page, and the Response travels back.

How do I install Nginx on Amazon Linux 2023?

Step 1 — Launch the instance and connect (short version)

In the EC2 console click Launch instance, choose the Amazon Linux 2023 AMI, a free-tier eligible type such as t3.micro or t2.micro, a key pair, and a security group with SSH 22 from My IP and HTTP 80 / HTTPS 443 from Anywhere-IPv4. Keep Auto-assign public IP enabled. When the instance shows Running and 2/2 checks passed, connect from your laptop:

chmod 400 mykey.pem
ssh -i mykey.pem ec2-user@<PUBLIC_IP>

The login user on Amazon Linux is ec2-user.

Step 2 — Allow HTTP (port 80) in the security group

The security group is the firewall in front of your instance. For a website it needs these inbound rules:

Type Protocol Port Source Why
SSH TCP 22 My IP Only you can log in
HTTP TCP 80 Anywhere-IPv4 0.0.0.0/0 Website
HTTPS TCP 443 Anywhere-IPv4 0.0.0.0/0 Secure website (later)

If your instance was launched without port 80, open the instance's security group from the Security tab and add the HTTP rule (details: How to Open Port 80 and 443 in an EC2 Security Group). Never open port 22 to 0.0.0.0/0.

Step 3 — Install Nginx

sudo yum install -y nginx

On Amazon Linux 2023 the package and the service are both called nginx. (On the older Amazon Linux 2 the command is sudo amazon-linux-extras install -y nginx1; everything else in this guide is the same.)

Step 4 — Start Nginx and enable it at boot

On Amazon Linux 2023 the service does not start by itself after install, so start it now and enable it for every reboot:

sudo service nginx start
sudo systemctl enable nginx
sudo service nginx status          # look for "active (running)"

Step 5 — Check that Nginx works

First test from inside the server, then from your browser:

sudo nginx -t                      # test configuration syntax
curl -I http://localhost           # expect HTTP/1.1 200 OK

Now open http://<PUBLIC_IP> in your browser — note http, not https. You should see the Nginx welcome page. Server ready! 🎉

Ravindra Bagale's Tip

💡 If the page doesn't open in the browser, don't change the config straight away. First run curl -I http://localhost on the server itself. If you get 200 there but not in the browser, the problem is not in Nginx – it is outside: port 80 in the security group, typing https://, or the wrong IP. Inside and outside – always keep these two tests separate.

Step 6 — Replace the default page with your own

On Amazon Linux 2023 the default web root of Nginx is /usr/share/nginx/html. For our example – a one-page proposal site – write a simple index.html there:

ls /usr/share/nginx/html
sudo tee /usr/share/nginx/html/index.html > /dev/null <<'EOF'
<!DOCTYPE html>
<html lang="en">
<head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1">
<title>A question for you</title></head>
<body style="text-align:center;font-family:sans-serif;padding-top:80px">
  <h1>Will you be my Valentine? ❤️</h1>
  <p>Made by me, hosted on AWS EC2 with Nginx.</p>
</body>
</html>
EOF
curl http://localhost

Refresh http://<PUBLIC_IP> on your phone and your own page appears – that is the link you send. The same steps work for any static site, for example a portfolio or a small shop page. Replacing the default test page before a server goes on the internet is also good security: default pages make a server easy to identify.

For a real site in its own folder with its own server block (for example /var/www/mysite), follow How to Host a Static Website on EC2 with Nginx.

Step 7 — Firewall, port and log checks

Amazon Linux 2023 has no host firewall active by default, so the AWS security group alone controls access. These commands answer the usual questions:

sudo ss -tlnp | grep :80                    # is Nginx listening on port 80?
sudo nginx -t && sudo service nginx reload  # apply a config change safely
sudo tail -n 20 /var/log/nginx/error.log    # read the latest errors

On CentOS Stream 9, firewalld may be running and must allow HTTP as well; on Ubuntu the install command is sudo apt install -y nginx. See the quick reference for each OS.

Ravindra Bagale's Tip

Make sudo nginx -t && sudo service nginx reload a habit. If the -t test fails, the reload does not happen, so one typo cannot take your site down. Don't restart blindly; when an error appears, read the error log first – the file name and line number are right there.

How do I fix common Nginx errors on EC2?

Ghabru naka 😅 — every beginner meets these. Find your symptom in the first column:

Symptom Likely cause Fix
Browser spins, then timeout Port 80 missing in the security group; no public IP; typed https:// Add inbound HTTP 80 from 0.0.0.0/0; check the public IP; use http://
Connection refused Nginx is not running sudo service nginx start, then sudo service nginx status; sudo ss -tlnp
Still shows the welcome page Your file is not in the web root Write index.html into /​usr/​share/​nginx/​html; ls /​usr/​share/​nginx/​html
bind() to 0.​0.​0.​0:​80 failed (98: Address in use) Apache or another Nginx already uses port 80 sudo ss -​tlnp | grep :​80; stop the other server (sudo service httpd stop)
Config change has no effect Forgot to reload, or a syntax error sudo nginx -​t && sudo service nginx reload
Site broke after stop/start The auto-assigned public IP changed Use the new IP, or attach an Elastic IP

Learn it properly

This guide is the short path. The free AWS course explains every step in depth, with labs:

Got it? Install, start, enable, curl – and port 80 in the security group. That's all! Your first website is live now – just send the link and wait for the answer. If you want to add a domain and HTTPS next, continue with the next lessons of the course.

Frequently asked questions

How do I install Nginx on Amazon Linux 2023?

Run sudo yum install -y nginx, then sudo service nginx start to start it now and sudo systemctl enable nginx so that it starts at every boot.

How do I check that Nginx is working?

On the server run sudo service nginx status and curl -I http://localhost; you should see HTTP/1.1 200 OK. sudo nginx -t tests the configuration syntax.

Why does the Nginx page not open in my browser?

Almost always one of: the security group does not allow port 80 from 0.0.0.0/0, you typed https:// but no TLS is configured, Nginx is not running, or the instance has no public IP.

Where are the default website files for Nginx?

On Amazon Linux 2023 the default web root is /usr/share/nginx/html. Put your index.html there.

How do I install Nginx on Amazon Linux 2?

On the older Amazon Linux 2 use sudo amazon-linux-extras install -y nginx1. Everything else (paths, service names) is the same.

How do I install Apache instead of Nginx?

Run sudo yum install -y httpd, sudo service httpd start and sudo systemctl enable httpd, then check with sudo apachectl configtest (expect "Syntax OK").