Automate WordPress Setup with Ansible on Ubuntu VPS

If you’re new to Ansible and want to avoid setting up WordPress manually every time — this guide is for you.

We’ll go step by step:
From installing Ansible → to writing a playbook → to deploying a full WordPress website with NGINX, Apache, MariaDB, SSL, and a custom user.


Why Use Ansible?

Ansible lets you automate server setup so you don’t have to repeat the same commands again and again. One playbook = one-click deployment.


Step-by-Step Guide

Step 1: VPS Requirements

  • A fresh Ubuntu 22.04 VPS (e.g. from Contabo)
  • Root SSH access
  • Domain name pointed to the server IP (for SSL)

Step 2: Install Ansible (on your local PC or another server)

If you’re using Ubuntu:

sudo apt update<br>sudo apt install ansible -y

Step 3: Create Your Ansible Project

Make a project folder:

mkdir wordpress-ansible && cd wordpress-ansible

Create a file called hosts and add:

[webservers]
your_server_ip ansible_user=root

Replace your_server_ip with your VPS IP.


Step 4: Create the Playbook

Create a file named wordpress_setup.yml

Paste this [shortened version for beginners] playbook:

- name: Setup WordPress on Ubuntu VPS
hosts: webservers
become: true
vars:
site_user: sohail
domain: example.com
mysql_root_password: rootpass
db_name: wp_db
db_user: wp_user
db_user_password: wp_pass

tasks:
- name: Add system user
user:
name: "{{ site_user }}"
shell: /bin/bash
create_home: yes

- name: Install base packages
apt:
name:
- nginx
- apache2
- mariadb-server
- php
- php-mysql
- unzip
- curl
- certbot
- python3-certbot-nginx
update_cache: yes
state: present

- name: Start MariaDB and set root password
mysql_user:
name: root
password: "{{ mysql_root_password }}"
login_unix_socket: /var/run/mysqld/mysqld.sock
state: present

- name: Create WordPress DB and user
mysql_db:
name: "{{ db_name }}"
state: present

- name: Create DB user
mysql_user:
name: "{{ db_user }}"
password: "{{ db_user_password }}"
priv: "{{ db_name }}.*:ALL"
state: present

- name: Download WordPress
get_url:
url: https://wordpress.org/latest.zip
dest: /tmp/wordpress.zip

- name: Extract WordPress
unarchive:
src: /tmp/wordpress.zip
dest: "/var/www/{{ domain }}"
remote_src: yes
extra_opts: [--strip-components=1]

- name: Set permissions
file:
path: "/var/www/{{ domain }}"
owner: www-data
group: www-data
recurse: yes

- name: Setup Apache on port 8080
copy:
dest: "/etc/apache2/sites-available/{{ domain }}.conf"
content: |
<VirtualHost *:8080>
DocumentRoot /var/www/{{ domain }}
<Directory /var/www/{{ domain }}>
AllowOverride All
Require all granted
</Directory>
</VirtualHost>

- name: Enable site and modules
shell: |
a2enmod rewrite
a2ensite {{ domain }}
systemctl reload apache2

- name: Configure NGINX reverse proxy
copy:
dest: "/etc/nginx/sites-available/{{ domain }}"
content: |
server {
listen 80;
server_name {{ domain }};
location / {
proxy_pass http://127.0.0.1:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}

- name: Enable NGINX site
file:
src: "/etc/nginx/sites-available/{{ domain }}"
dest: "/etc/nginx/sites-enabled/{{ domain }}"
state: link

- name: Reload NGINX
shell: nginx -t && systemctl reload nginx

- name: Install SSL with Certbot
shell: certbot --nginx --non-interactive --agree-tos -m [email protected] -d {{ domain }}

Step 5: Run Your Playbook

ansible-playbook -i hosts wordpress_setup.yml

This will:

  • Install all software
  • Set up database
  • Deploy WordPress
  • Set up Apache on port 8080
  • Configure NGINX as a reverse proxy
  • Enable free SSL from Let’s Encrypt

Done!

You’ve just automated your first WordPress setup using Ansible — a real-world use case even beginners can understand.

Full-stack developer, tech enthusiast, and server-side tinkerer. I share short dev notes, server setups, code snippets, and practical guides for beginners and fellow developers. Follow my journey and learn something new with every post. 🔗 GitHub: @meetsohail

Leave a Comment

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