Blog

Ansible Apache Playbook: Install Your Apache Web Server with Ansible

4/10/2024 1 min read

Installing and Managing Apache Web Server with Ansible

Do you want to install and manage an Apache Web Server on your server? With Ansible, you can do this easily and efficiently. This post shows how to create an Ansible playbook to install Apache, start it, and configure the firewall accordingly.


What is Ansible?

Ansible is an open-source tool for automating IT tasks such as configuration management, application deployment, and task automation. Configuration is done via so-called playbooks, which declaratively describe how a system should look.


Prerequisites

Before you begin, make sure that:

  • Ansible is installed on your control machine
  • You have SSH access to the target server
  • The target server is based on Debian/Ubuntu
  • You have sudo privileges on the target server

Step-by-Step Guide

1. Create Ansible Playbook

Create a new file named install_apache.yml and add the following content:

---
- name: Install Apache Web Server
  hosts: webservers
  become: yes

  tasks:
    - name: Update the apt cache
      apt:
        update_cache: yes

    - name: Install Apache
      apt:
        name: apache2
        state: present

    - name: Start the Apache service
      service:
        name: apache2
        state: started
        enabled: yes

    - name: Open the firewall for HTTP
      ufw:
        rule: allow
        port: '80'
        proto: tcp

2. Configure Ansible Inventory File

Create a file named hosts and add your server:

[webservers]
your-server

Replace your-server with the IP address or hostname of your web server.


3. Run the Playbook

Execute the playbook with the following command:

ansible-playbook -i hosts install_apache.yml

Conclusion

With this simple Ansible playbook, the Apache Web Server can be installed and configured automatically. Recurring tasks are performed reproducibly, significantly simplifying server management. Ideal for clean, maintainable infrastructure.