DEV Community

Cover image for Automated Web Stack Deployment using Ansible and Nginx on AWS
Hritik Raj
Hritik Raj

Posted on

Automated Web Stack Deployment using Ansible and Nginx on AWS

TLDR;

We used Infrastructure as Code (IaC) to automate server configuration, moving from a manual SSH process to a repeatable, one-command deployment.

Step Action Tool Concept
1. Target Launch AWS EC2 instances (Control & Web) AWS EC2 Infrastructure
2. Code Write a declarative playbook (.yml) Ansible Configuration Code
3. Run Execute the playbook (ansible-playbook) Ansible Automation Engine
4. Result Nginx installed, service running, custom HTML deployed Nginx Web Service

Conceptual Analogy:

Concept Manual Deployment Automated Deployment (Ansible)
Process SSH into server and manually type commands Write commands once in a playbook
Tool Terminal (bash) Ansible Control Node
Goal Get one server running Get any number of servers running reliably

Infrastructure as Code (IaC) means treating your server setup like software. Instead of clicking in a console, you write a configuration file (the playbook) that can be version-controlled and run anywhere.


1. Project Architecture and Inventory

The setup requires two main components running on AWS EC2:

  1. Ansible Control Node: My central hub (where I run ansible-playbook).
  2. Web Server (web1): The target machine (where Nginx is installed).

Inventory (inventory.ini):

This tells Ansible where to connect and how to authenticate.

[web]
web1 ansible_host=ec2-54-89-190-79.compute-1.amazonaws.com ansible_user=ubuntu ansible_ssh_private_key_file=Ansible-project.pem

Enter fullscreen mode Exit fullscreen mode

2. The Automation Code: Ansible Playbook

The custom playbook defines all necessary steps declaratively. It ensures idempotency—meaning you can run it multiple times, and it will only make changes if the server is not already in the desired state.

Playbook (setup_nginx.yml):

---
- name: Install and configure Nginx
  hosts: web
  become: true

  tasks:
    - name: Update APT packages
      apt:
        update_cache: yes

    - name: Install Nginx
      apt:
        name: nginx
        state: present

    - name: Start and enable Nginx
      service:
        name: nginx
        state: started
        enabled: yes

    - name: Deploy custom index.html file
      copy:
        src: /home/ubuntu/site/index.html
        dest: /var/www/html/index.html
        owner: www-data
        group: www-data
        mode: '0644'
Enter fullscreen mode Exit fullscreen mode

3. The Custom Landing Page

To showcase the deployment, I designed a custom, modern HTML page with a dark-mode UI/UX and a gamified success message. This file (site/index.html) is copied directly to the Nginx content root.

Key Features:

  • Professional Dark Mode: Ensures a sleek, developer-friendly aesthetic.
  • Gamified Success: Frames the successful deployment as a "QUEST COMPLETE" achievement.
  • Personal Branding: Clearly credits the architecture to HRITIK RAJ.

4. Execution and Verification

The entire process is initiated from the Ansible Control Node with a single command.

# Run the playbook using the defined inventory
ansible-playbook -i inventory.ini setup_nginx.yml
Enter fullscreen mode Exit fullscreen mode

Verification:

Command Output/Action Status
ansible-playbook ok=, changed=, unreachable=0 Playbook Success
curl http://<WEBSERVER_PUBLIC_IP> Displays the custom, styled HTML content Deployment Success

Result:

The project successfully validates the IaC principle: reliable, repeatable, and automated deployment of a static web stack.

https://github.com/Hritikraj8804/Ansible_and_Nginx_AWS.git

Wanna know more about devops 🤔? check this out - https://www.linkedin.com/in/hritik-raj-8804hr/ -- thanks :)

Top comments (0)