Installing WordPress with Ansible (Ubuntu 14.04)
Keyword: Ansible WordPress installation
What Will You Learn in This Guide?
In this guide, you will learn how to automate WordPress installation using Ansible.
Apache, MySQL, PHP and WordPress can be installed with a single command.
An ideal structure is created for repeated installations.
Technical Summary
- Technical Topic: Installing WordPress with Ansible on Ubuntu 14.04
- Solved Problem: Manual installation takes time and risks errors
- Steps Followed:
- Ansible installation
- Inventory (hosts) definition
- Configuration with roles
- LAMP installation
- WordPress auto-configuration
Prerequisites
- A build server (runs Ansible)
- A WordPress target server
- Two servers with Ubuntu 14.04
- Access via SSH key
sudoauthorized user
Passwordless SSH from the build server to the target server is recommended.
1. Ansible Installation
Connect to the Build server via SSH.
sudo apt-get install ansible -y
- This command installs Ansible on the system.
Verify installation:
ansible --version
- Allows you to verify the installation.
2. Creating the Project Structure
- Prepare the working directory:
mkdir ~/wordpress-ansible && cd ~/wordpress-ansible
touch playbook.yml hosts
- These files are the center of automation.
- Create the roles directory:
mkdir roles && cd roles
ansible-galaxy init server
ansible-galaxy init php
ansible-galaxy init mysql
ansible-galaxy init wordpress
- Roles provide reusable modules.
3. Inventory (hosts) File
- edit the hosts file:
[wordpress]
192.0.2.10
- This IP is the server where WordPress will be installed.
4. Playbook Description
- Edit the playbook.yml file:
- hosts: wordpress
roles:
- server
- php
- mysql
- wordpress
- This structure runs all roles sequentially.
Test the connection:
ansible-playbook playbook.yml -i hosts -u sammy -K
5. Server Role (LAMP Installation)
- Add the following in roles/server/tasks/main.yml:
- name: Update apt cache
apt: update_cache=yes cache_valid_time=3600
sudo: yes
- name: Install required software
apt: name={{ item }} state=present
with_items:
- apache2
- mysql-server
- php5
- php5-mysql
- libapache2-mod-php5
- php5-mcrypt
- python-mysqldb
sudo: yes
- This role installs Apache, MySQL and PHP.
6. PHP Role
- Edit the roles/php/tasks/main.yml file:
- name: Install PHP extensions
apt: name={{ item }} state=present
with_items:
- php5-gd
- libssh2-php
sudo: yes
- Necessary PHP plugins for WordPress are installed.
7. MySQL Role
- Define default variables:
wp_mysql_db: wordpress
wp_mysql_user: wordpress
wp_mysql_password: guclu_sifre
- Add database tasks:
- name: Create database
mysql_db: name={{ wp_mysql_db }} state=present
- name: Create user
mysql_user:
name={{ wp_mysql_user }}
password={{ wp_mysql_password }}
priv=*.*:ALL
- Database for WordPress is created automatically.
8. WordPress Role
- Download and configure WordPress:
- 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
sudo: yes
- WordPress files are downloaded to the server.
Add database information:
- name: Configure wp-config
lineinfile:
dest: /var/www/wordpress/wp-config.php
regexp: "{{ item.regexp }}"
line: "{{ item.line }}"
with_items:
- { regexp: "DB_NAME", line: "define('DB_NAME', '{{ wp_mysql_db }}');" }
- { regexp: "DB_USER", line: "define('DB_USER', '{{ wp_mysql_user }}');" }
- { regexp: "DB_PASSWORD", line: "define('DB_PASSWORD', '{{ wp_mysql_password }}');" }
9. Running the Setup
- Start all configuration:
ansible-playbook playbook.yml -i hosts -u sammy -K
- WordPress is installed with a single command.
- Open the following address from the browser:
http://sunucu_ip_adresi
Frequently Asked Questions (FAQ)
1. Why should Ansible be preferred? Provides fast, repeatable and error-free installation.
2. Can I install on more than one server? Yes. It is enough to add IP to the hosts file.
3. Is it safe to write passwords in plain text? No. Ansible Vault is recommended in production.
4. Can Nginx be used instead of Apache? Yes. Roles can be updated accordingly.
Result
WordPress can now be installed with a single command. Scalable and consistent infrastructure is provided with Ansible.
🚀 You can try automation immediately with Ansible supported servers on GenixNode.

