Automate WordPress Installation on Ubuntu with Ansible
Use Ansible to automate a full WordPress installation on Ubuntu, including Apache, MySQL, PHP, and WordPress configuration.
Prerequisites
- Ansible installed on the control node
- Target Ubuntu server accessible via SSH
- Python installed on the target host
Step 1 — Define the Inventory
# /etc/ansible/hosts
[wordpress]
192.168.1.100 ansible_user=ubuntu ansible_ssh_private_key_file=~/.ssh/id_rsa
Step 2 — Create the Playbook
# wordpress.yml
---
- hosts: wordpress
become: yes
vars:
db_name: wordpress
db_user: wp_user
db_pass: wp_pass
wp_dir: /var/www/html/wordpress
tasks:
- name: Install LAMP packages
apt:
name: [apache2, mysql-server, php, php-mysql, libapache2-mod-php, python3-pymysql]
state: latest
update_cache: yes
- name: Create MySQL database
mysql_db:
name: "{{ db_name }}"
state: present
login_unix_socket: /var/run/mysqld/mysqld.sock
- name: Create MySQL user
mysql_user:
name: "{{ db_user }}"
password: "{{ db_pass }}"
priv: "{{ db_name }}.*:ALL"
state: present
login_unix_socket: /var/run/mysqld/mysqld.sock
- name: Download WordPress
get_url:
url: https://wordpress.org/latest.tar.gz
dest: /tmp/wordpress.tar.gz
- name: Extract WordPress
unarchive:
src: /tmp/wordpress.tar.gz
dest: /var/www/html/
remote_src: yes
- name: Configure wp-config.php
template:
src: wp-config.j2
dest: "{{ wp_dir }}/wp-config.php"
- name: Set ownership
file:
path: "{{ wp_dir }}"
owner: www-data
group: www-data
recurse: yes
- name: Restart Apache
service:
name: apache2
state: restarted
Step 3 — Run the Playbook
ansible-playbook wordpress.yml