feat: headless WordPress Docker Compose setup (BIT-351)

- docker-compose.yml: WordPress 6/PHP8.3 + MariaDB 11, port 8080
- setup.sh: WP-CLI install, headless config, 5 categories, 10 sample articles
- php.ini: upload/memory limits for media-heavy news content
- .env.example: credential placeholders (values in vault)
- README.md: quick-start and REST API reference

Tested on CT 222 (news-project-test, 10.10.50.140):
- REST API /wp-json/wp/v2/ → 200 OK
- 5 categories: Football, Basketball, F1, Tennis, MMA
- 11 posts confirmed via REST API

Co-Authored-By: Paperclip <[email protected]>
Made-with: Cursor
This commit is contained in:
Artur MoreiraandPaperclip committed 2026-03-23 15:45:36 +00:00
1 parent d2d56a4751
commit 3cda2063dd
5 files changed
+249

No files matched your search

+9
View File
@@ -0,0 +1,9 @@
# WordPress Database credentials
# Copy to .env and fill in secure values — never commit .env to git
MYSQL_ROOT_PASSWORD=change_this_root_password
MYSQL_PASSWORD=change_this_wordpress_password
# WordPress admin (used by setup scripts only)
WP_ADMIN_USER=admin
WP_ADMIN_EMAIL=[email protected]
# WP_ADMIN_PASSWORD — set in vault; do not store here
+59
View File
@@ -0,0 +1,59 @@
# WordPress Headless CMS
Headless WordPress instance serving the News Platform via the REST API. The WP frontend theme is suppressed — all content is consumed by the Fastify bridge at port 3200.
## Services
| Service | Port | Notes |
|---------|------|-------|
| WordPress | 8080 | Apache + PHP 8.3; REST API only |
| MariaDB 11 | 3306 | Internal only — not exposed to host |
## Quick Start (on CT 222)
```bash
# 1. Clone repo and enter wordpress dir
cd /opt/news-platform/wordpress
# 2. Copy env and fill credentials
cp .env.example .env
# Edit .env — set MYSQL_ROOT_PASSWORD, MYSQL_PASSWORD, WP_ADMIN_PASSWORD
# 3. Start services
docker compose up -d
# 4. Wait for WordPress to be ready (~60s), then run setup
WP_ADMIN_PASSWORD=your_password docker compose exec wordpress bash /var/www/html/setup.sh
```
## REST API
After setup, the REST API is available at:
```
http://10.10.50.140:8080/wp-json/wp/v2/posts
http://10.10.50.140:8080/wp-json/wp/v2/categories
```
The Fastify bridge (port 3200) proxies these endpoints and adds Redis caching.
## Plugins Installed
| Plugin | Purpose |
|--------|---------|
| Advanced Custom Fields | Custom field support for article metadata |
| Yoast SEO | SEO meta exposed via REST API |
| JWT Auth for WP REST API | Token-based auth for protected endpoints |
| Disable XML-RPC | Security hardening |
## Sample Content
The setup script creates:
- 5 categories: Football, Basketball, Formula 1, Tennis, MMA
- 10+ sample articles (2 per category)
## Notes
- WordPress admin credentials are in the team vault — never commit to git
- Docker volumes persist at `db_data` (database) and `wp_content` (uploads/plugins)
- The setup script is idempotent (safe to re-run)
+51
View File
@@ -0,0 +1,51 @@
services:
db:
image: mariadb:11
restart: unless-stopped
environment:
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
volumes:
- db_data:/var/lib/mysql
healthcheck:
test: ["CMD", "healthcheck.sh", "--connect", "--innodb_initialized"]
interval: 10s
timeout: 5s
retries: 5
wordpress:
image: wordpress:6-php8.3-apache
restart: unless-stopped
depends_on:
db:
condition: service_healthy
ports:
- "8080:80"
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_NAME: wordpress
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: ${MYSQL_PASSWORD}
WORDPRESS_TABLE_PREFIX: wp_
# Disable WordPress cron (use system cron instead)
WORDPRESS_CONFIG_EXTRA: |
define('DISABLE_WP_CRON', true);
define('WP_DEBUG', false);
define('SCRIPT_DEBUG', false);
/* REST API — allow all origins for bridge access */
define('REST_API_ENABLED', true);
volumes:
- wp_content:/var/www/html/wp-content
- ./php.ini:/usr/local/etc/php/conf.d/custom.ini:ro
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost/wp-json/wp/v2/", "-o", "/dev/null", "-w", "%{http_code}"]
interval: 30s
timeout: 10s
retries: 5
start_period: 60s
volumes:
db_data:
wp_content:
+5
View File
@@ -0,0 +1,5 @@
; Custom PHP settings for WordPress headless mode
upload_max_filesize = 64M
post_max_size = 64M
memory_limit = 256M
max_execution_time = 60
+125
View File
@@ -0,0 +1,125 @@
#!/usr/bin/env bash
# WordPress headless setup script — runs inside the wordpress container via docker exec
# Usage: docker compose exec wordpress bash /scripts/setup.sh
# Requires: WP-CLI available inside container (downloaded if not present)
set -euo pipefail
WP_URL="${WP_URL:-http://localhost:8080}"
WP_TITLE="${WP_TITLE:-News Platform}"
WP_ADMIN_USER="${WP_ADMIN_USER:-admin}"
WP_ADMIN_EMAIL="${WP_ADMIN_EMAIL:-admin@bitreport.pt}"
WP_ADMIN_PASSWORD="${WP_ADMIN_PASSWORD:?Must set WP_ADMIN_PASSWORD}"
WPCLI=/usr/local/bin/wp
# Download WP-CLI if not present
if [ ! -f "$WPCLI" ]; then
echo "Downloading WP-CLI..."
curl -sS -o "$WPCLI" https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
chmod +x "$WPCLI"
fi
WP="$WPCLI --allow-root --path=/var/www/html"
# --- WordPress core install ---
if ! $WP core is-installed 2>/dev/null; then
echo "Installing WordPress core..."
$WP core install \
--url="$WP_URL" \
--title="$WP_TITLE" \
--admin_user="$WP_ADMIN_USER" \
--admin_email="$WP_ADMIN_EMAIL" \
--admin_password="$WP_ADMIN_PASSWORD" \
--skip-email
fi
# --- Headless: disable default theme frontend ---
echo "Configuring headless mode..."
$WP theme activate twentytwentyfour 2>/dev/null || true
$WP rewrite structure '/%postname%/' --hard
# Disable comments (not needed for headless news)
$WP option update default_comment_status closed
$WP option update default_ping_status closed
# Allow REST API for all (CORS is handled at Nginx/bridge level)
$WP option update blog_public 1
# --- Plugins ---
echo "Installing plugins..."
# Advanced Custom Fields (free version)
$WP plugin install advanced-custom-fields --activate
# Yoast SEO (REST meta for articles)
$WP plugin install wordpress-seo --activate
# JWT Authentication (for bridge if needed)
$WP plugin install jwt-authentication-for-wp-rest-api --activate
# Disable XML-RPC (not needed for headless)
$WP plugin install disable-xml-rpc --activate
# --- Sample content: Categories ---
echo "Creating categories..."
declare -A CATEGORIES=(
["football"]="Football"
["basketball"]="Basketball"
["formula1"]="Formula 1"
["tennis"]="Tennis"
["mma"]="MMA"
)
declare -A CAT_IDS
for slug in "${!CATEGORIES[@]}"; do
name="${CATEGORIES[$slug]}"
existing=$($WP term get category --by=slug "$slug" --field=term_id 2>/dev/null || echo "")
if [ -z "$existing" ]; then
id=$($WP term create category "$name" --slug="$slug" --porcelain)
else
id="$existing"
fi
CAT_IDS[$slug]="$id"
echo " Category: $name (id=$id)"
done
# Remove default Uncategorized if possible
$WP term delete category 1 2>/dev/null || true
# --- Sample content: Articles ---
echo "Creating sample articles..."
create_post() {
local title="$1"
local category_slug="$2"
local excerpt="$3"
local cat_id="${CAT_IDS[$category_slug]}"
$WP post create \
--post_title="$title" \
--post_excerpt="$excerpt" \
--post_content="<p>$excerpt Full article content to be replaced with real data.</p><p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris.</p>" \
--post_status=publish \
--post_category="$cat_id" \
--porcelain
}
create_post "Champions League: Real Madrid vs Manchester City Preview" "football" "A blockbuster Champions League clash as Real Madrid host Manchester City at the Bernabeu."
create_post "Premier League Title Race: Arsenal vs Liverpool Analysis" "football" "Both clubs are separated by just two points with six games remaining in the Premier League season."
create_post "NBA Playoffs: Golden State Warriors Eliminate Denver Nuggets" "basketball" "Steph Curry dropped 42 points as the Warriors completed a stunning comeback series win."
create_post "NBA Draft Prospects: Top 10 Players to Watch" "basketball" "A deep dive into the most promising prospects heading into this summer's NBA Draft."
create_post "Formula 1 Monaco Grand Prix: Starting Grid and Race Preview" "formula1" "The most glamorous race on the F1 calendar promises drama as teams split strategies."
create_post "Max Verstappen Extends Championship Lead with Dominant Victory" "formula1" "The reigning world champion was untouchable as he led from lights to flag."
create_post "Wimbledon Preview: Can Alcaraz Defend His Title?" "tennis" "Carlos Alcaraz arrives at SW19 as the heavy favourite after his dominant clay-court season."
create_post "US Open Draw Analysis: Djokovic and Swiatek in Same Half" "tennis" "The draw has set up a potential blockbuster showdown in the semi-finals."
create_post "UFC 300 Main Event: Jon Jones vs Stipe Miocic Full Preview" "mma" "The most anticipated heavyweight fight in years finally arrives at UFC 300."
create_post "Conor McGregor Announces Comeback Fight Date and Opponent" "mma" "The Notorious one returns to the octagon after a three-year absence."
echo ""
echo "=== Setup complete ==="
echo "WordPress URL: $WP_URL"
echo "Admin URL: $WP_URL/wp-admin/"
echo "REST API: $WP_URL/wp-json/wp/v2/"
echo "Categories: $WP_URL/wp-json/wp/v2/categories"
echo "Posts: $WP_URL/wp-json/wp/v2/posts"
echo ""
echo "Credentials are stored in the vault (not in git)."