How to Install Apache 2 on AWS EC2 Instance Ubuntu 20.04

How to Install Apache 2 on AWS EC2 Instance Ubuntu 20.04

How to Install Apache 2 on AWS EC2 Instance Ubuntu 20.04

Dear Reader, I hope you are doing great. In one of my previous tutorials, I explained how to install apache web server on Amazon Linux.

Today, I am here to help you install apache 2 on AWS EC2 instance ubuntu 20.04 . If you have gone through my previous tutorial, you might say that we installed httpd in that. Then why apache2 here?

Well, in case of Ubuntu/Debian, the binary to install apache web server is called apache2 instead of httpd. So basically, it doesn’t make any difference. All we are going to do is installing apache web server on EC2 instance ubuntu.

I hope that makes sense 🙂

Steps to Install Apache 2 on AWS EC2 Instance Ubuntu 20.04

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

Let’s get started …

Step 1: Launch an EC2 Instance(Ubuntu 20.04)

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

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

Important Note: Please note that, above tutorial uses Linux 2 AMI. However, to create Ubuntu instance you need to select Ubuntu while choosing AMI for your instance. Rest of the process for creating instance remains same.

While you are in Choose AMI screen, search for ubuntu keyword and select ubuntu server 20.04(free tier eligible) as shown below. Here is how it looks while choosing AMI.

How to Install Apache Web Server on AWS EC2 instance Ubuntu 20.04

Step 2: Connect to your EC2 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. It allows you to SSH into instance from browser itself.

Lazy me 😛

However, if you want to usual SSH client, navigate to the directory where keypair file is there on your local system and then run command like below from your terminal.

ssh -i "DemoKeyPair.pem" ubuntu@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 AWS EC2 instance Ubuntu 20.04 2

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

How to Install Apache Web Server on AWS EC2 instance Ubuntu 20.04 3

Verify that ubuntu is showing in username field and click Connect.

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

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 apt update	
sudo apt install apache2 -y
apache2 -version
sudo systemctl status apache2

Explanation of commands

Update latest package available on the system

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

sudo apt update	

Install Apache Web Server

In this step, we are installing apache 2 on the EC2 instance. We are providing -y option here to run this command silently. If you don’t provide -y option, the CLI will ask you Y/n option and you have to choose Y to install it. We want it install anyway so we are using -y option.

sudo apt install apache2 -y

Verify Apache Installation

Let’s check if apache2 has been installed successfully. You can use below command-

apache2 -version

As you can see in above screenshot, apache 2.4.41 has been installed successfully.

Check Apache Server Status

By default, Apache is configured to start automatically when the server boots. Lets check the status of server by using below command.

sudo systemctl status apache2

At this moment, your apache web server is already installed and started in your Ubuntu 20.04 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.

Why?

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.

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 by Accessing the Server

We have installed apache web server and it is running on our instance. We have allowed web traffic as well in previous step.

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

How to Install Apache Web Server on AWS EC2 instance Ubuntu 20.04 4

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

How to Install Apache Web Server on AWS EC2 instance Ubuntu 20.04 5

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 using ‘>’ creates a problem and the the above command ends up running as ubuntu instead of root resulting in permission denied like below.

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

Let’s switch our user to root here by using sudo su. As you can see in below screenshot, after using this command, root becomes the current user.

Although 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 command runs as root which is not secured.

After switching the user try that command again but this time without sudo as we already are running it as sudo

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

Once you hit enter, the file content gets replaced by hello world things. Let’s verify that.

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.

Congratulation !!!

You have installed and customized apache web server on Ubuntu 20.04.

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 apache2

2. Stop Apache Web Server

sudo systemctl stop apache2

3. Start the Apache If it is Stopped

sudo systemctl start apache2

4. Prevent Apache from loading on system boot

By default, Apache is configured to start automatically when the server boots. If you don’t need that, you can disable it by below command:

sudo systemctl disable apache2

5. To Re-enable Apache to Load on Boot

sudo systemctl enable apache2

6. Force Apache Web Server to refresh configuration files

sudo systemctl reload apache2

Conclusion:

In this post, we learnt to Install Apache 2 on AWS EC2 Instance Ubuntu 20.04 . We launched an Ubuntu Ec2 Instance, connected to it 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:

2 thoughts on “How to Install Apache 2 on AWS EC2 Instance Ubuntu 20.04

  1. Thank you, Thank you, Thank you.

    You saved my day. I was struggling to access my apache page until I saw this tutorial on adding to the Security Group.

    God bless you

Leave a Reply

Your email address will not be published. Required fields are marked *