Setting Up Apache Web Server in Docker on AWS Linux: A Practical Guide
In this blog post, we’ll walk through the process of setting up and configuring an Apache web server using Docker on an AWS Linux instance. This approach offers flexibility, ease of deployment, and consistency. Let’s dive in!
Prerequisites:
- An AWS EC2 instance running Amazon Linux 2
- Docker installed on your instance
- Basic knowledge of Docker and Linux commands
Step 1: Connect to Your AWS Linux Instance
First, connect to your AWS Linux instance using SSH:
ssh -i your-key.pem ec2-user@your-instance-public-ip
Step 2: Install Docker (if not already installed)
If Docker isn’t installed, you can install it using these commands:
sudo yum update -y
sudo amazon-linux-extras install docker
sudo service docker start
sudo usermod -a -G docker ec2-user
Log out and log back in for the group changes to take effect.
Step 3: Create a Dockerfile
Let’s create a Dockerfile to build our custom Apache image:
mkdir apache-docker
cd apache-docker
nano Dockerfile
Add the following content to the Dockerfile:
FROM httpd:2.4
COPY ./public-html/ /usr/local/apache2/htdocs/
Step 4: Create a Sample Web Page
Create a directory for your web content
mkdir public-html
nano public-html/index.html
Add some sample HTML content:
<!DOCTYPE html>
<html>
<head>
<title>My Apache Docker Server</title>
</head>
<body>
<h1>Hello from Docker!</h1>
<p>This is a sample page served by Apache in a Docker container.</p>
</body>
</html>
Step 5: Build the Docker Image
Now, let’s build our Docker image:
docker build -t my-apache-image .
Step 6: Run the Apache Container
Run your Apache container:
docker run -d --name my-apache-container -p 80:80 my-apache-image
Step 7: Verify the Setup
You can verify that your container is running with:
docker ps
To access your web server, open a web browser and enter your EC2 instance’s public IP address. You should see your sample web page.
Step 8: Configuring Apache (Optional)
If you need to make changes to Apache’s configuration, you can do so by modifying the Dockerfile.
For example, to enable mod_rewrite:
FROM httpd:2.4
COPY ./public-html/ /usr/local/apache2/htdocs/
RUN sed -i 's/#LoadModule rewrite_module/LoadModule rewrite_module/' /usr/local/apache2/conf/httpd.conf
After making changes, rebuild your image and restart your container.
Conclusion
You’ve successfully set up and configured an Apache web server using Docker on AWS Linux. This method provides a lightweight, portable, and easily reproducible web server environment. Remember to secure your EC2 instance and Docker container appropriately for production use.
So, whether you’re a tech enthusiast, a professional, or just someone who wants to learn more, I invite you to follow me on this journey. Subscribe to my blog and follow me on social media to stay in the loop and never miss a post.
Together, let’s explore the exciting world of technology and all it offers. I can’t wait to connect with you!”
Connect me on Social Media: https://linktr.ee/mdshamsfiroz
Happy learning!
Happy Dockerizing!