Installing Nginx Web Server on an AWS EC2
Installing Nginx Web Server on an AWS EC2
In this weeks project, I am going to show you how to deploy a Nginx server with a bash script on a AWS EC2 through the AWS Management Console and then later through the AWS Command Line.
What is Nginx? Nginx is a web server that can also be used as a reverse proxy, load balancer, mail proxy and HTTP cache. The software was created by Igor Sysoev and publicly released in 2004. Nginx is free and open-source software, released under the terms of the 2-clause BSD license.
Let’s start…
Step 1
First step is you are going to need to log into your AWS Management Console, go to EC2 and Launch an instance.
Step 2
Next, we will choose what we want to be included in the instance. Name the instance, my name is “Week6Project”. Pick the OS, I picked “Amazon Linux”
Step 3
On this step you will need to pick the instance type, I picked t2.micro which is a part of the free tier. Then either pick a previously created Key Pair or create a Key Pair and save it to your local machine.
Step 4
Next, I picked an already created Security Group which included ports 22 and 80 that could be connected from any IP. You can create a new Security Group and choose ports 22 and 80 along with cidr 0.0.0.0/0
Step 5
For this example I am only going to create one instance, and the rest is a summary of what I have chosen. If everything looks good click “Launch Instance”
Step 6
The next screen will show the status of your instance, once it shows “Running” your instance is ready.
Step 7
Now go into your command line, I am using Windows Terminal, to SSH into your newly created instance. Use the below command
ssh -i <yourkeypair.pem> ec2-user@<yourpublicip>
Step 8
We now need to create a bash script to update the OS, install Nginx and start the service. Open up a text editor and type out your bash script. Here is my bash script
#!/bin/bash
sudo yum update -y
sudo amazon-linux-extras install nginx1 -y
sudo amazon-linux-extras enable nginx1
sudo systemctl start nginx
Step 9
To get your public IP without having to dig for it I added curl icanhazip.com in my script to show me it. I also cat out the localhost to show me the website is up.
Step 10
But lets put that public IP into a web browser to confirm that the script worked.
WOW IT WORKED! I added some html wizard-ry (not really) to get the below.
Step 11
Now let’s do the same thing except from the command line.
First step is to install AWS CLI. Go to this link https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html and follow the instructions for your OS. I am using Windows
Step 12
Once installed, go into your command line and type
aws configure
You will need to enter your AWS Key ID, AWS Secret Access Key, Default Region Name, and Default Output Format
Step 13
To start lets create a security group for your EC2
aws ec2 create-security-group --group-name <name> --description <description>
Step 14
Next we are going to set which ports will be open on our instance. For this instance we will set ports 22 and 80 as open
aws ec2 authorize-security-group-ingress --group-name Week6 --protocol --port 80 --cidr 0.0.0.0/0
Do the same command for port 22
Step 15
Now lets create a key pair with the following command
aws ec2 create-key-pair --key-name <keypairname>
Once your key pair is created, we will create the script to update the instance and install Nginx.
Step 16
Open up a text editor and create your script, mine looks like this
Step 17
Next we will need the AMI ID for the next step up. You will need to go into the AWS Management Console to retrieve it. Click on AMI Catalog and find your OS and then copy the AMI ID
Step 18
The next step will create the EC2 in your AWS account. Here is the command.
aws ec2 run-instances --image-id <yourimageid> --count 1 --instance-type t2.micro --key-name <yourkeyname> --security-group-ids <yoursecuritygroupname> --user-data file://yourscriptfile.sh
Step 19
After running the above command, go back into AWS Management Console and check your instances. You will see your new instance has been created.
Step 20
Now let’s take the public IP and put it in your browser. Click into your instance to retrieve your public IP. Paste it into your browser and you will find that your newly created instance is hosting the Nginx that was installed.
And that is it! You have successfully created an AWS EC2 instance running Nginx from the AWS Command Line!