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
Check version:
tofu version
π 2. Create Your OpenTofu Project
mkdir tofu
cd tofu
nano main.tf
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"
}
}
βΆοΈ 4. Initialize & Apply OpenTofu
tofu init
tofu validate
tofu plan
tofu apply -auto-approve
π 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/
Your WordPress installation page will appear instantly. π
π 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)