Using Ansible to Install and Configure WordPress with LAMP on Ubuntu featured image

Using Ansible to Install and Configure WordPress with LAMP on Ubuntu

Introduction

There are always times when we need to have new servers configured. This could be to distribute the workload or if we simply need more servers. When setting up new servers, users need to utilize server automation to configure them. This is done in order to minimize manual intervention. Ansible is one of the configuration management tools that can be used to automatically configure a new server reducing the chances of human error when manually setting the server up.

Ansible is a feature-rich tool while being simple in architecture. It facilitates the configuration of servers through scripts. Due to its concise architecture, Ansible does not require any other software to be installed across the nodes.

In this tutorial, we will show you how to use Ansible to automate the installation of WordPress with the LAMP stack running on an Ubuntu server. WordPress is a CMS that is used to create blogs and websites utilizing PHP as coding language and MySQL database to store data. Once we have WordPress installed, users can administer the site using the web frontend.

Prerequisites

Ansible Playbook Actions Summary

In this section, we will see what our Ansible Playbook does when we execute it. This procedure is an alternative to the manual setup provided here: How to Install WordPress with LAMP on Ubuntu 20.04.

When we run the Playbook, Ansible will:

  • Install aptitude which is a preferred Ansible package manager.
  • Install and configure PHP extensions and LAMP packages.
  • Create a new Apache VirtualHost for the WordPress website.
  • Enable the mod_rewrite module and disable the default website offered by Apache.
  • Set the password for the MySQL root user.
  • Remove the anonymous MySQL accounts and test database.
  • Create a new user and a new database. These will be used by the WordPress website.
  • Set up UFW to allow HTTP traffic on the configured port ( 80 by default).
  • Download and unpack WordPress.
  • Setup the directory permissions and ownerships.
  • Configure a new wp-config.php file using its template.

WordPress on LAMP Ansible Playbook Guide

First, we will have to obtain the WordPress on LAMP playbook and its dependencies from the do-community/ansible-playbooks repository. We will clone this repository inside the Ansible Control Node.

You can run the git pull command as mentioned below:

Make sure you have the updated pull of the above mentioned repository. This is if you have cloned the repository before. Below is how you can take the updated:

Now we have to locate the files inside the wordpress-lamp_ubuntu folder. The name of this folder can be different depending upon the version of Ubuntu you are using. At a glance, you will see the following structure in this directory:

Using Ansible to Install and Configure WordPress with LAMP on Ubuntu Dir Struc

Let’s summarize the purpose of these files below:

  • files/apache.conf.j2: Apache uses VirtualHost. This file is used to specify the settings for VirtualHost.
  • files/wp-config.php.j2: This file contains settings for configuring WordPress.
  • vars/default.yml: It configures the playbook settings.
  • playbook.yml: If there are tasks that need to be executed on a remote server, this file is used to configure them.
  • readme.md: Guide to using the playbook.

We have to edit the playbook’s variable file in order to customize the installation. First, open the vars/default.yaml file which is located inside the above mentioned directory:

This file is quite lengthy. It has a number of configurations to help us with our installation:

Here we are concerned with the following variables:

  • php_modules: An array containing PHP extensions that should be installed to support your WordPress setup. If you want to install more modules you can specify this here.
  • mysql_root_password: The password for the root MySQL account.
  • mysql_db: This specifies the name of the MySQL database which WordPress will be using.
  • mysql_user: The name of the MySQL user that should be created for WordPress.
  • mysql_password: The password for the new MySQL user.
  • http_host: Domain name or the IP of the server.
  • http_conf: The name of the configuration file that will be created within Apache.
  • http_port: HTTP port for this virtual host. This port is used to access the website. The default value is 80.

Next, enter the values in the above file and save and close. For the users using the nano editor, press CTRL+X, Y, and press ENTER.

Now you are ready to run this playbook. You can run this playbook on one or more servers. If you want to run this playbook on any specific server, you can use -l flag. Further, if you need to specify the user to connect to the remote server, specify the user using the -u flag.

Suppose that we want to execute our playbook on any one server, srvr1 using the user u1, we can run the below command:

Once you run the above command you will get an output like below:

Wait until the playbook execution is completed. Then, navigate to the server_name on your web browser:

You will see the screen:

WP Page

Next, select the language of your choice and press Continue. You will get the next screen prompting some details to conclude the installation:

Using Ansible to Install and Configure WordPress with LAMP on Ubuntu Set User Pass

After that, enter the information and press Install WordPress. It will take some time and then you will see a screen like below:

Using Ansible to Install and Configure WordPress with LAMP on Ubuntu Install Success

Select Login to login into your website and configure the settings:

Homepage

Ansible Playbook Content

Remember the wordpress-lamp_ubuntu folder. It contains a few files inside it. So let us go over them one at a time:

●    vars/default.yml

This file contains values that are used to configure settings for your WordPress website:

●    files/apache.conf.j2

This file is used to configure the Apache VirtualHost:

●    files/wp-config.php.j2

This file is used to configure WordPress. It contains unique keys and salts generated by hash functions:

●    playbook.yml

This file contains all the tasks defined from this setup. It begins by listing a group of servers that are targeted by this setup. It includes the vars/default.yml variable file to load configuration options:

You can modify these files as needed to fit your specific needs for the website you are building.

Conclusion

In this tutorial, we demonstrated how you can automate the installation and setup of a WordPress website running on the LAMP stack on the latest Ubuntu operating system with Ansible.

For more guides on working with Ansible, you can check out the following tutorials on our blog:

Happy Computing!