Y NOT — Install an Apache Server on AWS EC2 Instance

Nagarajan Sivathanu
3 min readApr 19, 2021

--

First thing we have to do is to have an EC2 Instance launched. If not available, please create a new EC2 Instance. Steps to launch an EC2 Instance have been detailed out on https://nagarajansivathanu.medium.com/y-not-launch-an-ec2-instance-in-aws-cloud-platform-dfddfe79b728

Once we have our EC2 Instance up and running, try to connect to that instance remotely (via Putty if connected via windows or via ssh username@hostname on Linux machines)

Step 1: Type sudo su command to switch to root account

Step 2 : Type yum update -y to update all packages on EC2 machine. -y param was to allow the updates to go through without prompting us to acknowledge

Step 3: Type yum install -y httpd.x86_64 to install httpd service on our EC2 Instance.

Step 4: Type systemctl start httpd.service command to start the apache httpd service. This service would get killed/terminated when we stop the EC2 Instance and it requires users to explicitly start the httpd service again upon launching EC2 instance everytime.

To enable the httpd service to automatically get started whenever EC2 instance gets started/restarted, type systemctl enable httpd.service command.

To stop the service, type systemctl stop httpd.service

Step 5: By default, index.html gets called/executed upon receiving http request (eg localhost:80). For our usecase, let us try to print the username on index.html.

Type echo “Hello World from $(hostname -f)” > /var/www/html/index.html

This command will overwrite the contents of index.html residing on /var/www/ with the contents we gave.

Make sure, we have appropriate Security Group to allow inbound traffic on http ports.

Step 6: If we hit the public IP of the EC2 Instance from our client web browser, we will see something the index.html page getting loaded

It is possible to bootstrap our instances using EC2 User Data Scripts. Bootstrapping means launching commands automatically when a machine starts. That script is only run once when the instance first starts.

EC2 User Data is used to automate boot tasks such as installing updates, installing softwares, downloading common files, service start (like httpd) etc.

EC2 User Data Script run with root user

Hope you enjoyed this article!

--

--