DEV Community

Cover image for πŸš€ Automating Apache + PHP + WordPress Setup on Ubuntu Using OpenTofu (IaC)
Latchu@DevOps
Latchu@DevOps

Posted on

πŸš€ Automating Apache + PHP + WordPress Setup on Ubuntu Using OpenTofu (IaC)

3

Infrastructure-as-Code (IaC) has become essential for DevOps engineers who want consistent, repeatable, and automated server provisioning. In this guide, I’ll show how I installed and configured Apache, PHP, WordPress, and custom Apache virtual host settings on my local Ubuntu machine using OpenTofu.

This whole deployment uses IaC β€” no more manual installations!


🧩 What Is OpenTofu ?

OpenTofu is an open-source fork of Terraform (after Terraform became closed-source under BSL).
It is a drop-in replacement, meaning your existing Terraform code works the same.

βœ” Why OpenTofu is powerful:

  • 100% open source (community-driven)
  • Full Terraform compatibility
  • No license restrictions
  • Fast updates and innovative features
  • Supported by Linux Foundation
  • Fully backward-compatible with Terraform modules and providers

Think of it as Terraform without limitations.


πŸ–₯️ 1. Install OpenTofu on Ubuntu

sudo apt-get update -y
sudo apt-get upgrade -y
sudo snap install --classic opentofu
Enter fullscreen mode Exit fullscreen mode

Check version:

tofu version
Enter fullscreen mode Exit fullscreen mode

πŸ“ 2. Create Your OpenTofu Project

mkdir tofu
cd tofu
nano main.tf
Enter fullscreen mode Exit fullscreen mode

Paste the following full automation code πŸ‘‡


πŸ’Ύ 3. Complete main.tf – Automate Apache, PHP & WordPress

terraform {
  required_providers {
    local = {
      source = "hashicorp/local"
    }
    null = {
      source = "hashicorp/null"
    }
  }
}

############################################
# INSTALL APACHE + PHP
############################################
resource "null_resource" "install_apache_php" {
  provisioner "local-exec" {
    command = <<-EOT
      sudo apt update -y

      # Install Apache + PHP
      sudo apt install net-tools zip unzip -y

      sudo apt install -y apache2 php php-mysql php-curl php-dom php-xml php-zip php-mbstring libapache2-mod-php

      # Fix Apache + PHP module setup
      sudo a2dismod mpm_event || true
      sudo a2enmod mpm_prefork || true
      sudo a2enmod php8.3 || true

      sudo systemctl restart apache2
    EOT
  }
}

############################################
# CREATE DIRECTORY + DOWNLOAD WORDPRESS
############################################
resource "null_resource" "wordpress_setup" {
  depends_on = [null_resource.install_apache_php]

  provisioner "local-exec" {
    command = <<-EOT
      sudo mkdir -p /var/www/html/myapp

      # Download and extract WordPress
      sudo wget https://wordpress.org/latest.zip -O /home/ubuntu/latest.zip
      sudo unzip /home/ubuntu/latest.zip -d /home/ubuntu/
      sudo mv /home/ubuntu/wordpress/* /var/www/html/html/myapp
      sudo chown -R www-data:www-data /var/www/html/myapp
      sudo chmod 755 -R /var/www/html/myapp
    EOT
  }
}

############################################
# UPDATE 000-default.conf
############################################
resource "null_resource" "apache_config" {
  depends_on = [null_resource.wordpress_setup]

  provisioner "local-exec" {
    command = <<-EOT
      sudo bash -c 'cat > /etc/apache2/sites-available/000-default.conf' <<'EOF'
<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html/myapp

    <Directory /var/www/html/myapp>
        Options FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog $${APACHE_LOG_DIR}/error.log
    CustomLog $${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
EOF
    EOT
  }
}


############################################
# UPDATE dir.conf TO USE index.php
############################################
resource "null_resource" "dir_conf" {
  depends_on = [null_resource.apache_config]

  provisioner "local-exec" {
    command = <<-EOT
      sudo bash -c 'cat > /etc/apache2/mods-enabled/dir.conf' <<'EOF'
<IfModule mod_dir.c>
    DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm
</IfModule>
EOF
    EOT
  }
}

############################################
# RESTART APACHE
############################################
resource "null_resource" "restart_apache" {
  depends_on = [
    null_resource.dir_conf
  ]

  provisioner "local-exec" {
    command = "sudo systemctl restart apache2"
  }
}
Enter fullscreen mode Exit fullscreen mode

▢️ 4. Initialize & Apply OpenTofu

tofu init
tofu validate
tofu plan
tofu apply -auto-approve
Enter fullscreen mode Exit fullscreen mode

1


πŸŽ‰ 5. What This Automation Does

Once you run tofu apply, your Ubuntu machine will:

βœ” Install Apache
βœ” Install PHP 8.3 + required extensions
βœ” Enable the correct PHP Apache module
βœ” Create /var/www/html/myapp
βœ” Download & extract WordPress
βœ” Fix file ownership
βœ” Update Apache virtual host (000-default.conf)
βœ” Set index.php priority in dir.conf
βœ” Restart Apache

Open in browser:

http://your-server-ip/
Enter fullscreen mode Exit fullscreen mode

Your WordPress installation page will appear instantly. πŸš€

2


🏁 Conclusion

OpenTofu makes it incredibly simple to automate server configuration β€” even on your local machine.
This setup is:

  • Reproducible
  • Idempotent
  • Script-free
  • Error-free
  • Fully automated

Instead of manually installing packages and editing config files, IaC handles everything.


🌟 Thanks for reading! If this post added value, a like ❀️, follow, or share would encourage me to keep creating more content.


β€” Latchu | Senior DevOps & Cloud Engineer

☁️ AWS | GCP | ☸️ Kubernetes | πŸ” Security | ⚑ Automation
πŸ“Œ Sharing hands-on guides, best practices & real-world cloud solutions

Top comments (0)