How to Install Apache Web Server on Amazon Linux 2

Sharing is Caring:

How to Install Apache Web Server on Amazon Linux 2

Dear Reader, I hope you are doing good. Today, I am here to help you install apache web server on amazon Linux 2.

Linux 2: Linux 2 is a very very popular linux AMI provided by AWS itself and also free tier eligible.

Apache Web Server: Apache web server is one of the most used web server when it comes to serving web content online. It is open source and easy to setup.

Although it is easy to setup, being a beginner, we do face problem in even smallest thing when it comes to cloud environment. That’s why, I will guide you in a step by manner on how you can install an apache web server, customize it with our own code and view it locally on your browser.

Steps to Install Apache Web Server on Amazon Linux 2

  1. Launch an EC2 Instance(Linux 2)
  2. Connect to your Linux 2 Instance
  3. Install Apache Web Server
  4. Configure Security group of instance to allow port 80 and 443
  5. Verify the Installation
  6. Customize the web page
  7. View Customized Web page

Let’s get started …

Step 1: Launch an EC2 Instance(Linux 2)

Before we install apache web server on the EC2 instance, we will need an EC2 instance up and running. You can refer my previous tutorial to launch a Linux 2 instance in AWS.

Link to tutorial: How to Launch EC2 Instance Step by Step in AWS

Step 2: Connect to your Linux 2 instance

Once your instance is up and running, you need to connect to your instance. I will be using EC2 instance connect feature for this as it allows to SSH into instance from browser itself.

However, if you want to usual SSH, feel free to use below command from your terminal.

ssh -i /path/my-key-pair.pem ec2-user@instance-public-ip

To SSH from browser using Instance connect, follow below steps.

  1. Select your instance and click connect as highlighted below
How to Install Apache Web Server on Amazon Linux 2

2. Once, you click Connect, you will see a screen with default username for a Linux 2 instance

How to Install Apache Web Server on Amazon Linux 2

verify that ec2-user is showing in username field and click Connect.

A new browser window will open and you will be connected into your instance like below.

How to Install Apache Web Server on Amazon Linux 2

Amazing !!!

Now you are ready to run commands on your EC2 instance. Let’s move to the installation part in next step

Step 3: Install Apache Web Server

Now, we need to run below set of commands one by one, to install apache web server on our instance. Please note that we will use sudo(root privilege ) to run all these command. The reason is, whenever you try to install, remove or change any software, you must have root privilege to do such tasks.

sudo yum update -y
sudo yum install -y httpd.x86_64
sudo systemctl start httpd.service
sudo systemctl enable httpd.service

Explanation of commands

  1. Update latest package available on the system

It’s a best practice to update al the packages to latest before installing anything new.

sudo yum update -y

2. Install Apache Web Server

sudo yum install -y httpd.x86_64

3. Start Apache Server

Above command will install apache web server but it will not start it. You need to explicitly start the server using below command

sudo systemctl start httpd.service

4. Configure Apache to run on system boot

It is your web server and you always would like it to auto start on system boot. Isn’t it?

You can do so by below command

sudo systemctl enable httpd.service

At this moment, your apache web server is already installed and started in your Linux 2 instance. But, you can’t access it right now. if you try to hit the public IP you will get error like This site can’t be reached.


Because, our EC2 instance doesn’t allow web traffic yet.

On the security group, only SSH is allowed as of now.

Let’s change that.

Step 4: Change Security Group of instance to allow port 80 and 443

Let’s allow web traffic on port 80 and 443(Internet traffic for http and https).

Click on your instance Id to see the instance details. Scroll down and click on Security Tab and you should see security group like below.

Click on Security Group

Click on Security Group id link -> Click on Edit Inbound Rule

Cick Save Rule to save the rule

Use Add rule button to add more rule one by one.

Specify rules for HTTP and HTTPS Web traffic from anywhere like above.

Step 5: Verify the Installation

We have installed and started the apache web server.

Our instance allows web traffic now, it’s time to grab the public IP or public dns of the instance.

Click on open address and you should be able to see the tomcat default page like below.

Note: You can see change in IP in above and below screen. The reason is, I stopped and started my instance so public IP changed. For you both of this should remain same.

Feel free to ask me in comment if you have questions.

Install Apache on EC2 Linux

Step 6: Customize the web page

We have seen the default apache page served by apache. Lets modify that.

We will create our own index.html in the Document Root folder which is /var/www/html in our case.

DocumentRoot: Document Root is the directory from which apache looks for and serves web files on your request. So we will create an index.html in /var/www/html folder

You can use various command to create and put content into index.html inside folder. You can check this tutorial

For example- one of the simplest option is to use:

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

But ‘>’ creates a problem and the the above command ends up running as ec2-user instead of root resulting in permission denied like below.

-bash: /var/www/html/index.html: Permission denied

instal apache on ec2

It will work if you switch your user as root using sudo su or sudo sh but I wanted to avoid that.

You might ask me, why?

Well, it is advisable to stick to sudo when performing tasks that require root privileges. By doing so, the current user is only granted privileged for the specified command. On the other hand, su switches to the root user completely and every commnad runs as root which is not secured.

What worked for me?

I used sudo nano command to create the file and put content into it.

sudo nano /var/www/html/index.html

Once, file opens up, paste below content into the file

<!DOCTYPE html>
<h1>Hello World !!</h1>
<p>Welcome to CloudKatha</p>

Save and close the file.

Step 7: View the customized web page

This time when you enter public ip or dns into the browser, you will see your customized page like below.

install apache on linux 2

Congratulation !!!

You have installed and customized apache web server on Linux 2.

Other important commands:

Here are few more commands that you might find useful during your journey with apache web server.

1. Restarts Apache Web server

sudo systemctl restart httpd.service 

2. Stop Apache Web Server

sudo systemctl stop httpd.service

3. Prevent Apache from loading on system boot

sudo systemctl disable httpd.service

4. Force Apache Web Server to refresh configuration files

sudo systemctl reload httpd.service 


In this post we learnt to Install Apache Web Server on Amazon Linux 2 Instance. We learnt a bit about Linux 2 and Apache Web Server. Then, we connected to EC2 instance using browser based connect and we used a set of commands to install apache web server.

We also modified the security group of instance to allow web traffic using port 80 and 443. Finally we customized the apache default page with our own code and we reviewed the modified page.

Feel free to drop a comment in case you face any issue or just to share the feedback.

Enjoyed the content?

Subscribe to our newsletter below to get awesome AWS learning materials delivered straight to your inbox.

Don’t forget to motivate me by-

Suggested Read:

Sharing is Caring:

Leave a Reply

Your email address will not be published.