Skip to content
SumGuy's Ramblings
Go back

WordPress, Docker, NGINX, and MySQL via Ansible

In the realm of web development, ensuring the seamless deployment and management of web applications is crucial. This article provides a detailed guide on using Ansible to set up a Dockerized environment for running a WordPress site, backed by NGINX as a web server and MySQL as the database. By automating this setup, we can significantly enhance security, repeatability, and scalability.

Why Choose Docker, Ansible, NGINX, and MySQL?

Step 1: Prerequisites

To follow this guide, you’ll need:

Step 2: Prepare the Ansible Playbook

Create a directory for your Ansible playbook and related files:

mkdir wordpress-deployment
cd wordpress-deployment

Step 3: Define the Docker Compose File

Within the wordpress-deployment directory, create a docker-compose.yml file. This file defines the services, networks, and volumes that compose your WordPress environment.

services:
  db:
    image: mysql:5.7
    volumes:
      - db_data:/var/lib/mysql
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: password123
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wordpress
      MYSQL_PASSWORD: wordpress

  wordpress:
    depends_on:
      - db
    image: wordpress:latest
    ports:
      - "8000:80"
    restart: always
    environment:
      WORDPRESS_DB_HOST: db:3306
      WORDPRESS_DB_USER: wordpress
      WORDPRESS_DB_PASSWORD: wordpress
    volumes:
      - wordpress_data:/var/www/html

  nginx:
    image: nginx:latest
    ports:
      - "80:80"
    volumes:
      - ./nginx/default.conf:/etc/nginx/conf.d/default.conf
    depends_on:
      - wordpress

volumes:
  db_data:
  wordpress_data:

Step 4: NGINX Configuration

Create an NGINX configuration file default.conf in the nginx directory. Configure NGINX to act as a reverse proxy to the WordPress container.

server {
    listen 80;
    server_name localhost;

    location / {
        proxy_pass http://wordpress:80;
        proxy_set_header Host $host;
        proxy_setsecure_protocol $scheme;
        proxy_buffering off;
    }
}

make sure to change localhost to your domain

Step 5: Ansible Playbook to Deploy Everything

Now, create an Ansible playbook named deploy.yml:

- hosts: all
  become: true
  tasks:
  - name: Ensure Docker is installed
    apt:
      name: docker-ce
      state: latest

  - name: Clone project containing Docker Compose file
    git:
      repo: 'http://github.com/youraccount/wordpress-docker-compose.git'
      dest: /home/user/wordpress
      clone: yes
      update: yes

  - name: Run Docker Compose
    docker_compose:
      project_src: /home/user/wordpress

Why This Setup is Secure and Efficient?

This setup not only ensures a robust deployment strategy for WordPress sites but also simplifies management and scaling, making it an ideal choice for modern web applications.


Share this post on:

Previous Post
Ansible vs. Terraform: Cloud Infrastructure Management
Next Post
Prompts for Image Generation in Stable Diffusion