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
- A GCP VM instance (or any Ubuntu server) running Ubuntu 24.04 LTS or Ubuntu 26.04 LTS. Other releases are rejected up front.
bashandcurlinstalled (present by default).- A valid domain name with DNS A record pointing to the VM’s external IP.
- GCP VPC firewall rules allowing TCP 80 and TCP 443 ingress.
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:
- Domain name
wwwalias preference- Database name and user
- Admin email (for Let’s Encrypt and WordPress)
- Fail2Ban enable/disable
- 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
wp-config.phpconstants:DISALLOW_FILE_EDIT,FS_METHOD = 'direct',FORCE_SSL_ADMIN,WP_AUTO_UPDATE_CORE = 'minor',DISABLE_WP_CRON = true- MU-plugin to disable XML-RPC pingback (DDoS mitigation)
- Default
adminuser removed and replaced with a custom admin account readme.htmlandlicense.txtremoved from webroot- File permissions hardened: directories
0775, files0664,wp-config.php0640
Essential Plugins
The following plugins are automatically installed (not activated — configure per your needs):
- Jetpack, Jetpack Protect, Jetpack Boost
- Akismet Anti-Spam
- AMP
- Wordfence Security
- WP Mail SMTP
- Cloudflare Flexible SSL
- Google Analytics for WordPress (MonsterInsights)
- UpdraftPlus Backup
- Better Search Replace
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
- Security headers snippet (
X-Frame-Options,X-Content-Type-Options,Referrer-Policy,X-XSS-Protection,Content-Security-Policy) - PHP execution denied in
uploads/andfiles/directories - Hidden files (
dotfiles) denied xmlrpc.php,wp-config.php,readme.html,license.txt,install.phpblocked- Static asset caching with
expires max - phpMyAdmin served at
/phpmyadmin
Server Security
- Fail2Ban — SSH jail with configurable ban time, find time, and max retries
- Unattended Upgrades — Automatic OS security patches
- Certbot — Automatic HTTPS with Let’s Encrypt and systemd renewal timer
- MariaDB — Anonymous users removed, test database dropped, root password set
Monitoring
- Google Cloud Ops Agent — installed on GCP VMs (detected via the metadata server) for log collection. Its metrics pipeline is turned off: Ops Agent metrics are billed per MiB beyond the billing account’s 150 MiB/month free tier (~120 MiB/month per VM with defaults). Compute Engine’s own VM metrics (CPU, disk and network I/O) stay available in Cloud Monitoring for free. Set
ENABLE_OPS_AGENT=n/enable_ops_agent: falseto skip it.
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
- Logs — Error output is displayed directly on
stdoutduring execution. Check console output for failures. - DNS — Ensure DNS A records are propagated before running the script. Certbot will fail otherwise.
- Firewall — Verify GCP VPC firewall allows TCP 80 and 443 ingress.
- Re-run safety — Both the shell scripts and Ansible playbook are designed to be idempotent; re-running is safe.
- Credentials file — If
~/.wp-credentialsexists from a prior run, the script reuses stored credentials.
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.