Automating Nginx Deployment on AWS EC2 Using Ansible-Handlers (Real-World DevOps Project)

 

Introduction

In modern DevOps practices, automation is not optional — it is mandatory. Managing servers manually does not scale and often leads to configuration drift and human error. This is where Ansible plays a critical role.

In this article, I will walk you through a real-world DevOps project where I automated the deployment and configuration of Nginx on AWS EC2 using Ansible.
This project follows 
industry-standard practices and is suitable for beginners to intermediate DevOps engineers preparing for interviews or building a strong portfolio.

Project Overview:-

The goal of this project is to:

  • Automate Nginx installation on AWS EC2
  • Configure Nginx using Ansible playbooks
  • Manage servers using Ansible inventory
  • Deploy a custom web page automatically
  • Validate deployment via browser access
  • Follow real DevOps workflow used in production environments

This project is fully automated, agentless, and reproducible.

Architecture Flow:-



  1. Ansible Control Node initiates execution
  2. Inventory file defines target EC2 server
  3. Secure SSH connection is established
  4. Ansible installs and configures Nginx
  5. Service is started and enabled
  6. Web page is deployed
  7. End user accesses application via browser

This architecture reflects how real DevOps teams manage infrastructure.

Services and Tools Used:-

AWS Services:-

  • Amazon EC2 — Control node and target server
  • Security Group — Allows ports 22, 80, and 81
  • VPC — Default AWS networking
  • Key Pair — Secure SSH authentication

DevOps Tools:-

  • Ansible — Configuration management and automation
  • YAML — Playbook definition
  • Nginx — Web server
  • Linux (Amazon Linux) — Operating system
  • SSH — Remote connectivity

Project Structure:-

devops-ansible-handlers-nginx-aws-project/
├── inventory.ini
├── without_handlers.yml
└── README.md
Press enter or click to view image in full size

Each file has a specific role and keeps the automation clean and maintainable.

Inventory File Explained:-

The inventory file defines which servers Ansible will manage.

[targetserver]
172.31.6.233 ansible_user=ec2-user ansible_ssh_private_key_file=/home/ec2-user/terraform.pem

Key Points:

  • Host group name helps organize servers
  • Private IP ensures secure internal communication
  • SSH key enables passwordless authentication
  • No agent installation required on target server

This demonstrates Ansible’s agentless architecture.

Ansible Playbook Explained:-

Play Definition:-

The playbook targets the EC2 server group and runs with elevated privileges.

- name: deploy nginx without handler
hosts: targetserver
become: yes

Variables Section:-

Using variables improves reusability and readability.

vars:
pkg: nginx
svc: nginx
file_path: /usr/share/nginx/html/index.html
conf_file: /etc/nginx/conf.d/custom.conf

Installing Nginx:-

- name: install nginx
ansible.builtin.dnf:
name: "{{ pkg }}"
state: present

This ensures idempotent installation — Ansible installs Nginx only if it is not already present.

Starting and Enabling the Service:-

- name: start and enable nginx
ansible.builtin.systemd_service:
name: "{{ svc }}"
state: started
enabled: yes

This guarantees Nginx:

  • Starts immediately
  • Automatically starts after system reboot

Configuring Nginx:-

- name: configure nginx server block
ansible.builtin.blockinfile:
path: "{{ conf_file }}"
create: yes
block: |
server {
listen 81;
server_name _;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
}

This dynamically creates a custom Nginx configuration and listens on port 81, demonstrating real configuration management.

Deploying the Web Page:-

- name: deploy index.html
ansible.builtin.copy:
dest: "{{ file_path }}"
content: "Deployed by Ansible"

This task deploys a static web page automatically without manual file transfer.

Playbook Execution:-

The entire automation is executed using a single command:-

ansible-playbook -i inventory.ini without_handlers.yml
Press enter or click to view image in full size

This single command performs installation, configuration, and deployment.

Deployment Verification

After successful execution:-

  • Nginx is running
  • Configuration is applied
  • Web page is accessible

Access URL:-

http://<EC2-PUBLIC-IP>:81
Press enter or click to view image in full size

This confirms the automation worked correctly.

Why This Project Matters:-

Many tutorials show toy examples, but this project reflects how:

  • DevOps engineers automate servers in production
  • Configuration consistency is maintained
  • Manual errors are eliminated
  • Infrastructure becomes scalable and repeatable

This makes it interview-ready and portfolio-worthy.