How to Configure Apache web server on Linux using Ansible
Published: 2023-07-03 17:04:47

In this blog post, we will configure an Apache web server on a Linux Machine (Azure Virtual Machine) using Ansible. Apache web server is a free and open-source software that allows users to deploy their websites on the internet. 

## **Prerequisites**

- Familiarity with the concept of Infrastructure-as-Code (I.a.C) is helpful, but isn't necessary.
- A Linux VM: Although this action is performed on an Azure virtual machine, similar steps could be performed on any Linux distribution. You can follow this link to automate [creation of virtual machines on Azure](https://dev.to/paschalogu/create-azure-virtual-machine-linux-vm-with-terraform-29jp).

So, let's dive in.

## What is Ansible, and why use it?

Ansible is an open-source automation tool that can be used to automate the configuration of servers and systems. It is a command-line IT automation software application that automates cloud provisioning, configuration management, application deployment, and many other IT needs. Read more about Ansible here: [Get Started with Ansible.](https://docs.ansible.com/ansible/latest/index.html) 

Let's connect to our virtual machine via SSH. If you have not already created a vm, you can follow this link to automate [creation of virtual machines on Microsoft Azure with Terraform](https://dev.to/paschalogu/create-azure-virtual-machine-linux-vm-with-terraform-29jp). Connect to the virtual machine

```bash
ssh linuxuser@ip_address
```

Make a directory/folder for this Ansible project and change directory to this folder.

```bash
mkdir ansible
cd ansible
```

## Install Ansible

Make a file for the installation of Ansible. We will call this file `installations.sh`.

```bash
nano installation.sh
```

Copy the code below into the file `installation.sh` Note that python is required to perform Ansible operations, hence it needs to be installed also.

```bash
#!/bin/bash

sudo apt update

sudo apt install python3-pip

pip install boto3 botocore

pip install boto

sudo apt install ansible
```

Run the command to change the permission of the file and make it executable.

```bash
chmod +x ./installation.sh
```

Run the command to install Ansible and the above requirements

```bash
./installation.sh
```

We can test if Ansible is properly configured by running the command below. This should print the version of Ansible installed.

```bash
ansible --version
```

## Create an Ansible playbook

An Ansible playbook is a YAML file with list of instructions that executes against the hosts. It is an organized and systematic unit of scripts which defines work for a server configuration executed by the automation tool Ansible. 

Create a file named `ansible-playbook.yml` and paste the code below. In this playbook, we are specifying two tasks (namely `installing apache2` and `starting apache2`) to be performed on the localhost.  

```bash
---
- name: installing and starting apache2
  hosts: localhost
  become: yes
  
  tasks: 
    - name: installing apache2
      apt: 
        name: apache2 
        state: present
    
    - name: start nginx
      service: 
        name: apache2
        state: started
        enabled: yes
```

Notice that we are running the playbook against `localhost`. This is because we are successfully logged into the host server. However, it is possible to run a playbook against multiple hosts. This is done with the use of an inventory file. Read more: [Ansible Inventory](https://docs.ansible.com/ansible/latest/inventory_guide/intro_inventory.html).

## Test and deploy the Ansible Playbook configuration

To test the configuration and check for errors, run the command below. This would check the correctness of the playbook.

```bash
ansible-playbook ansible-playbook.yml --ask-become-pass --check
```

![Ansible Playbook Deployment](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tqimvjuxfdk5vxbt875y.png)

## Deploy the configuration.

Run the Ansible playbook below to provision and start the Apache2 web server. 

```bash
ansible-playbook ansible-playbook.yml --ask-become-pass
```

Once the playbook has finished running, Apache will be configured on the Virtual Machine. You can test the configuration by opening a web browser and navigating to the public IP address of the VM. You should see the Apache web server homepage.


![Configured Apache web server](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pa3esv58cjgyd5xwgoaz.png)

## Conclusion

Thanks for reading. Let me know if you found this helpful. You are welcome to follow me on [LinkedIn](https://www.linkedin.com/in/paschal-ogu/) and [Twitter @Paschal_ik](https://twitter.com/Paschal_ik).

Previous:


Coming Up Next:
Install LAMP STACK on an Azure Virtual machine
Enable SSL on a Custom Domain on an azure Virtual Machine

References:

[Installing Ansible — Ansible Documentation](https://docs.ansible.com/ansible/latest/installation_guide/intro_installation.html#pip-install)

[Installing Packages — Python Packaging User Guide](https://packaging.python.org/en/latest/tutorials/installing-packages/#ensure-you-can-run-python-from-the-command-line)