From a77c293d0ed35b8d1140f8f56b2e16294c800172 Mon Sep 17 00:00:00 2001 From: Artur Moreira Date: Sat, 28 Mar 2026 01:15:51 +0000 Subject: [PATCH] feat(wordpress): nginx + PHP-FPM stacks; retire Apache images (BIT-578) - Use wordpress:6-php8.3-fpm with nginx:alpine sidecars; keep host ports 8080/8081 - Add shared wp_html / wp_andebol_html volumes so nginx can serve the same docroot as FPM - Nginx: try_files permalinks, proxy headers, HTTP_AUTHORIZATION for App Passwords - Update deploy scripts to force-recreate nginx with wordpress when public URL is fixed - Refresh README and deployment docs Co-Authored-By: Paperclip Made-with: Cursor --- deploy/scripts/deploy.sh | 4 +- deploy/scripts/webhook-deploy.sh | 4 +- docs/architecture.md | 2 +- docs/deployment.md | 8 ++-- wordpress/README.md | 13 ++++-- wordpress/docker-compose.yml | 52 +++++++++++++++++++---- wordpress/nginx/wordpress-andebol.conf | 56 +++++++++++++++++++++++++ wordpress/nginx/wordpress.conf | 57 ++++++++++++++++++++++++++ 8 files changed, 175 insertions(+), 21 deletions(-) create mode 100644 wordpress/nginx/wordpress-andebol.conf create mode 100644 wordpress/nginx/wordpress.conf diff --git a/deploy/scripts/deploy.sh b/deploy/scripts/deploy.sh index 2d9c7d8..72b9913 100755 --- a/deploy/scripts/deploy.sh +++ b/deploy/scripts/deploy.sh @@ -49,8 +49,8 @@ _wp_pub=$? set -e docker compose up -d --remove-orphans if [ "$_wp_pub" -eq 2 ]; then - log "WORDPRESS_PUBLIC_URL was corrected — recreating wordpress container..." - docker compose up -d --force-recreate wordpress + log "WORDPRESS_PUBLIC_URL was corrected — recreating wordpress + nginx containers..." + docker compose up -d --force-recreate wordpress nginx fi bash "$APP_DIR/deploy/scripts/wordpress-sync-options-url.sh" || log "wordpress-sync-options-url skipped (db not ready or still local URL)" diff --git a/deploy/scripts/webhook-deploy.sh b/deploy/scripts/webhook-deploy.sh index a068c1b..df394b5 100755 --- a/deploy/scripts/webhook-deploy.sh +++ b/deploy/scripts/webhook-deploy.sh @@ -40,8 +40,8 @@ bash "$APP_DIR/deploy/scripts/wordpress-ensure-public-url.sh" _wp_pub=$? set -e if [ "$_wp_pub" -eq 2 ]; then - log "WORDPRESS_PUBLIC_URL updated — recreating wordpress container..." - (cd "$APP_DIR/wordpress" && docker compose up -d --force-recreate wordpress) || log "wordpress recreate failed (compose may be down)" + log "WORDPRESS_PUBLIC_URL updated — recreating wordpress + nginx containers..." + (cd "$APP_DIR/wordpress" && docker compose up -d --force-recreate wordpress nginx) || log "wordpress recreate failed (compose may be down)" fi # Rebuild bridge if changed diff --git a/docs/architecture.md b/docs/architecture.md index 40265d1..f754b8e 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -100,7 +100,7 @@ A scalable, multi-domain news platform designed for high traffic. Built on a hea └─────────────────────────┬───────────────────────────────────────┘ │ HTTP ┌─────────────────────────▼───────────────────────────────────────┐ -│ WordPress (headless, :8080) │ +│ WordPress (headless, nginx :8080 → PHP-FPM) │ │ │ │ REST API /wp-json/wp/v2/ │ │ posts, categories, tags, media, users │ diff --git a/docs/deployment.md b/docs/deployment.md index 3adfe9f..62ece33 100644 --- a/docs/deployment.md +++ b/docs/deployment.md @@ -43,7 +43,7 @@ LXC: news-project-test ├── redis (host or Docker) :6379 │ └── Docker Compose - ├── wordpress :8080 + ├── nginx (compose) :8080 → wordpress PHP-FPM └── mariadb (db) :3306 (internal only) ``` @@ -108,15 +108,15 @@ A stack real está versionada em **`wordpress/docker-compose.yml`** (não duplic | Item | Valor actual | |------|----------------| | Database | **MariaDB 11** (`mariadb:11`), serviço `db`, porta 3306 só na rede Compose | -| WordPress | `wordpress:6-php8.3-apache`, `WORDPRESS_DB_HOST: db:3306` | +| WordPress | `wordpress:6-php8.3-fpm` + `nginx:alpine` sidecar per site; `WORDPRESS_DB_HOST: db:3306` (andebol: `db-andebol`) | | URL pública | Preferir `WORDPRESS_PUBLIC_URL=https://newsproject.bitreport.pt` no `wordpress/.env`. Se faltar, o `wp-config` infere `https://` + `Host` quando o pedido chega **via Nginx do CT** e traz qualquer *hint* de proxy (`X-Forwarded-Proto`, `X-Forwarded-For`, ou `X-Real-IP`); `X-Forwarded-Proto` deve reflectir o HTTPS do NPM — ver `deploy/nginx/newsproject.bitreport.pt.conf`. | | Arranque | `depends_on: db` com `condition: service_healthy` | | Volumes | `db_data` (MySQL), `wp_content` (uploads/plugins) | -| Healthchecks | `db`: script oficial `--innodb_initialized`; `wordpress`: `curl` ao REST `/wp-json/wp/v2/` | +| Healthchecks | `db` / `db-andebol`: script oficial `--innodb_initialized`; FPM: socket TCP :9000; `nginx` / `nginx-andebol`: `wget` ao REST `/wp-json/wp/v2/` | Operação e recuperação de **«Error establishing a database connection»**: `wordpress/README.md` (secção homónima). No CT, após `cd /opt/news-platform/wordpress`: `bash scripts/verify-db-connection.sh` confirma em cadeia `ps` + healthcheck do MariaDB + ligação desde o contentor `wordpress`. -**Automação LXC (`APP_DIR=/opt/news-platform` apenas):** `deploy/scripts/wordpress-ensure-public-url.sh` corrige `WORDPRESS_PUBLIC_URL` no `wordpress/.env` para `https://newsproject.bitreport.pt` quando ainda está vazio ou em `http://localhost:8080`. `deploy/scripts/wordpress-sync-options-url.sh` actualiza `siteurl`/`home` na tabela `wp_*options` para o mesmo URL (resolve redirects para `localhost` mesmo após instalação antiga). São invocados por `deploy/scripts/deploy.sh` e `deploy/scripts/webhook-deploy.sh`; o primeiro devolve código de saída `2` quando alterou o `.env` para o *caller* fazer `docker compose up -d --force-recreate wordpress`. +**Automação LXC (`APP_DIR=/opt/news-platform` apenas):** `deploy/scripts/wordpress-ensure-public-url.sh` corrige `WORDPRESS_PUBLIC_URL` no `wordpress/.env` para `https://newsproject.bitreport.pt` quando ainda está vazio ou em `http://localhost:8080`. `deploy/scripts/wordpress-sync-options-url.sh` actualiza `siteurl`/`home` na tabela `wp_*options` para o mesmo URL (resolve redirects para `localhost` mesmo após instalação antiga). São invocados por `deploy/scripts/deploy.sh` e `deploy/scripts/webhook-deploy.sh`; o primeiro devolve código de saída `2` quando alterou o `.env` para o *caller* fazer `docker compose up -d --force-recreate wordpress nginx`. --- diff --git a/wordpress/README.md b/wordpress/README.md index 36762c2..310065c 100644 --- a/wordpress/README.md +++ b/wordpress/README.md @@ -6,8 +6,13 @@ Headless WordPress instance serving the News Platform via the REST API. The WP f | Service | Port | Notes | |---------|------|-------| -| WordPress | 8080 | Apache + PHP 8.3; REST API only | -| MariaDB 11 | 3306 | Internal only — not exposed to host | +| `nginx` | **8080** → 80 | Public HTTP edge; proxies to PHP-FPM | +| `wordpress` | (internal) | `wordpress:6-php8.3-fpm` — PHP-FPM only | +| `nginx-andebol` | **8081** → 80 | Andebol WordPress edge | +| `wordpress-andebol` | (internal) | Second FPM stack | +| MariaDB 11 | 3306 | Internal only — not exposed to host (`db`, `db-andebol`) | + +Nginx configs live under `wordpress/nginx/` (pretty permalinks + `HTTP_AUTHORIZATION` for Application Passwords). Host ports **8080** / **8081** are unchanged so CT Nginx and NPM upstreams stay the same. ## Quick Start (on CT 222) @@ -89,8 +94,8 @@ Se o browser mostra a página genérica do WordPress com **Error establishing a No LXC (`/opt/news-platform/wordpress` ou caminho equivalente): -1. `docker compose ps` — `db` tem de estar **Up (healthy)** e `wordpress` **Up**. -2. `docker compose logs db --tail 80` e `docker compose logs wordpress --tail 80`. +1. `docker compose ps` — `db` **Up (healthy)**, `wordpress` **Up (healthy)**, `nginx` **Up**; andebol stack: `db-andebol`, `wordpress-andebol`, `nginx-andebol`. +2. `docker compose logs db --tail 80` e `docker compose logs wordpress nginx --tail 80`. 3. Confirmar que `.env` no host tem `MYSQL_PASSWORD` / `MYSQL_ROOT_PASSWORD` alinhados com o que a BD foi inicializada (se mudaste a password depois do primeiro `up`, pode ser preciso recriar o volume ou alinhar credenciais). 4. Arranque: `docker compose up -d` e esperar o healthcheck do `db` antes do WordPress aceitar ligações. 5. Diagnóstico rápido à BD (sem credenciais no comando — usa o script da imagem): `docker compose exec db healthcheck.sh --connect --innodb_initialized`; deve sair `0`. Se falhar, rever logs do passo 2 e espaço em disco no CT (`df -h`). diff --git a/wordpress/docker-compose.yml b/wordpress/docker-compose.yml index d8728c7..ab51427 100644 --- a/wordpress/docker-compose.yml +++ b/wordpress/docker-compose.yml @@ -17,13 +17,11 @@ services: start_period: 90s wordpress: - image: wordpress:6-php8.3-apache + image: wordpress:6-php8.3-fpm restart: unless-stopped depends_on: db: condition: service_healthy - ports: - - "8080:80" environment: WORDPRESS_DB_HOST: db:3306 WORDPRESS_DB_NAME: wordpress @@ -91,10 +89,29 @@ services: } } volumes: + - wp_html:/var/www/html - 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}"] + test: ["CMD-SHELL", "php -r \"exit(@fsockopen('127.0.0.1',9000)?0:1);\""] + interval: 10s + timeout: 5s + retries: 8 + start_period: 90s + + nginx: + image: nginx:alpine + restart: unless-stopped + depends_on: + wordpress: + condition: service_healthy + ports: + - "8080:80" + volumes: + - wp_html:/var/www/html:ro + - ./nginx/wordpress.conf:/etc/nginx/conf.d/default.conf:ro + healthcheck: + test: ["CMD", "wget", "-q", "-O", "/dev/null", "http://127.0.0.1/wp-json/wp/v2/"] interval: 30s timeout: 10s retries: 5 @@ -118,13 +135,11 @@ services: start_period: 90s wordpress-andebol: - image: wordpress:6-php8.3-apache + image: wordpress:6-php8.3-fpm restart: unless-stopped depends_on: db-andebol: condition: service_healthy - ports: - - "8081:80" environment: WORDPRESS_DB_HOST: db-andebol:3306 WORDPRESS_DB_NAME: wordpress_andebol @@ -181,10 +196,29 @@ services: } } volumes: + - wp_andebol_html:/var/www/html - wp_andebol_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}"] + test: ["CMD-SHELL", "php -r \"exit(@fsockopen('127.0.0.1',9000)?0:1);\""] + interval: 10s + timeout: 5s + retries: 8 + start_period: 90s + + nginx-andebol: + image: nginx:alpine + restart: unless-stopped + depends_on: + wordpress-andebol: + condition: service_healthy + ports: + - "8081:80" + volumes: + - wp_andebol_html:/var/www/html:ro + - ./nginx/wordpress-andebol.conf:/etc/nginx/conf.d/default.conf:ro + healthcheck: + test: ["CMD", "wget", "-q", "-O", "/dev/null", "http://127.0.0.1/wp-json/wp/v2/"] interval: 30s timeout: 10s retries: 5 @@ -192,6 +226,8 @@ services: volumes: db_data: + wp_html: wp_content: db_andebol_data: + wp_andebol_html: wp_andebol_content: diff --git a/wordpress/nginx/wordpress-andebol.conf b/wordpress/nginx/wordpress-andebol.conf new file mode 100644 index 0000000..218bf4c --- /dev/null +++ b/wordpress/nginx/wordpress-andebol.conf @@ -0,0 +1,56 @@ +# Nginx front for WordPress PHP-FPM (andebol.net instance). Published as host :8081 → container :80. + +server { + listen 80; + listen [::]:80; + server_name _; + root /var/www/html; + index index.php; + + client_max_body_size 64M; + + add_header X-Content-Type-Options "nosniff" always; + add_header X-Frame-Options "SAMEORIGIN" always; + add_header Referrer-Policy "strict-origin-when-cross-origin" always; + + location / { + try_files $uri $uri/ /index.php?$args; + } + + location ~ \.php$ { + try_files $uri =404; + fastcgi_split_path_info ^(.+\.php)(/.+)$; + fastcgi_pass wordpress-andebol:9000; + fastcgi_index index.php; + include fastcgi_params; + fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; + fastcgi_param PATH_INFO $fastcgi_path_info; + fastcgi_param HTTP_AUTHORIZATION $http_authorization; + fastcgi_param HTTP_X_FORWARDED_PROTO $http_x_forwarded_proto; + fastcgi_param HTTP_X_FORWARDED_FOR $http_x_forwarded_for; + fastcgi_param HTTP_X_REAL_IP $http_x_real_ip; + fastcgi_param HTTP_X_FORWARDED_HOST $http_x_forwarded_host; + fastcgi_buffer_size 128k; + fastcgi_buffers 4 256k; + fastcgi_busy_buffers_size 256k; + } + + location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$ { + expires max; + log_not_found off; + } + + location = /favicon.ico { + log_not_found off; + access_log off; + } + + location = /robots.txt { + log_not_found off; + access_log off; + } + + location ~ /\. { + deny all; + } +} diff --git a/wordpress/nginx/wordpress.conf b/wordpress/nginx/wordpress.conf new file mode 100644 index 0000000..e00b2c0 --- /dev/null +++ b/wordpress/nginx/wordpress.conf @@ -0,0 +1,57 @@ +# Nginx front for WordPress PHP-FPM (main site). Published as host :8080 → container :80. +# Replaces Apache mod_rewrite (.htaccess) with try_files + fastcgi. + +server { + listen 80; + listen [::]:80; + server_name _; + root /var/www/html; + index index.php; + + client_max_body_size 64M; + + add_header X-Content-Type-Options "nosniff" always; + add_header X-Frame-Options "SAMEORIGIN" always; + add_header Referrer-Policy "strict-origin-when-cross-origin" always; + + location / { + try_files $uri $uri/ /index.php?$args; + } + + location ~ \.php$ { + try_files $uri =404; + fastcgi_split_path_info ^(.+\.php)(/.+)$; + fastcgi_pass wordpress:9000; + fastcgi_index index.php; + include fastcgi_params; + fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; + fastcgi_param PATH_INFO $fastcgi_path_info; + fastcgi_param HTTP_AUTHORIZATION $http_authorization; + fastcgi_param HTTP_X_FORWARDED_PROTO $http_x_forwarded_proto; + fastcgi_param HTTP_X_FORWARDED_FOR $http_x_forwarded_for; + fastcgi_param HTTP_X_REAL_IP $http_x_real_ip; + fastcgi_param HTTP_X_FORWARDED_HOST $http_x_forwarded_host; + fastcgi_buffer_size 128k; + fastcgi_buffers 4 256k; + fastcgi_busy_buffers_size 256k; + } + + location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$ { + expires max; + log_not_found off; + } + + location = /favicon.ico { + log_not_found off; + access_log off; + } + + location = /robots.txt { + log_not_found off; + access_log off; + } + + location ~ /\. { + deny all; + } +}