DEV Community

Antai Okon-Otoyo
Antai Okon-Otoyo

Posted on

Setting Up Nginx on an Amazon EC2 Instance

INTRODUCTION


This document provides a step-by-step guide on how to install and configure the NGINX web server on an Amazon EC2 Instance running on Ubuntu. At the end of this guide, we should have a functional nginx web server set up that serves a custom webpage, demonstrating foundational cloud infrastructure management and web server configuration skills.

STEP 0 - Prepping AWS


We need to set up an EC2 instance. You do that by using the search bar to search for EC2 and clicking on Launch Instance. Then follow the steps below:

  • Select a free tier Ubuntu image as the AMI (Amazon Machine Image).
  • Select the Instance type next, I used free tier eligible t2.micro to eliminate cost.
  • Create security key pair for secure connection to your instance (ensure to keep your .pem file very safe).
  • Ensure you create a security group to allow SSH connection to your instance using your .pem file.
  • You can use default settings to configure instance details.
  • Launch Instance.

Ensure your status check shows "2/2 checks passed" as seen in the screenshot below:

EC2 Launched

Connect to AWS EC2 Instance using code below (for SSH Client):

chmod 400 "nginx-web.pem" (for MacOS Users only)

ssh -i "nginx-web.pem" ubuntu@ec2-44-243-195-121.us-west-2.compute.amazonaws.com

Note: nginx-web.pem should be replaced with the name of your .pem file. AWS would also assist you with commands to run as seen below:

Connection to EC2

STEP 1 - Update Package AND Install NGINX


Run the command below to update the list of packages in package manager.

sudo apt update

Package Update

Run the command below to install nginx (option -y is to automatically say yes to the permission requested during installation)

sudo apt install nginx -y

Nginx Installed

After installing the nginx service, you use the command below to start the nginx service.

sudo systemctl start nginx

The enable command below is to add nginx as part of the system services and ensure that whenever you restart your instance, the nginx service is restarted along side.

sudo systemctl enable nginx

Nginx Enabled

Check the status of your nginx status to ensure it is active with below

sudo systemctl status nginx

Nginx Status

STEP 2 - Create Your HTML File


First, we will navigate to the var directory which is the root path for your HTML files.

cd /var/www/html

Then create and write to your html file using vim with command below.

sudo vim index.html

You press "I" on your keyboard to change into insert mode and insert your code.

Press the esc key to leave insert mode and :wq to write/save and quit.

HTML file created

Now that your index.html file is created, restart nginx for the changes to take effect.

sudo systemctl restart nginx

Nginx Restarted

STEP 3 - Update Security Group FOR EC2 Instance


Under the Security tab of the EC2 Instance, Click on the security group.

Locate Security Group

Edit inbound rules and add rule to allow port 80 to enable us access the HTML page over the internet.

Security Group Updated

You can now open your web browser and verify via the public IP as seen in the screenshot below.

Locate Public IP

Access the html page through your web browser.

http://your-server-ip

Open web browser

Project Completed.

References


-DevOps Engineers
-Cloud Engineers

Top comments (0)