EC2 Create an HTTP server [Amazon AMI]

Hey there! If you want to create an HTTP server on your EC2 Instance, you’ve come to the right place. 

In this article, I’m going to assume you have created an Amazon AMI EC2 instance and connected to it. If you haven’t created and connected to your EC2 instance, please go through the EC2 Create a server article first.

To connect to your instance, click on Connect in Services > EC2 > Instances

You can then copy the ssh string from here. Paste it in the terminal from the directory where your key is to connect to your instance.

Firstly, let’s do a yum update to update installed packages. We need to be the root user to do that, so enter 

sudo su

Then enter

yum update -y

We can now install httpd, i.e. apache to install the apache web server on our EC2 instance.

yum install httpd -y

You should see this at the end.

To start the service we need to say 

service httpd start

service httpd status

service httpd status will give you the current status of the service. Here you can see that the server is running. Let’s also make sure it restarts every time it crashes.

chkconfig httpd on

Now if we do a curl request to localhost, we will get an html in return. This is because apache has added an html file for us. We can replace this content by creating an index.html at /var/www/html. Let’s check it out!

I added a file index.html to the directory and then when you curl localhost, you don’t see anything. That’s because the file is empty. Let’s add some HTML markup in there.

sudo nano index.html

Add the following in or whatever you’d like to see in the page.

Then press 

Ctrl + x

Y

return (or the enter key)

Now if you do a curl request, you will get the new contents. 

So now we can just access this server from our system right? Hold your horses! If you try to access your instance’s public DNS on you browser, you’re going to see… not much

The reason for this is because even though we have created the server, we haven’t opened up our instance for connections on port 80. To do that, go to Services > EC2 > Instances. Here you’ll see your instance listed. Select it and go to Description at the bottom, and look for security groups. Click on launch-wizard-1 or whatever the name of your security group is.

Click on the security group id.

Here as we can see, the server only allows for incoming ssh connections. However we need to allow HTTP connections on port 80. Click on Edit Inbound Rules.

NOTE: You shouldn’t allow ssh connections from all IPs in your production server. This is only for demo purposes.

Add a new item and select HTTP from the dropdown. If you were to allow connections on a different port, you would select TCP and enter your port number. We need to allow connections from everywhere so we’ll keep it open (0.0.0.0/0) in case of HTTP. Go ahead and click save rules

Now it may take a few seconds to reflect, but when you refresh your browser, you should see your new shiny web page.

Every startup starts with a hello world. Congrats, you’ve already made the first step! 🎉

Thank you for reading my article. See you next time 👋