Installing Nginx Using AWS EC2 User Data

Sai Dilip
Sai Ops
Published in
4 min readJan 2, 2022

--

Photo by Sigmund on Unsplash

What is Nginx?

  • Nginx is open-source software and a tool that has the feature set to be many things: a web server, reverse proxying, caching, load balancing, media streaming, logging, and many more.

Terms and Explanations

Web server - uses hardware and software to serve data/content via HTTP protocol
Reverse Proxying - is a server that resides in front of your origin server (main server) accepting public requests and relays those requests to your origin server so that your origin server details are hidden from public internet
Caching - is a copy of your frequently accessed data that can be queried quickly by your application for fast results.
Load Balancing - distributes the traffic/workload evenly to multiple serves increasing high availability
Media Streaming - "delivers video and audio content to clients who request it"
Logging - storing the results of application/server behavior as logs

Requirements

  • AWS Account
  • VPC, Internet Gateway, Public Subnet should be created already

Installing Nginx in AWS EC2

  1. Inside the AWS EC2 Service, click on Launch instances

2. Choose the Amazon Linux 2 AMI with 64-bit (x86). Click on Next.

3. Choose an Instance type that will match your application load. I chose t2.micro for demo purposes. Click on Next: Configure Instance Details

4. Choose your desired VPC, public subnet and enable Auto-assign Public IP

5. Scroll down and add the following lines to the User Data section.

#!/bin/bash
sudo yum update -y
sudo amazon-linux-extras install nginx1 -y
sudo systemctl enable nginx
sudo systemctl start nginx

6. Click on Next: Add Storage. Leave it to default unless you need more storage

7. Click on Next: Add Tags. Here are sample tags you can use for yours.

8. Click on Next: Configure Security Group. Create a new security group with SSH access from your public IP and port 80 from everywhere

9. Click on Review and Launch. Review all the settings, click on Launch

10. You will be asked to either create a key_pair or use an existing one to use to access the instance. I created a new one.

A key pair consists of a public key and a private key that is used to prove your identity when connecting to an instance

11. After the instance has been created, and fully initialized, go to a browser and type in the public IP of that instance to verify that Nginx has been installed.

http://ec2-public-ip

Troubleshooting

  • If you don’t see Nginx welcome page, check the System Logs of the ec2 where you will find out if the user data script ran correctly.
  • Another way to troubleshoot is to ssh into the instance and check if nginx service is running “sudo systemctl status nginx”, and check the error logs in “/var/log/nginx/” if they exist

What’s next?

  • As mentioned above, you can use this instance as a web server, reverse proxying, caching, load balancing, media streaming, logging and etc.
  • Go through the configuration files, commands, what files/ packages/folders are involved, and try to create your own web server

Tutorials

--

--