Apache Server on Amazon Linux 2
How to install Apache web server on Amazon Linux 2
In this tutorial, we will show you how to run Apache web server on an EC2 Instance with Amazon Linux 2 Machine Image.
An Amazon Machine Image is a machine image offered by AWS that contains all the information required to launch an EC2 instance. You will need to select a Machine Image when you launch an EC2 Instance and you can launch several instances from a single machine image if you need similar configuration on all of them. There are several AMIs to choose from including Amazon’s official Machine Images Amazon Linux 2 and 3 as well as Ubuntu and Debian.
Launch an EC 2 Instance
When you have to launch an EC2 instance, you can login to your AWS account and search for EC2. Once you are on the EC2 dashboard, click on launch instances to launch a new instance. Give your new instance a name like ‘My_new_webserver’ or something and then you can move on to select an AMI from the list of provided AMIs.
From the dropdown menu for Amazon Linux AMIs, select Amazon Linux 2.
Now, select the instance type. We are using a t2.micro instance for this tutorial. You can select a larger one based on your requirements. Following that, you will need to create a keypair. This keypair can be used to SFTP to your server and establish SSH connection.
In the network security settings select to allow SSH traffic from anywhere as well as http and https traffic from the internet.
Configure the storage according to your need and then click on launch instance on the bottom right. The instance will be launched and it will be in running state within a few minutes. Wait until it is running and then you can ssh to your instance.
Install Apache web server on EC2 Instance with Amazon Linux 2 Machine Image
Apache is an open source and popular web server used for running websites. It is a highly flexible server and popular for its modular structure that allows you to extend its functionality using Apache Modules.
To install Apache Server on Amazon Linux 2, first SSH to your instance and run the updates. It is a good idea to update the system before installing any software.
Sudo yum update -y
Now, you can install the Apache server using yum package manager:
Sudo yum install httpd -y
Now that you have installed the Apache server, you will need to start it and enable it to start on boot. If you check its status right now, you will find that it has loaded but it is inactive. To check out its status, run the following command:
Sudo systemctl status httpd
You will receive an output like the following:
$ sudo systemctl status httpd
httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
Active: inactive (dead)
Docs: man:httpd.service(8)
So, we will start and enable the Apache server with the following command:
Sudo systemctl start httpd
Sudo systemctl enable httpd
You will receive an output like the following when you enable the Apache server:
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
Now, you can again check the status of Apache server:
Sudo systemctl status httpd
You will receive an output like the following showing that Apache is active and running on your instance:
$ sudo systemctl status httpd
httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
Active: active (running) since Thu 2024-06-27 17:13:00 UTC; 1min 41s ago
Docs: man:httpd.service(8)
Main PID: 3832 (httpd)
Status: "Total requests: 0; Idle/Busy workers 100/0;Requests/sec: 0; Bytes served/sec: 0 B/sec"
CGroup: /system.slice/httpd.service
├─3832 /usr/sbin/httpd -DFOREGROUND
├─3833 /usr/sbin/httpd -DFOREGROUND
├─3834 /usr/sbin/httpd -DFOREGROUND
├─3835 /usr/sbin/httpd -DFOREGROUND
├─3836 /usr/sbin/httpd -DFOREGROUND
└─3837 /usr/sbin/httpd -DFOREGROUND
Jun 27 17:13:00 ip-172-31-21-135.ec2.internal systemd[1]: Starting The Apache HTTP Server...
Jun 27 17:13:00 ip-172-31-21-135.ec2.internal systemd[1]: Started The Apache HTTP Server.
You can test the Apache server by going to the public ip of your instance and you will see the Apache test page being loaded.
We can try running a simple static website on the server. However, we will need to make changes to the root folder. Changer permissions to allow writing to the root folder.
sudo chmod 755 /var/www/html
Add an index.html file to the root folder /var/www/html
Sudo nano /var/www/html/index.html
Add the following content to this file:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My Website</title>
</head>
<body>
<h1>Welcome to my website</h1>
</body>
</html>
Now, go back to your server’s public ip and reload the page. You will see the webpage with the new content you just added.