DEV Community

Cover image for Automate NGINX Deployment on AWS EC2 Server using Bash Script
Taiwo Akinbolaji
Taiwo Akinbolaji

Posted on

Automate NGINX Deployment on AWS EC2 Server using Bash Script

Introduction

In this guide, we’ll walk through how to launch a free t2.micro EC2 instance running Amazon Linux, and use the user data section to run a bash script that updates the system packages, installs NGINX, and starts the service automatically. After deployment, we’ll confirm that NGINX is properly installed and running by accessing the instance through its public IP address.

Next, we will take things a step further by carrying out the same process using the AWS CLI, so you can see how to automate everything from the command line.

If you want to explore even more, we will also create an Amazon Machine Image (AMI) from the configured instance and launch a new EC2 instance from that AMI to verify that the web server works right out of the box.

Background

AWS
Amazon Web Services (AWS) is an extensive cloud computing platform that delivers both Infrastructure as a Service (IaaS) and Platform as a Service (PaaS) solutions. It offers flexible, scalable tools for computing power, storage, databases, analytics, and a wide range of other cloud services.

NGINX

NGINX is an open-source tool used for web serving, reverse proxying, caching, load balancing, media streaming, and various other functions. It was originally developed as a high-performance, highly stable web server. Its long-standing popularity largely comes from its ability to scale efficiently—even on limited hardware—and its low resource consumption.

Amazon EC2 (Elastic Compute Cloud) Instance
An Amazon EC2 instance is essentially a virtual machine provided by AWS that delivers on-demand computing power. With EC2, there’s no need to purchase physical hardware upfront, allowing you to build and deploy applications much more quickly and at a reduced cost.

Step 1: Launch EC2 Instance
To begin, let’s launch our EC2 instance. In my earlier guide, “Create EC2 Instance and Install Apache Web Server,” I walked through the full process of setting up a Linux-based EC2 instance.

For this tutorial, we’ll name our instance “NGINX_Instance” and select Amazon Linux 2 AMI, which is eligible for the Free Tier, as our base image—just as shown in the illustration below.

Click or press enter to view the image in full size.

In the instance type section, let’s select the t2. micro free tier eligible instance type.

Create new key pair or select existing key pair.

Security Group

Next, we will create a security group for the instance. A security group acts as a virtual firewall, managing the inbound and outbound traffic that can access the EC2 instance.

We need to add rules to our firewall (security group) settings.

Rules:

  1. Allow SSH traffic from internet.
  2. Allow HTTP traffic from internet.

Step 2: Create Bash Script

Now Let’s head to the advance details section as shown below.

Scroll down to the User Data section. This is where we can enter a bash script that will run automatically when the instance starts. The script will update all system packages, install NGINX, and start the service without any manual steps.

Ensure 1 instance is selected on the “Number of Instance Section” on the right hand side as shown below.

Click launch instance on the bottom right hand side to launch our instance.

Successful!

Click on view instances to see our “NGNX_Instance” up and running

Well done! If you have followed along up to this stage, we have successfully completed what I like to call “Scripting NGINX on EC2.” The next step is to confirm that our instance (NGINX_Instance) actually has the NGINX web server installed and running. We can verify this by entering the instance’s public IPv4 address in a browser.

To see the Public IPV4 of our instance, click on the Instance ID. For me my IPV4 address is 35.176.102.1

Step 3: Verify If NGINX Web server Is Installed

Now let’s confirm that NGINX was installed correctly on our instance. To do this, open a web browser and enter the instance’s public IPv4 address, for example: http://35.176.102.1

Note: Search http://IPV4 on the web browser for it to be successful.

If you see the page below, the NGINX web server is successfully installed and working.

Congratulations!!

You have successfully launched an EC2 t2.micro instance using the Amazon Linux 2 AMI, which is eligible for the free tier.

An automated batch script was used to update all packages, install NGINX, and start the service. The setup was confirmed by accessing the instance's public IPv4 address in a web browser.

If you have got this far, well done!

Thanks for reading. I hope it was worthwhile to you.

Top comments (0)