Need help?

Our experts have had an average response time of 11.7 minutes in August 2021 to fix urgent issues.

We will keep your servers stable, secure, and fast at all times for one fixed price.

How to use Ansible Playbook to Install WordPress with LAMP

by | Feb 1, 2021

Wondering how to use Ansible Playbook to Install WordPress? We can help you.

Suppose we have to install WordPress for testing, development, and main production website. Instead of repeating the installation three times, it is easy to write an Ansible playbook, which can be run from a different location to install WordPress on the target system(s).

Ansible is one of the configuration management tools that help to automate server setup like WordPress.

Using the Ansible playbook also reduces human errors which are prone to occur during manual server setups.

Here at Bobcares, we often handle server setups of our customers with the help of the Ansible playbook as a part of our Server Management Services.

Today let’s see how our Support Engineers use Ansible to automate server setups.

Steps for using Ansible Playbook to Install WordPress with LAMP on Ubuntu 18.04

We will be using Ansible to automate the installation of WordPress with LAMP on Ubuntu 18.04.

The prerequisites for the setup is an Ubuntu 18.04 server with Ansible as a control node and one remote Ubuntu 18.04 server as a host.

The steps our Support Techs follow are given below:

1. Writing the Ansible Playbook

The playbook, in Ansible terminology, consists of a set of hosts on which we can perform automation.

Steps to follow are given below:

1. First we need to create a directory where we can store all of our configurations:

$ mkdir wordpress-playbook
$ cd wordpress-playbook
$ mkdir roles
$ touch hosts
$ touch playbook.yml

2. Next, we will open up the hosts file and add the desired name along with the IP address(es) of the server(s) where WordPress will be installed in the following format:

[wordpress]
wp_server_ip

3. After this we will define the different roles in the roles sub-directory:

$ cd roles
$ ansible-galaxy init server
$ ansible-galaxy init php
$ ansible-galaxy init mysql
$ ansible-galaxy init wordpress

2. Creating Roles

Ansible instructions are written in .yml files which are quite human-readable.

We will write for each individual role of the LAMP stack and WordPress build on top of it.

1. Playbook.yml

First, add the following contents into the file wordpress-playbook/playbook.yml

- hosts: all
gather_facts: False

tasks:
- name: install python 2
raw: test -e /usr/bin/python || (apt -y update && apt install -y python-minimal)

- hosts: wordpress

roles:
- server
- php
- mysql
- wordpress

This installs Python 2.7 onto all the target servers and assigns four roles to the hosts.

2. Server Role

We will add the following contents into the file wordpress-playbook/roles/server/tasks/main.yml

# tasks file for server
- name: Update apt cache
apt: update_cache=yes cache_valid_time=3600
become: yes

- name: Install required software
apt: name={{ item }} state=present
become: yes
with_items:
- apache2
- mysql-server
- php7.2-mysql
- php7.2
- libapache2-mod-php7.2
- python-mysqldb

3. PHP

Here we will install additional PHP modules that write the following contents to the file wordpress-playbook/roles/php/tasks/main.yml

# tasks file for php
- name: Install php extensions
apt: name={{ item }} state=present
become: yes
with_items:
- php7.2-gd
- php7.2-ssh2

4. MySQL

Next, we will add the following contents into the file  wordpress-playbook/roles/mysql/defaults/main.yml

# defaults file for mysql
wp_mysql_db: wordpress
wp_mysql_user: wordpress
wp_mysql_password: randompassword

After this, we configure our main MySQL task of creating a MySQL user, database and granting that user access to the newly created database.
In the file, wordpress-playbook/roles/mysql/tasks/main.yml add the following:

- name: Create mysql database
mysql_db: name={{ wp_mysql_db }} state=present
become: yes

- name: Create mysql user
mysql_user:
name={{ wp_mysql_user }}
password={{ wp_mysql_password }}
priv=*.*:ALL

become: yes

5. WordPress

To get the WordPress tar file from the official site, extract it and modify the wp-config.php with appropriate data we will add the following contents into wordpress-playbook/roles/wordpress/tasks/main.yml

- name: Download WordPress
get_url:
url=https://wordpress.org/latest.tar.gz
dest=/tmp/wordpress.tar.gz
validate_certs=no

- name: Extract WordPress
unarchive: src=/tmp/wordpress.tar.gz dest=/var/www/ copy=no
become: yes

- name: Update default Apache site
become: yes
lineinfile:
dest=/etc/apache2/sites-enabled/000-default.conf
regexp="(.)+DocumentRoot /var/www/html"
line="DocumentRoot /var/www/wordpress"
notify:
- restart apache

- name: Copy sample config file
command: mv /var/www/wordpress/wp-config-sample.php /var/www/wordpress/wp-config.php creates=/var/www/wordpress/wp-config.php
become: yes

- name: Update WordPress config file
lineinfile:
dest=/var/www/wordpress/wp-config.php
regexp="{{ item.regexp }}"
line="{{ item.line }}"
with_items:
- {'regexp': "define\\('DB_NAME', '(.)+'\\);", 'line': "define('DB_NAME', '{{wp_mysql_db}}');"}
- {'regexp': "define\\('DB_USER', '(.)+'\\);", 'line': "define('DB_USER', '{{wp_mysql_user}}');"}
- {'regexp': "define\\('DB_PASSWORD', '(.)+'\\);", 'line': "define('DB_PASSWORD', '{{wp_mysql_password}}');"}
become: yes

Finally, we will add the following snippet to wordpress-playbook/roles/wordpress/handlers/main.yml


# handlers file for wordpress
- name: restart apache
service: name=apache2 state=restarted
become: yes

3. Running Ansible

We can use the following command:

$ ansible-playbook playbook.yml -i hosts -u username -K

[Need assistance? We are happy to help you!]

 

Conclusion

To conclude, we saw the steps our Support Engineers follow for using Ansible Playbook to install WordPress with LAMP on Ubuntu 18.04.

PREVENT YOUR SERVER FROM CRASHING!

Never again lose customers to poor server speed! Let us help you.

Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.

GET STARTED

var google_conversion_label = "owonCMyG5nEQ0aD71QM";

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Privacy Preference Center

Necessary

Necessary cookies help make a website usable by enabling basic functions like page navigation and access to secure areas of the website. The website cannot function properly without these cookies.

PHPSESSID - Preserves user session state across page requests.

gdpr[consent_types] - Used to store user consents.

gdpr[allowed_cookies] - Used to store user allowed cookies.

PHPSESSID, gdpr[consent_types], gdpr[allowed_cookies]
PHPSESSID
WHMCSpKDlPzh2chML

Statistics

Statistic cookies help website owners to understand how visitors interact with websites by collecting and reporting information anonymously.

_ga - Preserves user session state across page requests.

_gat - Used by Google Analytics to throttle request rate

_gid - Registers a unique ID that is used to generate statistical data on how you use the website.

smartlookCookie - Used to collect user device and location information of the site visitors to improve the websites User Experience.

_ga, _gat, _gid
_ga, _gat, _gid
smartlookCookie

Marketing

Marketing cookies are used to track visitors across websites. The intention is to display ads that are relevant and engaging for the individual user and thereby more valuable for publishers and third party advertisers.

IDE - Used by Google DoubleClick to register and report the website user's actions after viewing or clicking one of the advertiser's ads with the purpose of measuring the efficacy of an ad and to present targeted ads to the user.

test_cookie - Used to check if the user's browser supports cookies.

1P_JAR - Google cookie. These cookies are used to collect website statistics and track conversion rates.

NID - Registers a unique ID that identifies a returning user's device. The ID is used for serving ads that are most relevant to the user.

DV - Google ad personalisation

IDE, test_cookie, 1P_JAR, NID, DV, NID
IDE, test_cookie
1P_JAR, NID, DV
NID
hblid

Security

These are essential site cookies, used by the google reCAPTCHA. These cookies use an unique identifier to verify if a visitor is human or a bot.

SID, APISID, HSID, NID, PREF
SID, APISID, HSID, NID, PREF