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?

  • Docker simplifies deployment by containerizing applications, ensuring that they work uniformly across different environments.
  • Ansible automates software provisioning and configuration management, reducing the potential for human error.
  • NGINX serves as a high-performance web server and reverse proxy, offering better performance and resource utilization than traditional servers.
  • MySQL is a robust and scalable database management system, ideal for managing data-intensive applications like WordPress.

Step 1: Prerequisites

To follow this guide, you’ll need:

  • A Linux system (preferably Ubuntu)
  • Docker and Docker Compose installed
  • Ansible installed on your local machine

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.

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

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

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


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
  - name: Ensure Docker is installed
      name: docker-ce
      state: latest

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

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

Why This Setup is Secure and Efficient?

  1. Isolation: Docker provides an isolated environment for your applications, minimizing conflicts and security risks.
  2. Version Control: With Docker and Ansible, specific versions of services can be defined, ensuring consistency across environments.
  3. Automation: Ansible automates the entire process, reducing the likelihood of errors and misconfigurations.
  4. Scalability: Scaling services is as simple as updating the Docker Compose file and re-running the playbook.

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.

Similar Posts

Leave a Reply

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