---
title: Ansible Playbook for Apache installation
description: Learn to install Apache using Ansible playbook. Follow our guide to configure Ansible and deploy Apache on your servers effortlessly.
tags: Ansible Apache Playbook
products:
  - instances
dates:
  validation: 2025-07-28
  posted: 2018-08-02
  validation_frequency: 12
difficulty: beginner
usecase:
  - deploy-external-software
ecosystem:
  - third-party
---
import image from './assets/scaleway-apache_defaultpage.webp'

import Requirements from '@macros/iam/requirements.mdx'


Apache is one of the most used open-source web servers globally. This tutorial explains how to deploy and configure Apache on Scaleway Instances with the power of Ansible automation. Before diving in, ensure you meet the prerequisites:

<Requirements />
- [Owner](/iam/concepts/#owner) status or [IAM permissions](/iam/concepts/#permission) allowing you to perform actions in the intended Organization
- Familiarity with [Ansible](/tutorials/ansible-bionic-beaver/) for seamless execution

## Configuring Ansible for Apache

Begin by setting up Ansible to communicate with your Scaleway Instances:

1. Create a dedicated directory:
    ```bash
    mkdir ansible-apache
    ```
2. Navigate to the directory:
    ```bash
    cd ~/ansible-apache/
    ```
3. Create an `ansible.cfg` file:
    ```bash
    nano ansible.cfg
    ```
4. Populate it with the host file configuration:
    ```ini
    [defaults]
    hostfile = hosts
    ```
5. Create and edit a `hosts` file:
    ```bash
    nano hosts
    ```
6. Define your host information:
    ```ini
    [apache]
    secondary_server_ip ansible_ssh_user=username
    ```

## Creating a playbook

Utilize Ansible playbooks for configuration and deployment:

1. Craft a playbook named `apache.yml`:
    ```bash
    nano apache.yml
    ```
2. Insert the following content:
    ```yaml
    - hosts: apache
      tasks:
        - name: run echo command
          command: /bin/echo hello world
    ```
3. Execute the playbook:
    ```bash
    ansible-playbook apache.yml
    ```

## Installing Apache

Install Apache on your Scaleway Instance:

1. Update `apache.yml` with the apt module:
    ```bash
    nano apache.yml
    ```
2. Replace the existing content with:
    ```yaml
    ---
    - hosts: apache
      become: yes
      tasks:
        - name: install apache2
          apt:
            name: apache2
            update_cache: yes
            state: latest
    ```
3. Run the playbook:
    ```bash
    ansible-playbook apache.yml --ask-become-pass
    ```

## Configuring Apache modules

Enable Apache modules as needed:

1. Open `apache.yml`:
    ```bash
    nano apache.yml
    ```
2. Include module activation and restart handler:
    ```yaml
    ---
    - hosts: apache
      become: yes
      tasks:
        - name: install apache2
          apt:
            name: apache2
            update_cache: yes
            state: latest

        - name: enable mod_rewrite
          apache2_module:
            name: rewrite
            state: present
          notify:
            - restart apache2

      handlers:
        - name: restart apache2
          service:
            name: apache2
            state: restarted
    ```
3. Execute the playbook:
    ```bash
    ansible-playbook apache.yml --ask-become-pass
    ```

## Configuring Apache options

Customize Apache configuration as per your requirements:

1. Open `apache.yml` for editing:
    ```bash
    nano apache.yml
    ```
2. Adjust port settings:
    ```yaml
    ---
    - hosts: apache
      become: yes
      tasks:
        - name: install apache2
          apt:
            name: apache2
            update_cache: yes
            state: latest

        - name: enable mod_rewrite
          apache2_module:
            name: rewrite
            state: present
          notify:
            - restart apache2

        - name: apache2 listen on port 8081
          lineinfile:
            dest: /etc/apache2/ports.conf
            regexp: "^Listen 80"
            line: "Listen 8081"
          notify:
            - restart apache2

        - name: apache2 virtualhost on port 8081
          lineinfile:
            dest: /etc/apache2/sites-available/000-default.conf
            regexp: "^<VirtualHost \*:80>"
            line: "<VirtualHost *:8081>"
          notify:
            - restart apache2

      handlers:
        - name: restart apache2
          service:
            name: apache2
            state: restarted
    ```
3. Run the playbook:
    ```bash
    ansible-playbook apache.yml --ask-become-pass
    ```

Once Ansible is fully set up, you can visit your web browser on port 8081 (rather than port 80). In most web browsers, this can be easily achieved by adding :port onto the end of the URL: http://public_ip_address:8081/.

<Lightbox image={image} alt="" />
<ClickableBanner
  productLogo="generic"
  title="Automate your web server deployment using Ansible on Scaleway."
  url="https://account.scaleway.com/register"
  label="Create your account"
/>