Containerized Web Hosting: A Guide to Deploying Multiple Websites with Docker, Nginx, and AWS Route 53 Subdomain Redirection
YouTube Video Link: https://www.youtube.com/watch?v=_o4JzINqWoY (This video is not in English, but step by step practical explanation of this article)
Introduction:
In today’s digital landscape, containerization has become a popular approach for deploying and managing applications. Docker, a leading containerization platform, allows developers to package applications and their dependencies into containers for seamless deployment. In this blog post, we will explore a use case where two different dummy websites are deployed using Docker containers and Nginx, with domain management facilitated by AWS Route 53.
Prerequisites:
Before we dive into the details, ensure you have the following prerequisites:
- Docker installed on your server or local machine.
- Nginx is installed on your server or local machine and configured to act as a reverse proxy.
- AWS Route 53 account with the domain “xyz.com” registered.
Step 1: Launching AWS EC2 Ubuntu 22.04 instance
Begin by creating an AWS EC2 instance (I have created an Ubuntu 22.04 EC2 instance).
Step 2: Docker and Nginx installation on AWS EC2 Ubuntu instance
#Linux commands to install the nginx and docker
apt update
apt install -y nginx
#download the script to setup the docker engine
curl -fsSL https://get.docker.com -o install-docker.sh
cat install-docker.sh
sudo sh install-docker.sh
Once nginx and docker are installed verify them:
systemctl status nginx.service
systemctl status docker.service
#We need to add the user to the docker group
sudo usermod -aG docker $USER
Step 3: Setting up Docker Containers:
Creating two Docker containers for your dummy websites. For this example, let’s call them Container “nginx-abc” and “nginx-def”. You can use any web application or static content inside these containers.
# Commands to create Docker containers "nginx-abc" and "nginx-def"
docker run -d --name nginx-abc -p 8080:80 nginx
docker run -d --name nginx-def -p 8081:80 nginx
Step 4: Configuring Nginx for Subdomain Redirection:
Now, set up Nginx as a reverse proxy to redirect traffic based on subdomains. Create a configuration file, e.g., "abc.conf" and “def.conf”
, and configure the server blocks.
server {
listen 80;
server_name abc.syedusmanahmad.com;
location / {
proxy_pass http://127.0.0.1:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
server {
listen 80;
server_name abc.syedusmanahmad.com;
location / {
proxy_pass http://127.0.0.1:8081;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
Note: I have used my domain name “syedusmanahmad.com”, so replace your domain name with my domain name.
#Restart Nginx Server
sudo systemctl restart nginx
Step 5: Access the containers and cat the “index.html” file
I have connected with both of the containers and cat the “index.html” file to show you the content of the page.
Step 6: Allow Ports 80, 8080 and 8081 in the AWS EC2 instance Security Group
Step 7: Configuring AWS Route 53:
I have created subdomains “abc.syedusmanahmad.com” and “def.syedusmanahmad.com”. Define the A records to the destination IP address of our AWS EC2 instance machine.
Step 7: Final step verification
Conclusion:
Congratulations! You have successfully deployed two Docker containers with Nginx as a reverse proxy, redirecting traffic from subdomains “abc.syedusmanahmad.com” and “def.syedusmanahmad.com.” AWS Route53 is crucial in managing the domain and directing traffic to the appropriate IP addresses. This setup allows a scalable and organized approach to hosting multiple websites on a single domain.
I hope you enjoyed reading this article, please feel free to contact me Syedusmanahmad if you have any questions.
Please feel free to write @ engr.syedusmanahmad@gmail.com | Linkedin https://www.linkedin.com/in/engrusman-ahmad for any queries on AWS/DevOps & stay tuned for the next write-up.
If this post was helpful, please click the clap 👏 button below a few times to show your support for the author 👇