End-to-End Web Server Deployment Automation

Task Objective

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.

Technology Stack

Task Workflow

  1. Provision Infrastructure (Optional - Using Terraform)
  2. Create an EC2 instance in AWS for hosting the web server.

    Configure security groups to allow HTTP/HTTPS traffic.

  3. Automate Web Server Setup (Using Ansible)
  4. Install and configure Apache or Nginx.

    Set up virtual hosts to host multiple websites.

    Deploy a static website with proper permissions.

  5. Deploy the Static Website
  6. Clone the static website from GitHub.

    Copy the website files to the appropriate directory.

    Ensure the correct file permissions and SELinux policies (if applicable).

  7. Testing & Validation
  8. Verify the web server status.

    Check if the website is accessible from the public domain/IP.

  9. Monitoring & Logging (Optional)
  10. Integrate Prometheus and Grafana for monitoring.

    Configure access logs for troubleshooting.

  11. CI/CD Pipeline (Optional)
  12. Use Jenkins to automate website deployment whenever there are changes in the GitHub repository.

Step-by-Step Implementation

Step 1: Provision Infrastructure (Using Terraform)

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"
  }
}

Run the following commands to apply the Terraform configuration:

terraform init
terraform apply -auto-approve

Step 2: Automate Web Server Setup (Using Ansible)

Create an Ansible playbook to install and configure Apache or Nginx.

Ansible Inventory File (hosts.ini)

[webserver]
ec2-instance ansible_host=YOUR_EC2_PUBLIC_IP ansible_user=ubuntu ansible_ssh_private_key_file=~/.ssh/my-key.pem

Ansible Playbook (install_webserver.yml)

- 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

Run the playbook:

ansible-playbook -i hosts.ini install_webserver.yml

Step 3: Configure Virtual Hosts (Using Templates)

Virtual Host Template (vhost.conf.j2)

<VirtualHost *:80>
    ServerAdmin admin@example.com
    DocumentRoot /var/www/html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Step 4: Deploy Static Website

Place your HTML, CSS, and JavaScript files inside an ansible/website/ directory.

Example index.html:

<!DOCTYPE html>
<html>
<head>
    <title>My Automated Web Server</title>
</head>
<body>
    <h1>Welcome to My Web Server!</h1>
</body>
</html>

Run the playbook again to deploy the website:

ansible-playbook -i hosts.ini install_webserver.yml

Step 5: Testing & Validation

Get the public IP of your EC2 instance:

curl http://<YOUR_EC2_PUBLIC_IP>

You should see your static website displayed.

Step 6: CI/CD Automation (Optional)

To automate deployment on code changes, set up a Jenkins Pipeline:

Jenkins Pipeline Script (Jenkinsfile)

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'
            }
        }
    }
}

Project Deliverables

Summary

✔️ 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. 🚀