In this post we going to use AWS Cloud services to deploy a simple web server using nginx.

In this laboratory we going to start our journey in AWS making a simple web server, first we need to login in our AWS account, if you don't have one, you can create one is free and you will have some free services for 12 months, if you want to know more you can check it here.

Requirements:

  • Basic Linux commands knowledge

Hands-On

  1. Login in your AWS Management Console

2. Go to EC2 in AWS Services, you can use the find bar to to there easily.

3. Go to instances

4. Click on Launch instances

5. In the fist step we need to specify the AMI, you can check the Free tier only, to select AMI that will be free with the 12 month trial of AWS in case you are still learning.

6. For this lab I'm going to use Amazon Linux 2 64-bit AMI, this image works with yum package manager and comes with AWS CLI and some tools to work with AWS, for this lab we are not going to use any of this tools, so you can use select image you feel more comfortable.

7. Choose the instance type and click next

For this lab we can use the t2.micro who is Free tier eligible for the free trial, this type comes with 1 vCPU's and 1 GB memory.

8. Configure the Instance details, in this window, we going to select 1 instance, network by default, subnet by default, and make sure you have "Auto-assign public IP enabled" to connect from internet, and click next

9. Leave the storage as is, this will add 8 GB of general purpose storage, and leave the Delete on Termination checkbox, to delete the block when we don't need the web server.

10. In the next steep is for add tags, we don't going to add any so click in next.

11. The next steep is to configure the security group, this is a firewall on a instance level, as default, it recommends add the port 22, for testing purposes, we going to leave the source as it is 0.0.0.0/0 this means it will accept any traffic coming for any IP in the port 22, and as this is going to be a web server, we need to add a new rule, and add the port 80 with the same parameters.

Add a Rule, select HTTP, protocol TCP, Port 80, Source Custom 0.0.0.0/0, ::/0

Click on Review and Launch.

12. In the next window you will see a summary of all the information, click on launch when you are ready.

13. In the next window, we need to select a key pair to be able to connect via ssh, select create a new key pair, give it a name and Download it.

14. Now launch the instance, in the next window we will get a message about is launching, now go to view instances.

15. Great you have your instance running, give it a name, clicking on the write icon besides the name.

16. Now we need to install the sever in the instance, to do that we need to connect it first.

We are going to connect using linux and a terminal, If you are using windows, you can use putty, please go to this guide.

Click on the instance ID and then in the button Connect.

Click on the SSH client tab

Open a terminal, and go where you download the pem file and use chmod 400 to change the permissions of the pem file

chmod 400 Webserverlab.pem

Now connect to the AWS instance

ssh -i "Webserverlab.pem" ec2-user@ec2-3-17-190-140.us-east-2.compute.amazonaws.com
Change the file and the address, is not going to be the same.

17. Now, in the instance, install the nginix web server

sudo su -
amazon-linux-extras install nginx1

18. Check the nginx status

systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
   Active: inactive (dead)

Dec 07 23:58:47 ip-172-31-20-2.us-east-2.compute.internal systemd[1]: Unit nginx.service cannot be ....
Hint: Some lines were ellipsized, use -l to show in full.

19. Start the nginx server

systemctl start nginx

20. You can check that you are listen on the port 80 with netstat -ntlp

netstat -ntlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      2485/rpcbind        
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      3736/nginx: master  
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      3177/sshd           
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      2952/master         
tcp6       0      0 :::111                  :::*                    LISTEN      2485/rpcbind        
tcp6       0      0 :::80                   :::*                    LISTEN      3736/nginx: master  
tcp6       0      0 :::22                   :::*                    LISTEN      3177/sshd 

21. Now in the AWS console, select again the Instance ID, and copy the Public IP.

22. Open the ip on your browser and you will see the nginx web page.

23. Lets customize the web page for testing.

In the ssh client go to the nginx html folder

cd /usr/share/nginx/html

24. Change the content

echo "Hello world from EC2 and nginx" > index.html

25. Refresh your browser, and you will see the new content.

Great! you now know how to setup a nginx web server with EC2!

In the next Laboratories, we going to go deeper into the AWS services, we goint to talk about VPC, subnets and private and public networks!

Clean UP

  1. Go to instances, select the instance and then select "instance state" and terminate the instance.

2. Terminate the instance.

3. Delete the security group, go to Security groups in EC2

4. Select the security group (do not select default security group) and click on "Actions" and Delete security group

5. Confirm.