Skip to the content.

WordPress Deployment on GCP

Automated, production-ready scripts and Ansible playbooks to deploy a high-performance, hardened WordPress stack on Google Cloud Platform — optimised for Ubuntu 24.04 LTS and Ubuntu 26.04 LTS.

Stack

Component Details
Web server Nginx (official nginx.org repo)
PHP 8.4 FPM (Ondrej Surý’s packages) — pool sizing tuned to CPU cores and RAM, OPcache enabled
Database MariaDB with root hardening and dedicated WP user
CMS WordPress (latest) installed via WP-CLI
Database UI phpMyAdmin (latest, auto-configured)
TLS Certbot / Let’s Encrypt with auto-renewal
Security Fail2Ban (SSH jail), Unattended Upgrades, security headers, XML-RPC mitigation
Swap Configurable swap file for low-memory VMs
Monitoring Google Cloud Ops Agent on GCP VMs — log collection only; agent metrics off to stay within the Cloud Monitoring free tier
Auto-updates Weekly WP core/plugin/theme update cron + OS unattended-upgrades; WP-Cron events run via a 5-min system cron (page-load spawning disabled)

Prerequisites

Quick Start (Shell Scripts)

SSH into your VM and run:

curl -fsSL https://raw.githubusercontent.com/renderbit-technologies/WordPress-GCP-VM-Setup/main/install.sh -o install.sh && sudo bash install.sh && sudo rm install.sh

The script runs in interactive mode by default, prompting for:

  1. Domain name
  2. www alias preference
  3. Database name and user
  4. Admin email (for Let’s Encrypt and WordPress)
  5. Fail2Ban enable/disable
  6. Confirmation to proceed

Non-Interactive / CI Mode

Set environment variables before running the script to skip prompts:

Variable Description Default
DOMAIN (Required) Domain to install WordPress for —
USE_WWW Enable www alias (y/n) y
WP_DB WordPress database name wpdb
WP_DB_USER WordPress database user wpuser
WP_DB_PASS WordPress database password Auto-generated
WP_ADMIN_PASS WordPress admin password Auto-generated
MYSQL_ROOT_PASS MySQL root password Auto-generated
LE_EMAIL Email for Let’s Encrypt & WP notices admin@$DOMAIN
ENABLE_FAIL2BAN Enable Fail2Ban (y/n) y
CONT Skip confirmation prompt (y/n) y in batch mode
SWAP_SIZE Swap file size (used by setup-swap.sh) 2G
ENABLE_OPS_AGENT Install the Ops Agent, metrics off (auto/y/n; auto = GCP VMs only) auto

Example:

export DOMAIN="example.com" USE_WWW="y" ENABLE_FAIL2BAN="y"
sudo bash install.sh

Ansible Playbook

An Ansible-based deployment is also available in ansible/, offering idempotent, repeatable provisioning with the same functionality as the shell scripts.

Roles: common → swap → wordpress → security → ops_agent

See the dedicated Ansible README for full usage, variables, Vagrant testing, and requirements.

What Gets Installed

WordPress Hardening

Essential Plugins

The following plugins are automatically installed (not activated — configure per your needs):

WP-Cron

DISABLE_WP_CRON is set in wp-config.php, so WordPress no longer spawns WP-Cron on page loads. Instead, /etc/cron.d/wp-cron runs wp cron event run --due-now every 5 minutes as www-data under a non-blocking flock, logging to syslog via logger -t wp-cron. This prevents a heavy scheduled task (e.g. a security-plugin scan) from holding a PHP-FPM worker hostage on a live request, and the lock stops a long-running task from stacking a new process on top of itself every 5 minutes.

Upgrading an Existing Server

Already-provisioned boxes don’t need a full reprovision to pick up the WP-Cron migration or the Sucuri removal — both scripts are idempotent, so re-running them against an existing box applies just the new pieces (the WP-Cron runner, DISABLE_WP_CRON) without touching the existing site, database, or credentials:

sudo bash setup-wp-nginx.sh

or, for the Ansible path:

ansible-playbook -i inventory.ini playbook.yml

Re-running with the same DOMAIN (and leaving credential prompts blank in interactive mode) reuses the existing /root/.wp-credentials rather than rotating passwords. An existing sucuri-scanner install from before this change isn’t removed automatically — deactivate/delete it manually if desired:

wp plugin deactivate sucuri-scanner && wp plugin delete sucuri-scanner

Nginx Configuration

Server Security

Monitoring

Repository Structure

.
├── install.sh                  # Entrypoint — downloads and runs the setup scripts
├── setup-swap.sh               # Swap file creation and sysctl tuning
├── setup-ops-agent.sh          # Google Cloud Ops Agent (logging only, metrics off)
├── setup-wp-nginx.sh           # Full WordPress + Nginx + MariaDB + phpMyAdmin setup
├── ansible/                    # Ansible playbook (alternative to shell scripts)
│   ├── playbook.yml            # Main playbook with vars_prompt for credentials
│   ├── inventory.ini           # Target host inventory
│   ├── ansible.cfg             # Ansible configuration
│   ├── Vagrantfile             # Local testing VM (Vagrant + VirtualBox)
│   └── roles/
│       ├── common/             # System packages and apt cache update
│       ├── swap/               # Swap file management
│       ├── wordpress/          # Nginx, PHP, MariaDB, WP-CLI, WordPress, phpMyAdmin
│       ├── security/           # Fail2Ban, Unattended Upgrades, file permissions
│       └── ops_agent/          # Google Cloud Ops Agent (logging only, metrics off)
├── tests/
│   ├── bash/                   # Vagrant-based test harness for shell scripts
│   │   ├── Vagrantfile         # Test VM with env-var driven provisioning
│   │   └── README.md           # Test harness documentation
│   └── vagrant/
│       └── configure-apt-mirror.sh   # Points test VMs at the Azure mirror (Canonical HTTPS fallback)
├── .github/
│   ├── dependabot.yml          # Dependabot config for GitHub Actions
│   └── workflows/
│       ├── shellcheck.yml      # ShellCheck linting on push/PR
│       ├── codacy.yml          # Codacy security scanning (scheduled + push/PR)
│       ├── bash-test.yml       # Vagrant-based bash script integration test
│       └── ansible-test.yml    # Vagrant-based Ansible playbook integration test
├── CONTRIBUTING.md             # Contribution guidelines and shell script styleguide
├── LICENSE                     # MIT License
└── .codacy.yml                 # Codacy engine configuration (Trivy, Shell)

CI/CD & Quality

Workflow Trigger Description
ShellCheck Push / PR to main Lints all .sh files with ShellCheck
Codacy Security Scan Push / PR to main + weekly schedule Runs Trivy security analysis and uploads SARIF results
Bash Scripts Test Push / PR to main (when *.sh or tests/bash/** change) Provisions a Vagrant VM with the shell scripts, verifies deployment, and tests idempotency
Ansible Playbook Test Push / PR to main (when ansible/** changes) Runs syntax check, provisions a Vagrant VM with the playbook, verifies deployment, and tests idempotency

Credentials

After installation, all generated credentials are saved to ~/.wp-credentials (mode 0600, owned by root). View them with:

sudo cat ~/.wp-credentials

The file contains: MySQL root password, WordPress DB credentials, WordPress admin credentials, phpMyAdmin URL, and PHP-FPM pool tuning parameters.

Troubleshooting

Contributing

Please read CONTRIBUTING.md for guidelines on reporting bugs, suggesting enhancements, submitting pull requests, and the shell script styleguide.

License

This project is licensed under the MIT License — see LICENSE for details.