Hello Fiends, In this article I will help you to set up Apache server on EC2 Linux instance. I will cover the details in step by step. And this will help you to set up your own Apache server. I have divided this article in two sections mentioned below:
- Spinning up a Linux EC2 instance
- Configuring the EC2 instance to host Apache server.
Spinning up a Linux EC2 instance:
- Login to your aws console preferably using an IAM user. Please never use a root account in real time scenarios to login and create new services.
- Go the Services tab in the top left corner in the AWS console. Select EC2 under compute section.
- Once you are on EC2 Dashboard, Click on the Running instance link. Under the running instance slink you can see that there are no instances running if you are logging for the first time in the console.
- Going forward on the top of the screen click Launch Instances.
- Now you should be able to see the screen as Step 1: Choose an Amazon Machine Image(AMI).
- Select any one of the AMI from the available list. Make sure that the AMI you select is Free tier eligible for not getting billed. I have selected the one shown in the figure below.
- Now choose the instance type. Instance types comprise varying combinations of CPU, memory, storage, and networking capacity and give you the flexibility to choose the appropriate mix of resources for your applications. Here I have selected t2. micro which is again free tier eligible. We get 750 hours of usage free of cost within the first one year of creation of the AWS account.
- Once you have selected the instance type. Click Next: Configure Instance Details at the right bottom of the page. In this screen you have to select the VPC (VPC is a virtual data center created for you to host your resources. It separates your resources from other resources in the cloud). I have already written post on how to create a public subnet in a VPC in one of my post : https://www.dotnetforall.com/creating-your-first-aws-vpc-for-publicly-accessible-resource-on-internet/.
As you can see in the figure above, the VPC I used it already created by me in previous post. And finally the subnet used is the public subnet.
- In the “Add Storage” screen keep the default options.
- In the “Add Tags” add the Key as “Name” and Value as “WebDMZ”.
- Next in the “Configure Security Group” screen, select the “Add Security Group” option. Here we will create a new security group which allows SSH and HTTP traffic from the web.
Though it is not recommended to allow SSH from the 0.0.0.0/0 IP address range as it opens a vulnerability for the EC2 instance. Notably ours is a demo instance that is I allowed here.
- Finally Click “Review and Launch” and “Launch”. Eventually we are in the “Select Key pair screen” to select the key pair. In one of my previous posts I have mentioned how to create a key pair and hence login to Linux instance from windows : https://www.dotnetforall.com/how-to-login-to-aws-ec2-linux-instance-from-windows-using-putty/ . Accordingly I have selected a key pair which I had already crated.
AWS Cloud EC2 Linux Instance with Apache
- Once you are able to login to EC2 instance run the command “sudo su” to execute further commands as root user.
- Thereupon execute the below commands one by one.
sudo yum update (to update the EC2 instance with latest updates)
yum install httpd -y (to install the apache service)
service httpd start (to start the apache service)
chkconfig httpd on (always start the apache service on restart of instance)
service httpd status (to check the status of service)
Creating My First Web Application On Apache Server
- Once you done with above steps. Go to the folder “/var/www/html” as shown in the below figure.
- Use “ls” commands to see the contents of the folder. There are no files present in the folder currently. Furthermore use “nano index.html” to create a new file in the above folder.
- Once you done with creating of the file you can browse the file from any where in the world using the public IP of the aws ec2 instance.
Conclusion:
In this article I have configured an AWS Cloud EC2 Linux Instance with Apache and installed my first web application. Moreover it was a simple single page hello world example but it can definitely help someone who is struggling to do the initial setup. Once you can successfully do the necessary setup, consequently it is more easier to try more complicated things.
Leave a Reply