LAMP with Ansible featured image

Installing and Configuring LAMP on Ubuntu 20.04 with Ansible

Server automation is a secure and reliable process of managing and monitoring servers in an effective and tech-savvy way. Unlike the traditional style of managing servers at data centers controlled by a dedicated team of experts, all servers are managed using automation. As a result, human errors are minimized.

Configuration management tools like Ansible, Puppet, Terraform, to name a few, are typically used to automate servers. These tools set up servers using automation by establishing standard procedures for new servers while eliminating potential errors when done manually.

Ansible is a robust open-source project. It is flexible, easy to use, and has the most simplified architecture. There is no coding background required to use Ansible playbooks and it doesn’t require installing software on the nodes. Above all, it is packed with excellent capabilities that allow it to write scripts and streamline automation.

In this guide, we’ll walk you through the steps of installing and configuring LAMP on Ubuntu 20.04 with Ansible.

Prerequisites

To follow along with this tutorial, you’ll need:

What is the Purpose of the Ansible Playbook?

This Ansible playbook is an alternative way to run through the procedure outlined in our guide LAMP Stack Setup Linux Apache MySQL PHP.

Running the Ansible playbook will result in the following actions on your Ansible hosts:

  • Install aptitude, an alternative to the apt package manager as preferred by Ansible.

  • Install all the necessary LAMP packages.

  • Create a new Apache VirtualHost and configure a dedicated document root.

  • Turn ON the new VirtualHost.

  • Turn OFF the default Apache website i.e. set the disable_default variable to true.

  • Choose a password for the MySQL root user.

  • Remove anonymous MySQL accounts and the test database.

  • Set up UFW to allow HTTP traffic on the configured port, where the default is 80.

  • Set up a PHP test script.

After the Ansible playbook completes running, you will see a web PHP environment running on top of Apache as per the configurations we have set.

Utilizing the Ansible Playbook

First, get the LAMP playbook and its dependencies from the do-community/ansible-playbooks repository. Next, clone the repository with the LAMP playbook to a local folder inside the Ansible Control Node.

Run the git pull command to make sure you have access to the right content we are going to use in this tutorial:

If you are using the do-community/ansible-playbooks repository for the first time, consider cloning the repository to your home folder:

Go to the lamp_ubuntu2004 folder and you’ll see the structure:

Here is an overview of what these files mean:

  • files/info.php.j2: This is a template file where you can set up a PHP test page on the root of the webserver.

  • files/apache.conf.j2: Another template file used to set up the Apache VirtualHost.

  • vars/default.yml: It’s a variable file to customize playbook settings.

  • playbook.yml: This file contains all contents of the tasks to be executed on the remote server/s.

  • readme.md: A read file containing the information about this playbook.

Let’s customize the MySQL and Apache configurations by making changes in the playbook’s variable file. Navigate to the lamp_ubuntu2004 directory and open the vars/default.yml file using nano editor:

On opening the vars/default.yml file, there will be a list of variables that needs to be modified:

Let’s understand each of the variables in detail:

  • mysql_root_password: Stores the password for the root MySQL account.

  • app_user: It is a remote non-root user on the Ansible host that acts as the owner of the application files.

  • http_host: Displays your domain name.

  • http_conf: Displays the name of the configuration file created within Apache.

  • http_port: It’s the HTTP port for this virtual host, and 80 is the default.

  • disable_default: Used to undo default options that come with Apache.

Then, save and close the vars/default.yml file.

Once all the setup is done, we’re ready to run this playbook on the servers. By default, most of the servers on the playbooks are configured to be executed on every server in the inventories. Let’s use the -l flag to impact only a single server or a selected group subset that gets affected by the playbook. Alternatively, we can use the -u flag to get a detailed view of which remote server is getting connected and is executing on the remote hosts.

Let’s execute the playbook on one server server1 and connect it with one user as justin:

You will get output similar to this:

Once you see the playbook completes running, navigate to your web browser and access the host of the server. Don’t forget to attach the /info.php at the end of the IP:

On clicking on this url, you will see a page like this:

25 1

Warning: The page displayed contains sensitive information about your PHP environment. Therefore, it is recommended to remove your personal information from the server using the following command:

rm -f /var/www/info.php

What’s in the Playbook?

Next, let’s understand the meaning and significance of the files used in the ansible-playbook content:

  • vars/default.yml

The default.yml variable file contains the domain name and password of the MySQL root account. These are default values used in the ansible-playbook tasks:

  • files/apache.conf.j2

The apache.conf.j2 file is a Jinja 2 template file used to configure a new Apache VirtualHost. The variables used within this template must be defined in the vars/default.yml variable file:

  • files/info.php.j2

Similar to files/apache.conf.j2, the info.php.j2 file is also a Jinja template. We use this file to set up a test PHP script in the document root of a newly configured LAMP server:

  • playbook.yml

The playbook.yml file is where all tasks from this setup are defined. In this file, all the LAMP stacks are configured. It starts with defining the group of targeted servers and is set to all. Also, it takes the become value as true ( become: true), and defines all the tasks that need to be executed. Next, the yaml file has the default file, which is the vars/default.yml variable file, to load the configuration options:

You can modify these files based on the needs and requirements of your project.

Conclusion

In this tutorial, we went through the steps of installing and configuring LAMP on a remote server with Ansible. There are plenty of other customization options you can experiment with. For instance, using the Ansible official documentation and using the cases of the mysql_user from the Ansible module is an excellent way to level up your game. Also, follow the steps discussed in this guide to practice automation using other configuration management tools and OS to increase the level of difficulty.

Are you new to the LAMP stack and want to dive deep into this topic? Explore the following tutorials on our blog:

Happy Computing!