How to Install Apache Web Server on Amazon Linux 2

How to Install Apache Web Server on Amazon Linux 2

Dear Reader, I hope you are doing well. Today, I am here to help you install the Apache web server or httpd on Amazon Linux 2 EC2 instance in a step-by-step manner.

In some of my previous posts, I have shared with you how to install Nginx, Ansible, Docker, Git, Java, PHP 8.1, PHP 8.2 etc. on Amazon Linux 2.

In this post, we’ll see how to install the Apache/httpd web server on Amazon Linux 2.

So are you ready?

Background on Amazon Linux 2 and Apache Web Server

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

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

Although it is easy to set up, being a beginner, we do face problems in even the smallest things when it comes to a cloud environment. That’s why, I will guide you in a step-by-step 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 the Security Group of instance to allow ports 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 the Apache web server or httpd on the EC2 instance, we will need an EC2 instance up and running. You can refer to 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 the EC2 instance connect feature for this as it allows SSH into instances from the browser itself.

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

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

To SSH from the browser using Instance Connect, follow the 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 the default username for a Linux 2 instance

How to Install Apache Web Server on Amazon Linux 2

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

A new browser window will open and you will be connected to 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 the next step.

Step 3: Install Apache Web Server on Amazon Linux 2

Now, we need to run the below set of commands one by one, to install the Apache web server on our instance. Please note that we will use sudo(root privilege ) to run all these commands. The reason is that 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 the latest package available on the system

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

    sudo yum update -y

    2. Install Apache Web Server

    sudo yum install -y httpd.x86_64

    3. Start Apache Server

    The above command will install the Apache web server but it will not start it. You need to explicitly start the server using the 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 the 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 an 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 the Security Group of the instance to allow ports 80 and 443

    Let’s allow web traffic on ports 80 and 443(Internet traffic for HTTP and HTTPS).

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

    Click on Security Group

    Click on the Security Group ID link -> Click on Edit Inbound Rule

    Cick Save Rule to save the rule

    Use the Add rule button to add more rules 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 the open address and you should be able to see the Apache default page below.

    Note: You can see changes in IP on the above and below screens. The reason is, that I stopped and started my instance so the public IP changed. For you, both of these should remain the same.

    Feel free to ask me in a 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. Let’s 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 commands to create and put content into index.html inside the folder. You can check this tutorial

    For example- one of the simplest options 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 privileges 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.

    What worked for me?

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

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

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

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

    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 the Apache web server on Linux 2.

    Other important commands:

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

    1. Restarts Apache Web server

    sudo systemctl restart httpd.service 

    2. Stop the Apache Web Server

    sudo systemctl stop httpd.service

    3. Prevent Apache from loading on the system boot

    sudo systemctl disable httpd.service

    4. Force the Apache Web Server to refresh configuration files

    sudo systemctl reload httpd.service 

    Conclusion:

    In this post, we learned 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 the EC2 instance using a browser-based connection and we used a set of commands to install the Apache web server.

    We also modified the security group of instances to allow web traffic using ports 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 issues 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:

    4 thoughts on “How to Install Apache Web Server on Amazon Linux 2

    1. Hello sir!! I stop instance and again start instance ip changed but it does not open web page of Apache server.

      1. Hello Jasani, Thank you for your comment. That’s normal behaviour. If you stop and start the instance, IP will change. You can get the new IP from the EC2 details screen again and use that to hit your application. If you need IP not be changed, you will need to have elastic IP which I feel is not necessary for your case.

    Leave a Reply

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