Loading...

Watch: Set Up Apache Vhost on Debian with Ansible Playbook

Learn to configure an Apache virtual host on Debian using Ansible. Automate web server setup, document root creation, and firewall rules.

How to deploy a webserver apache httpd virtual host on Debian-like systems with Ansible?

I'm going to show you a live Playbook with some simple Ansible code.

I'm Luca Berton and welcome to today's episode of Ansible Pilot.

Deploy a web server apache httpd virtual host on Debian-like systems

  • install packages => ansible.builtin.apt
  • document root => ansible.builtin.file
  • custom index.html => ansible.builtin.copy
  • Apache virtualhost => ansible.builtin.template
  • enable new site => ansible.builtin.command
  • open firewall => community.general.ufw
  • reload service => ansible.builtin.service

Today we're talking about how to Deploy a web server apache httpd on Debian-like Linux systems.

The full process requires seven steps that you could automate with different Ansible modules.

Firstly you need to install the apache2 package and dependency using the ansible.builtin.apt Ansible module.

Secondly, you need to create the document root with the right permission with the ansible.builtin.file module.

Thirsty, you need to create the custom index.html with the ansible.builtin.copy Ansible module. You could upgrade this step using the template module.

Fourthly, you need to set up Apache configuration for the specific virtual host using the ansible.builtin.template module.

Fifty, you need to enable a new site using the a2ensite via the ansible.builtin.command module.

Sixty, you need to start the apache2 service and enable it on boot and all the dependant using the ansible.builtin.service Ansible module.

Seventy you need to open the relevant firewall service-related ports using the community.general.ufw Ansible module.

## Playbook

How to automate the deployment of a web server apache httpd virtual host on Debian-like systems with Ansible Playbook.

code

  • httpd_debian_vhost.yml

```yaml

---

  • name: setup webserver vhost

hosts: all

become: true

vars:

app_user: "www-data"

http_host: "example.com"

http_conf: "example.com.conf"

http_port: "80"

disable_default: true

tasks:

- name: apache installed

ansible.builtin.apt:

name: apache2

update_cache: true

state: latest

- name: document root exist

ansible.builtin.file:

path: "/var/www/{{ http_host }}"

state: directory

owner: "{{ app_user }}"

mode: '0755'

- name: custom index.html

ansible.builtin.copy:

dest: "/var/www/{{ http_host }}/index.html"

content: |

Read the full tutorial: Set Up Apache Vhost on Debian with Ansible Playbook