In this tutorial, we will guide you through the process of setting up an Apache web server on an Amazon EC2 (Elastic Compute Cloud) instance. By the end of this tutorial, you'll have a functional web server accessible through your EC2 instance's public IP address or DNS name.
Prerequisites
An AWS account (if you don't have one, sign up at aws.amazon.com). During the sign-up process, you'll be asked to provide your credit card information, but don't worry, AWS offers a free tier for the first year, allowing you to use certain services without incurring any charges.
AWS Management Console with an IAM user.
Basic knowledge of the Linux command line (if you're using Linux)
Step 1: Create a t2.micro EC2 instance
Sign in to the AWS Management Console using your IAM user credentials. IAM users have permanent long-term credentials and are used to directly interact with AWS services. Please note that the IAM user account is different from the root user account provided when you first create your AWS account. If you are new to AWS or need guidance on creating an IAM user account from the AWS root account, refer to this link.
Once logged in, navigate to the top left corner of the page and click on "Services." From the drop-down menu, type EC2, select "EC2" under the "Compute" section.
On the EC2 dashboard, locate and click on the "Launch Instance" button. You will notice that the instance will be launched in the region that is geographically closest to you. For example, if you are located in the United States, the default region may be displayed as "US East (N. Virginia)."
Provide a name for your EC2 instance. You can choose any descriptive name, such as "My web server". Additionally, you have the option to add a tag with a name, although this is entirely optional.
For this project, we will be using the Amazon Linux 2023 AMI, which is eligible for the free tier. An Amazon Machine Image (AMI) is a template that contains an operating system and applications, providing all the necessary information to launch an instance, including architecture, launch permissions, and root device storage.
On the AMI selection page, you will find a list of available AMIs with different configurations. Look for the "Amazon Linux 2023 AMI 2023 HVM kernel-6.1" and click on "Select" to choose it.
Next, on the right side of the page, you have the option to choose the number of instances. For this project, we only need one instance, so you can leave the default value of 1.
The next step is to choose a key pair that will allow you to log in to your EC2 instance via the command line interface (CLI). You have two options: you can either select an existing key pair from the dropdown menu or create a new key pair.
If you already have a key pair, choose it from the dropdown menu. If you need to create a new key pair, please visit the link to the documentation on how to create a key pair for detailed instructions on the process.
Having a key pair is essential for securely accessing your EC2 instance, so make sure to either select an existing key pair or create a new one before proceeding.
To configure the network settings and create a security group allowing SSH traffic from anywhere, follow these steps:
Click on "Create Security Group" to create a new security group.
Accept the default settings, which will automatically generate a security group based on the predefined rules.
This security group will be edited later to specify the IP address for running your EC2 instance.
By following these steps, you can create a security group that allows SSH traffic from anywhere. Remember to revisit the security group to specify the IP address when configuring your EC2 instance.
- Skip other processes and launch instance
Step 2: Connect to EC2 Instance
Now, let's connect to the EC2 instance using SSH.
- In your terminal or command prompt, use the following command:
ssh -i location_of_pem_file ec2-user@ec2-instance-public-dns-name
For example, assume that ec2-database-connect-key-pair.pem
is stored in /dir1
on Linux, and the public IPv4 DNS for your EC2 instance is ec2-12-345-678-90.compute-1.amazonaws.com
. Your SSH command would look as follows:
ssh -i /dir1/ec2-database-connect-key-pair.pem ec2-user@ec2-12-345-678-90.compute-1.amazonaws.com
To retrieve the IPv4 DNS of your instance, follow these steps:
Go to the EC2 dashboard in the AWS Management Console.
Click on the instance that you are interested in.
In the instance, details, locate and click on the instance ID.
A pop-up window will appear, displaying detailed information about the instance.
Look for the IPv4 DNS field in the instance information. This field will contain the DNS IP address.
ssh -i /dir1/ec2-database-connect-key-pair.pem ec2-user@ec2-12-345-678-90.compute-1.amazonaws.com
Now you should be logged in.
Step 3: Update and Install Required Packages
Once connected to the EC2 instance via SSH, let's update the package manager and install the necessary software.
Update the package manager using the appropriate command for your Linux distribution:
sudo yum update -y
Install the Apache web server:
sudo yum install httpd -y
Step 4: Start and Enable Apache
After installing Apache, let's start the service and enable it to start on boot.
Start the Apache service:
sudo systemctl start httpd
Enable Apache to start on boot:
sudo systemctl enable httpd
Step 5: Adjust Firewall Settings
Ensure that the necessary ports (e.g., port 80 for HTTP) are open in the EC2 instance's security group to allow incoming traffic. You can adjust the firewall settings through the AWS Management Console.
Go to the EC2 service in the AWS Management Console.
Select your EC2 instance.
Click on the "Security" tab.
Modify the inbound rules of the associated security group to allow incoming traffic on port 80 (HTTP) or any other necessary ports for your application.
Step 6: Test the Web Server
Let's check if the Apache web server is running and accessible.
- Open a web browser and enter your EC2 instance's public DNS or IP address.
If you see the page above, it means that Apache has been successfully installed on your server!
Conclusion
Congratulations! You have successfully connected your EC2 instance to an Apache web server. You can now deploy your web applications or websites on your EC2 instance and make them accessible to the public.
Remember to implement security best practices, such as disabling root login, securing SSH access, and managing user permissions, to ensure the safety and reliability of your EC2 instance.
Feel free to explore further documentation and resources to customize your Apache configuration and deploy your specific applications.