The goal of this Task is to automate the deployment of a web server (Apache or Nginx) using Ansible and configure virtual hosts to serve a static website. The project ensures repeatability, scalability, and minimal manual intervention.
Create an EC2 instance in AWS for hosting the web server.
Configure security groups to allow HTTP/HTTPS traffic.
Install and configure Apache or Nginx.
Set up virtual hosts to host multiple websites.
Deploy a static website with proper permissions.
Clone the static website from GitHub.
Copy the website files to the appropriate directory.
Ensure the correct file permissions and SELinux policies (if applicable).
Verify the web server status.
Check if the website is accessible from the public domain/IP.
Integrate Prometheus and Grafana for monitoring.
Configure access logs for troubleshooting.
Use Jenkins to automate website deployment whenever there are changes in the GitHub repository.
Create an EC2 instance with Terraform.
provider "aws" {
region = "us-east-1"
}
resource "aws_instance" "web" {
ami = "ami-0c55b159cbfafe1f0" # Ubuntu 22.04 LTS
instance_type = "t2.micro"
key_name = "my-key"
security_groups = ["web-server"]
tags = {
Name = "WebServer"
}
}
terraform init
terraform apply -auto-approve
Create an Ansible playbook to install and configure Apache or Nginx.
[webserver]
ec2-instance ansible_host=YOUR_EC2_PUBLIC_IP ansible_user=ubuntu ansible_ssh_private_key_file=~/.ssh/my-key.pem
- name: Setup Web Server
hosts: webserver
become: yes
tasks:
- name: Install Apache
apt:
name: apache2
state: present
update_cache: yes
- name: Start and Enable Apache
service:
name: apache2
state: started
enabled: yes
- name: Copy Virtual Host Configuration
template:
src: vhost.conf.j2
dest: /etc/apache2/sites-available/000-default.conf
notify: Restart Apache
- name: Copy Website Files
copy:
src: website/
dest: /var/www/html/
owner: www-data
group: www-data
mode: "0755"
handlers:
- name: Restart Apache
service:
name: apache2
state: restarted
ansible-playbook -i hosts.ini install_webserver.yml
<VirtualHost *:80>
ServerAdmin admin@example.com
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Place your HTML, CSS, and JavaScript files inside an ansible/website/ directory.
<!DOCTYPE html>
<html>
<head>
<title>My Automated Web Server</title>
</head>
<body>
<h1>Welcome to My Web Server!</h1>
</body>
</html>
ansible-playbook -i hosts.ini install_webserver.yml
Get the public IP of your EC2 instance:
curl http://<YOUR_EC2_PUBLIC_IP>
You should see your static website displayed.
To automate deployment on code changes, set up a Jenkins Pipeline:
pipeline {
agent any
stages {
stage('Checkout Code') {
steps {
git 'https://github.com/your-repo/your-static-site.git'
}
}
stage('Deploy with Ansible') {
steps {
sh 'ansible-playbook -i hosts.ini install_webserver.yml'
}
}
}
}
install_webserver.yml (Installs Apache/Nginx)
vhost.conf.j2 (Configures Virtual Host)
index.html, style.css
✔️ Provisioned EC2 instances using Terraform
✔️ Automated Apache/Nginx installation using Ansible
✔️ Configured Virtual Hosts for serving websites
✔️ Deployed a static website with automated updates
✔️ Integrated with CI/CD Pipeline (Jenkins, optional)
This end-to-end project ensures a fully automated and scalable web server deployment suitable for production use. 🚀