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.
how to go to word-press page after the installation
Hi Swaroop,
Please Visit your site’s login page by placing wp-admin after your domain name (e.g., http://example.com/wp-admin).
This article is really good and works better than most Ansible-Galaxy roles however, it seems that the actual database data is not getting copied to the wp-config.php file. Once run, I get a ‘Cannot Establish database connection error’. And surely, upon checking, I can see that the wp-config.php does not have the data passed on. Do you have a fix for this?
Hi ,
Please contact our support through live chat(click on the icon at right-bottom).