ServerHandbook live check: run log ------------------------------------------------------------------------------ Check scripts/live/checks/nginx-502-bad-gateway-ubuntu.mjs (sha256 dd595602f57c0b4c301be13cf4e74391e99b759e614968001dd0e42711e3465a) Ran at 2026-11-19T16:56:41Z (114 s) Result PASS, all 45 claims held Tested on System Ubuntu 24.04.5 LTS Kernel 6.8.0-147-generic Package nginx 1.24.0-2ubuntu7.20 Package php-fpm 2:8.3+93ubuntu2 Package php8.3-fpm 8.3.6-0ubuntu0.24.04.13 Package python3 3.12.3-0ubuntu2.1 Package iproute2 6.1.0-1ubuntu6.4 Package curl 8.5.0-2ubuntu10.16 Setup A fresh Ubuntu 24.04 server (a VM from Ubuntu’s cloud image) with Nginx and PHP-FPM from Ubuntu’s archive, the default site serving PHP through /run/php/php-fpm.sock and proxying /app/ to a small app on 127.0.0.1:8000, reached over SSH as the sudo user deploy. Each cause is created first, then found from the error log and fixed. Every version above was read from the server at run time. Passwords used on the throwaway test server are shown as [password]. How we test: https://serverhandbook.com/how-we-test ------------------------------------------------------------------------------ What the steps changed on the server (recorded before the first step and after the last) Packages added (0) Packages upgraded (0) Packages removed (0) Services enabled at boot (0) Services disabled (0) Listening sockets opened (0) Listening sockets closed (0) Files added in /etc (0) Files changed in /etc (1) /etc/nginx/sites-available/default Files removed from /etc (0) Disk used on / (includes logs written during the run): +0.1 MB ------------------------------------------------------------------------------ ------------------------------------------------------------------------------ Check-your-server script (2 items): https://cdn.serverhandbook.com/check/nginx-502-bad-gateway-ubuntu/ubuntu-24.04.5-20261119t165641z.sh SHA-256 9080a8e97304070c9acac12a9c0a7db366253f601d1be19e3334dcf24f08f14c ------------------------------------------------------------------------------ On the stock server, before the first step: FAIL a PHP-FPM service is running FAIL every PHP-FPM socket named in the enabled Nginx sites exists After the last step: PASS a PHP-FPM service is running PASS every PHP-FPM socket named in the enabled Nginx sites exists ------------------------------------------------------------------------------ Claims (45), each with what the run saw ------------------------------------------------------------------------------ 1. ok a bare Ubuntu 24.04 server box (web-server) starts 2. ok (setup) nginx and PHP-FPM 8.3 are installed and running | 8.3 3. ok (setup) the site works: test.php runs PHP and /app/ reaches the app on 127.0.0.1:8000 | PHP 8.3.6 OK | ok 4. ok (setup) /run/php/php-fpm.sock points to /run/php/php8.3-fpm.sock (Debian's alternatives link) | /run/php/php8.3-fpm.sock 5. ok curl -I: HTTP/1.1 502 Bad Gateway | HTTP/1.1 502 Bad Gateway 6. ok error.log: connect() to unix:/run/php/php-fpm.sock failed (2: No such file or directory) while connecting to upstream | 2026/11/19 16:58:18 [crit] 8760#8760: *5 connect() to unix:/run/php/php-fpm.sock failed (2: No such file or directory) while connecting to upstream, client: ::1, server: _, request: "HEAD /test.php HTTP/1.1", upstream: "fastcgi://unix:/run/php/php-fpm.sock:", host: "localhost" 7. ok systemctl list-units: php8.3-fpm.service is inactive (dead) | UNIT LOAD ACTIVE SUB DESCRIPTION | php8.3-fpm.service loaded inactive dead The PHP 8.3 FastCGI Process Manager | | Legend: LOAD → Reflects whether the unit definition was properly loaded. | ACTIVE → The high-level unit activation state, i.e. generalization of SUB. | SUB → The low-level unit activation state, values depend on unit type. | | 1 loaded units listed. | To show all installed unit files use 'systemctl list-unit-files'. 8. ok ls -l /run/php/: no socket while PHP-FPM is stopped | total 0 | lrwxrwxrwx 1 root root 30 Nov 19 16:58 php-fpm.sock -> /etc/alternatives/php-fpm.sock 9. ok enable --now starts php8.3-fpm (it was already enabled: no output, or a "Synchronizing" line) | Synchronizing state of php8.3-fpm.service with SysV service script with /usr/lib/systemd/systemd-sysv-install. | Executing: /usr/lib/systemd/systemd-sysv-install enable php8.3-fpm 10. ok curl: "PHP 8.3.x OK": PHP answers again | PHP 8.3.6 OK 11. ok the wrong socket path: HTTP/1.1 502 Bad Gateway | HTTP/1.1 502 Bad Gateway 12. ok error.log: connect() to unix:/run/php/php7.4-fpm.sock failed (2: No such file or directory) | 2026/11/19 16:58:21 [crit] 9846#9846: *9 connect() to unix:/run/php/php7.4-fpm.sock failed (2: No such file or directory) while connecting to upstream, client: ::1, server: _, request: "HEAD /test.php HTTP/1.1", upstream: "fastcgi://unix:/run/php/php7.4-fpm.sock:", host: "localhost" 13. ok ls -l /run/php/: the real socket is php8.3-fpm.sock, and php-fpm.sock links to it | total 4 | lrwxrwxrwx 1 root root 30 Nov 19 16:58 php-fpm.sock -> /etc/alternatives/php-fpm.sock | -rw-r--r-- 1 root root 4 Nov 19 16:58 php8.3-fpm.pid | srw-rw---- 1 www-data www-data 0 Nov 19 16:58 php8.3-fpm.sock 14. ok grep finds the line: fastcgi_pass unix:/run/php/php7.4-fpm.sock; | /etc/nginx/sites-enabled/default:14: fastcgi_pass unix:/run/php/php7.4-fpm.sock; 15. ok sed changes it to the version-free /run/php/php-fpm.sock 16. ok nginx -t: syntax is ok, test is successful | nginx: the configuration file /etc/nginx/nginx.conf syntax is ok | nginx: configuration file /etc/nginx/nginx.conf test is successful 17. ok curl: PHP answers again | PHP 8.3.6 OK 18. ok wrong socket owner: HTTP/1.1 502 Bad Gateway | HTTP/1.1 502 Bad Gateway 19. ok error.log: connect() to unix:/run/php/php-fpm.sock failed (13: Permission denied) | 2026/11/19 16:58:24 [crit] 10235#10235: *13 connect() to unix:/run/php/php-fpm.sock failed (13: Permission denied) while connecting to upstream, client: ::1, server: _, request: "HEAD /test.php HTTP/1.1", upstream: "fastcgi://unix:/run/php/php-fpm.sock:", host: "localhost" 20. ok ls -l: the socket is srw-rw---- nobody nogroup, so www-data (nginx) cannot open it | srw-rw---- 1 nobody nogroup 0 Nov 19 16:58 /run/php/php8.3-fpm.sock 21. ok the pool file says listen.owner = nobody, listen.group = nogroup | 53:listen.owner = nobody | 54:listen.group = nogroup 22. ok sed puts www-data back 23. ok php-fpm8.3 -t: configuration file … test is successful | [19-Nov-2026 16:58:25] NOTICE: configuration file /etc/php/8.3/fpm/php-fpm.conf test is successful 24. ok ls -l: srw-rw---- www-data www-data | srw-rw---- 1 www-data www-data 0 Nov 19 16:58 /run/php/php8.3-fpm.sock 25. ok curl: PHP answers again | PHP 8.3.6 OK 26. ok the app is down: HTTP/1.1 502 Bad Gateway | HTTP/1.1 502 Bad Gateway 27. ok error.log: connect() failed (111: Connection refused) while connecting to upstream … upstream: "http://127.0.0.1:8000/ok.txt" | 2026/11/19 16:58:26 [error] 10234#10234: *17 connect() failed (111: Connection refused) while connecting to upstream, client: ::1, server: _, request: "HEAD /app/ok.txt HTTP/1.1", upstream: "http://127.0.0.1:8000/ok.txt", host: "localhost" 28. ok ss: nothing listens on port 8000 (only the header line) | State Recv-Q Send-Q Local Address:Port Peer Address:Port Process 29. ok systemctl start demo-app 30. ok ss: python3 listens on 127.0.0.1:8000 | State Recv-Q Send-Q Local Address:Port Peer Address:Port Process | LISTEN 0 5 127.0.0.1:8000 0.0.0.0:* users:(("python3",pid=11215,fd=3)) 31. ok curl /app/ok.txt: "ok" from the app | ok 32. ok an 8 KB header: HTTP/1.1 502 Bad Gateway | HTTP/1.1 502 Bad Gateway 33. ok error.log: upstream sent too big header while reading response header from upstream | 2026/11/19 16:58:28 [error] 10234#10234: *21 upstream sent too big header while reading response header from upstream, client: ::1, server: _, request: "HEAD /big.php HTTP/1.1", upstream: "fastcgi://unix:/run/php/php-fpm.sock:", host: "localhost" 34. ok sed adds fastcgi_buffer_size 16k and fastcgi_buffers 16 16k under fastcgi_pass 35. ok nginx -t is successful with the bigger buffers | nginx: the configuration file /etc/nginx/nginx.conf syntax is ok | nginx: configuration file /etc/nginx/nginx.conf test is successful 36. ok curl big.php: "big header": the page loads | big header 37. ok a worker killed mid-request: HTTP/1.1 502 Bad Gateway | HTTP/1.1 502 Bad Gateway 38. ok error.log: the upstream closed the connection while reading the response header | 2026/11/19 16:58:30 [error] 11734#11734: *25 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: ::1, server: _, request: "HEAD /crash.php HTTP/1.1", upstream: "fastcgi://unix:/run/php/php-fpm.sock:", host: "localhost" 39. ok /var/log/php8.3-fpm.log: WARNING: [pool www] child … exited on signal 9 (SIGKILL) | [19-Nov-2026 16:58:30] WARNING: [pool www] child 10764 exited on signal 9 (SIGKILL) after 4.895434 seconds from start | [19-Nov-2026 16:58:30] NOTICE: [pool www] child 11862 started 40. ok the other pages still work: PHP-FPM started a new worker | PHP 8.3.6 OK 41. ok (a PHP-FPM service is running at the end of the steps) | 1 42. ok (every socket the enabled sites name exists at the end of the steps) | 0 43. ok (test.php answers 200 at the end of the steps) | 200 44. ok (tested on) the box reports its OS, kernel and the version of 6 package(s) | PRETTY_NAME="Ubuntu 24.04.5 LTS" | VIRT=none | KERNEL=6.8.0-147-generic | PKG curl 8.5.0-2ubuntu10.16 | PKG iproute2 6.1.0-1ubuntu6.4 | PKG nginx 1.24.0-2ubuntu7.20 | PKG php-fpm 2:8.3+93ubuntu2 | PKG php8.3-fpm 8.3.6-0ubuntu0.24.04.13 | PKG python3 3.12.3-0ubuntu2.1 45. ok undo: the two test pages are removed ------------------------------------------------------------------------------ Commands and what they printed (37) ------------------------------------------------------------------------------ $ curl -sI http://localhost/test.php | head -n 1 HTTP/1.1 502 Bad Gateway $ sudo tail -n 1 /var/log/nginx/error.log 2026/11/19 16:58:18 [crit] 8760#8760: *5 connect() to unix:/run/php/php-fpm.sock failed (2: No such file or directory) while connecting to upstream, client: ::1, server: _, request: "HEAD /test.php HTTP/1.1", upstream: "fastcgi://unix:/run/php/php-fpm.sock:", host: "localhost" $ systemctl list-units --all --no-pager 'php*-fpm.service' UNIT LOAD ACTIVE SUB DESCRIPTION php8.3-fpm.service loaded inactive dead The PHP 8.3 FastCGI Process Manager Legend: LOAD → Reflects whether the unit definition was properly loaded. ACTIVE → The high-level unit activation state, i.e. generalization of SUB. SUB → The low-level unit activation state, values depend on unit type. 1 loaded units listed. To show all installed unit files use 'systemctl list-unit-files'. $ ls -l /run/php/ total 0 lrwxrwxrwx 1 root root 30 Nov 19 16:58 php-fpm.sock -> /etc/alternatives/php-fpm.sock $ sudo systemctl enable --now php8.3-fpm Synchronizing state of php8.3-fpm.service with SysV service script with /usr/lib/systemd/systemd-sysv-install. Executing: /usr/lib/systemd/systemd-sysv-install enable php8.3-fpm $ curl -s http://localhost/test.php PHP 8.3.6 OK $ curl -sI http://localhost/test.php | head -n 1 HTTP/1.1 502 Bad Gateway $ sudo tail -n 1 /var/log/nginx/error.log 2026/11/19 16:58:21 [crit] 9846#9846: *9 connect() to unix:/run/php/php7.4-fpm.sock failed (2: No such file or directory) while connecting to upstream, client: ::1, server: _, request: "HEAD /test.php HTTP/1.1", upstream: "fastcgi://unix:/run/php/php7.4-fpm.sock:", host: "localhost" $ ls -l /run/php/ total 4 lrwxrwxrwx 1 root root 30 Nov 19 16:58 php-fpm.sock -> /etc/alternatives/php-fpm.sock -rw-r--r-- 1 root root 4 Nov 19 16:58 php8.3-fpm.pid srw-rw---- 1 www-data www-data 0 Nov 19 16:58 php8.3-fpm.sock $ grep -Rn 'fastcgi_pass' /etc/nginx/sites-enabled/ /etc/nginx/sites-enabled/default:14: fastcgi_pass unix:/run/php/php7.4-fpm.sock; $ sudo sed -i 's#unix:/run/php/php7.4-fpm.sock#unix:/run/php/php-fpm.sock#' /etc/nginx/sites-available/default $ sudo nginx -t && sudo systemctl reload nginx nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful $ curl -s http://localhost/test.php PHP 8.3.6 OK $ curl -sI http://localhost/test.php | head -n 1 HTTP/1.1 502 Bad Gateway $ sudo tail -n 1 /var/log/nginx/error.log 2026/11/19 16:58:24 [crit] 10235#10235: *13 connect() to unix:/run/php/php-fpm.sock failed (13: Permission denied) while connecting to upstream, client: ::1, server: _, request: "HEAD /test.php HTTP/1.1", upstream: "fastcgi://unix:/run/php/php-fpm.sock:", host: "localhost" $ ls -l /run/php/php8.3-fpm.sock srw-rw---- 1 nobody nogroup 0 Nov 19 16:58 /run/php/php8.3-fpm.sock $ grep -n '^listen\.' /etc/php/8.3/fpm/pool.d/www.conf 53:listen.owner = nobody 54:listen.group = nogroup $ sudo sed -i 's/^listen.owner = .*/listen.owner = www-data/; s/^listen.group = .*/listen.group = www-data/' /etc/php/8.3/fpm/pool.d/www.conf $ sudo php-fpm8.3 -t && sudo systemctl restart php8.3-fpm [19-Nov-2026 16:58:25] NOTICE: configuration file /etc/php/8.3/fpm/php-fpm.conf test is successful $ ls -l /run/php/php8.3-fpm.sock srw-rw---- 1 www-data www-data 0 Nov 19 16:58 /run/php/php8.3-fpm.sock $ curl -s http://localhost/test.php PHP 8.3.6 OK $ curl -sI http://localhost/app/ok.txt | head -n 1 HTTP/1.1 502 Bad Gateway $ sudo tail -n 1 /var/log/nginx/error.log 2026/11/19 16:58:26 [error] 10234#10234: *17 connect() failed (111: Connection refused) while connecting to upstream, client: ::1, server: _, request: "HEAD /app/ok.txt HTTP/1.1", upstream: "http://127.0.0.1:8000/ok.txt", host: "localhost" $ sudo ss -tlnp 'sport = :8000' State Recv-Q Send-Q Local Address:Port Peer Address:Port Process $ sudo systemctl start demo-app $ sudo ss -tlnp 'sport = :8000' State Recv-Q Send-Q Local Address:Port Peer Address:Port Process LISTEN 0 5 127.0.0.1:8000 0.0.0.0:* users:(("python3",pid=11215,fd=3)) $ curl -s http://localhost/app/ok.txt ok $ curl -sI http://localhost/big.php | head -n 1 HTTP/1.1 502 Bad Gateway $ sudo tail -n 1 /var/log/nginx/error.log 2026/11/19 16:58:28 [error] 10234#10234: *21 upstream sent too big header while reading response header from upstream, client: ::1, server: _, request: "HEAD /big.php HTTP/1.1", upstream: "fastcgi://unix:/run/php/php-fpm.sock:", host: "localhost" $ sudo sed -i 's#fastcgi_pass unix:/run/php/php-fpm.sock;#&\n fastcgi_buffer_size 16k;\n fastcgi_buffers 16 16k;#' /etc/nginx/sites-available/default $ sudo nginx -t && sudo systemctl reload nginx nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful $ curl -s http://localhost/big.php big header $ curl -sI http://localhost/crash.php | head -n 1 HTTP/1.1 502 Bad Gateway $ sudo tail -n 1 /var/log/nginx/error.log 2026/11/19 16:58:30 [error] 11734#11734: *25 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: ::1, server: _, request: "HEAD /crash.php HTTP/1.1", upstream: "fastcgi://unix:/run/php/php-fpm.sock:", host: "localhost" $ sudo tail -n 2 /var/log/php8.3-fpm.log [19-Nov-2026 16:58:30] WARNING: [pool www] child 10764 exited on signal 9 (SIGKILL) after 4.895434 seconds from start [19-Nov-2026 16:58:30] NOTICE: [pool www] child 11862 started $ curl -s http://localhost/test.php PHP 8.3.6 OK $ sudo rm /var/www/html/big.php /var/www/html/crash.php ------------------------------------------------------------------------------ The check-your-server script, its full screen before the steps ------------------------------------------------------------------------------ ServerHandbook check, nginx-502-bad-gateway-ubuntu (proved 2026-11-19) FAIL a PHP-FPM service is running fix: https://serverhandbook.com/kb/nginx-502-bad-gateway-ubuntu#php-fpm-stopped FAIL every PHP-FPM socket named in the enabled Nginx sites exists fix: https://serverhandbook.com/kb/nginx-502-bad-gateway-ubuntu#wrong-socket 0 passed, 2 failed To fix a FAIL, follow the steps in https://serverhandbook.com/kb/nginx-502-bad-gateway-ubuntu ------------------------------------------------------------------------------ The check-your-server script, its full screen after the steps ------------------------------------------------------------------------------ ServerHandbook check, nginx-502-bad-gateway-ubuntu (proved 2026-11-19) PASS a PHP-FPM service is running PASS every PHP-FPM socket named in the enabled Nginx sites exists 2 passed, 0 failed ------------------------------------------------------------------------------ The check's source (scripts/live/checks/nginx-502-bad-gateway-ubuntu.mjs) As it ran. ------------------------------------------------------------------------------ /** * KB fix: Nginx "502 Bad Gateway" on Ubuntu. Break → see → fix → prove, every cause in one run (plan04 §1): * the error page and the error log line that names the cause; then each cause reproduced the safe way and fixed: * A. PHP-FPM is stopped connect() to unix:… failed (2: No such file or directory) * B. fastcgi_pass names an old PHP version the same errno, the old version in the path; fix: /run/php/php-fpm.sock * C. the socket's owner is not www-data connect() to unix:… failed (13: Permission denied) * D. the proxied app is not running connect() failed (111: Connection refused) while connecting to upstream * E. the response headers are too big upstream sent too big header while reading response header from upstream * F. the PHP worker died mid-request what nginx logs, and the php-fpm log line that names the signal * * Hidden setup (the reader's working site): nginx + php-fpm, the default site with a PHP location and a /app/ * proxy_pass to 127.0.0.1:8000, a demo app (python3 http.server) as a systemd service, three test pages. * Runs on the local VM (no public port needed). */ import { bareBox, reportBox } from '../lib.mjs' export const meta = { reader: [ { item: 'a PHP-FPM service is running', cmd: "systemctl list-units --state=running --no-legend 'php*-fpm.service' | grep -c fpm", expect: '^[1-9]', step: 'php-fpm-stopped', }, { item: 'every PHP-FPM socket named in the enabled Nginx sites exists', cmd: "for s in $(grep -RhoP 'fastcgi_pass\\s+unix:\\K[^;]+' /etc/nginx/sites-enabled/ 2>/dev/null); do test -S \"$s\" || echo \"missing $s\"; done | grep -c missing", expect: '^0$', step: 'wrong-socket', }, ], } const RELEASE = process.env.LIVE_BOX_RELEASE || '24.04' const FORCE_V4 = `echo 'Acquire::ForceIPv4 "true";' | sudo tee /etc/apt/apt.conf.d/99force-ipv4 >/dev/null` const tee = (path, body) => `sudo tee ${path} > /dev/null <<'EOF'\n${body}\nEOF` const SITE = `server { listen 80 default_server; listen [::]:80 default_server; root /var/www/html; index index.php index.html; server_name _; location / { try_files $uri $uri/ =404; } location ~ \\.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/run/php/php-fpm.sock; } location /app/ { proxy_pass http://127.0.0.1:8000/; } }` const APP_UNIT = `[Unit] Description=Demo app on 127.0.0.1:8000 [Service] ExecStart=/usr/bin/python3 -m http.server 8000 --bind 127.0.0.1 --directory /var/www/html User=www-data [Install] WantedBy=multi-user.target` const check = (ctx) => bareBox(ctx, async ({ sudoUser }) => { const { run, L } = sudoUser() // ── hidden: the reader's working site ─────────────────────────────────────────────────── L(`${FORCE_V4} && sudo apt-get update -qq && sudo DEBIAN_FRONTEND=noninteractive apt-get install -y -qq nginx php-fpm >/dev/null`, 1800000) const PHPV = L('ls /etc/php | sort -V | tail -n 1').trim() const FPM = `php${PHPV}-fpm` const SOCK = `/run/php/${FPM}.sock` ctx.check(`(setup) nginx and PHP-FPM ${PHPV} are installed and running`, /^\d+\.\d+$/.test(PHPV) && L(`systemctl is-active nginx ${FPM}`) === 'active\nactive', PHPV) L(tee('/etc/nginx/sites-available/default', SITE)) L(tee('/etc/systemd/system/demo-app.service', APP_UNIT)) L(`echo '/dev/null`) L(`echo '/dev/null`) L(`echo '/dev/null`) L('echo ok | sudo tee /var/www/html/ok.txt >/dev/null') const up = L('sudo systemctl daemon-reload && sudo systemctl enable --now demo-app -q && sudo nginx -t -q && sudo systemctl reload nginx && sleep 1 && curl -s http://localhost/test.php && curl -s http://localhost/app/ok.txt') ctx.check('(setup) the site works: test.php runs PHP and /app/ reaches the app on 127.0.0.1:8000', new RegExp(`PHP ${PHPV.replace('.', '\\.')}\\.\\d+ OK`).test(up) && /^ok$/m.test(up), up) const linked = L('readlink -f /run/php/php-fpm.sock') ctx.check(`(setup) /run/php/php-fpm.sock points to ${SOCK} (Debian's alternatives link)`, linked === SOCK, linked) // ── cause A: PHP-FPM is stopped ───────────────────────────────────────────────────────── L(`sudo systemctl stop ${FPM}`) const a1 = run('curl -sI http://localhost/test.php | head -n 1', { id: 'a-curl' }) ctx.check('curl -I: HTTP/1.1 502 Bad Gateway', /^HTTP\/1\.1 502 Bad Gateway/.test(a1.out), a1.out) const a2 = run('sudo tail -n 1 /var/log/nginx/error.log', { id: 'a-log' }) ctx.check(`error.log: connect() to unix:/run/php/php-fpm.sock failed (2: No such file or directory) while connecting to upstream`, /\[crit\].*connect\(\) to unix:\/run\/php\/php-fpm\.sock failed \(2: No such file or directory\) while connecting to upstream/.test(a2.out), a2.out) const a3 = run("systemctl list-units --all --no-pager 'php*-fpm.service'", { id: 'a-units' }) ctx.check(`systemctl list-units: ${FPM}.service is inactive (dead)`, new RegExp(`${FPM}\\.service\\s+loaded\\s+inactive\\s+dead`).test(a3.out), a3.out) const a4 = run('ls -l /run/php/', { id: 'a-ls' }) ctx.check('ls -l /run/php/: no socket while PHP-FPM is stopped', !new RegExp(`^s.*${FPM}\\.sock`, 'm').test(a4.out), a4.out) const a5 = run(`sudo systemctl enable --now ${FPM}`, { id: 'a-fix' }) ctx.check(`enable --now starts ${FPM} (it was already enabled: no output, or a "Synchronizing" line)`, a5.exit === 0, a5.out) const a6 = run('curl -s http://localhost/test.php', { id: 'a-ok' }) ctx.check(`curl: "PHP ${PHPV}.x OK": PHP answers again`, new RegExp(`^PHP ${PHPV.replace('.', '\\.')}\\.\\d+ OK$`).test(a6.out), a6.out) // ── cause B: fastcgi_pass names a PHP version that is not installed ───────────────────── L(`sudo sed -i 's#unix:/run/php/php-fpm.sock#unix:/run/php/php7.4-fpm.sock#' /etc/nginx/sites-available/default && sudo systemctl reload nginx`) const b1 = run('curl -sI http://localhost/test.php | head -n 1', { id: 'b-curl' }) ctx.check('the wrong socket path: HTTP/1.1 502 Bad Gateway', /^HTTP\/1\.1 502 Bad Gateway/.test(b1.out), b1.out) const b2 = run('sudo tail -n 1 /var/log/nginx/error.log', { id: 'b-log' }) ctx.check('error.log: connect() to unix:/run/php/php7.4-fpm.sock failed (2: No such file or directory)', /connect\(\) to unix:\/run\/php\/php7\.4-fpm\.sock failed \(2: No such file or directory\)/.test(b2.out), b2.out) const b3 = run('ls -l /run/php/', { id: 'b-ls' }) ctx.check(`ls -l /run/php/: the real socket is ${FPM}.sock, and php-fpm.sock links to it`, new RegExp(`^s.*${FPM}\\.sock$`, 'm').test(b3.out) && /php-fpm\.sock -> \/etc\/alternatives\/php-fpm\.sock/.test(b3.out), b3.out) const b4 = run("grep -Rn 'fastcgi_pass' /etc/nginx/sites-enabled/", { id: 'b-grep' }) ctx.check('grep finds the line: fastcgi_pass unix:/run/php/php7.4-fpm.sock;', /fastcgi_pass unix:\/run\/php\/php7\.4-fpm\.sock;/.test(b4.out), b4.out) const b5 = run("sudo sed -i 's#unix:/run/php/php7.4-fpm.sock#unix:/run/php/php-fpm.sock#' /etc/nginx/sites-available/default", { id: 'b-sed' }) ctx.check('sed changes it to the version-free /run/php/php-fpm.sock', b5.exit === 0, b5.out) const b6 = run('sudo nginx -t && sudo systemctl reload nginx', { id: 'b-reload' }) ctx.check('nginx -t: syntax is ok, test is successful', /syntax is ok/.test(b6.out) && /test is successful/.test(b6.out), b6.out) const b7 = run('curl -s http://localhost/test.php', { id: 'b-ok' }) ctx.check('curl: PHP answers again', /^PHP \d+\.\d+\.\d+ OK$/.test(b7.out), b7.out) // ── cause C: the socket is not owned by www-data ──────────────────────────────────────── const POOL = `/etc/php/${PHPV}/fpm/pool.d/www.conf` L(`sudo sed -i 's/^listen.owner = www-data/listen.owner = nobody/; s/^listen.group = www-data/listen.group = nogroup/' ${POOL} && sudo systemctl restart ${FPM}`) const c1 = run('curl -sI http://localhost/test.php | head -n 1', { id: 'c-curl' }) ctx.check('wrong socket owner: HTTP/1.1 502 Bad Gateway', /^HTTP\/1\.1 502 Bad Gateway/.test(c1.out), c1.out) const c2 = run('sudo tail -n 1 /var/log/nginx/error.log', { id: 'c-log' }) ctx.check('error.log: connect() to unix:/run/php/php-fpm.sock failed (13: Permission denied)', /connect\(\) to unix:\/run\/php\/php-fpm\.sock failed \(13: Permission denied\)/.test(c2.out), c2.out) const c3 = run(`ls -l ${SOCK}`, { id: 'c-ls' }) ctx.check('ls -l: the socket is srw-rw---- nobody nogroup, so www-data (nginx) cannot open it', /^srw-rw----\s+\d+\s+nobody\s+nogroup\s/.test(c3.out), c3.out) const c4 = run(`grep -n '^listen\\.' ${POOL}`, { id: 'c-grep' }) ctx.check('the pool file says listen.owner = nobody, listen.group = nogroup', /listen\.owner = nobody/.test(c4.out) && /listen\.group = nogroup/.test(c4.out), c4.out) const c5 = run(`sudo sed -i 's/^listen.owner = .*/listen.owner = www-data/; s/^listen.group = .*/listen.group = www-data/' ${POOL}`, { id: 'c-sed' }) ctx.check('sed puts www-data back', c5.exit === 0, c5.out) const c6 = run(`sudo php-fpm${PHPV} -t && sudo systemctl restart ${FPM}`, { id: 'c-restart' }) ctx.check(`php-fpm${PHPV} -t: configuration file … test is successful`, /test is successful/.test(c6.out), c6.out) const c7 = run(`ls -l ${SOCK}`, { id: 'c-ls-after' }) ctx.check('ls -l: srw-rw---- www-data www-data', /^srw-rw----\s+\d+\s+www-data\s+www-data\s/.test(c7.out), c7.out) const c8 = run('curl -s http://localhost/test.php', { id: 'c-ok' }) ctx.check('curl: PHP answers again', /^PHP \d+\.\d+\.\d+ OK$/.test(c8.out), c8.out) // ── cause D: the proxied app is not running ───────────────────────────────────────────── L('sudo systemctl stop demo-app') const d1 = run('curl -sI http://localhost/app/ok.txt | head -n 1', { id: 'd-curl' }) ctx.check('the app is down: HTTP/1.1 502 Bad Gateway', /^HTTP\/1\.1 502 Bad Gateway/.test(d1.out), d1.out) const d2 = run('sudo tail -n 1 /var/log/nginx/error.log', { id: 'd-log' }) ctx.check('error.log: connect() failed (111: Connection refused) while connecting to upstream … upstream: "http://127.0.0.1:8000/ok.txt"', /connect\(\) failed \(111: Connection refused\) while connecting to upstream/.test(d2.out) && /upstream: "http:\/\/127\.0\.0\.1:8000\/ok\.txt"/.test(d2.out), d2.out) const d3 = run("sudo ss -tlnp 'sport = :8000'", { id: 'd-ss' }) ctx.check('ss: nothing listens on port 8000 (only the header line)', !/LISTEN/.test(d3.out), d3.out) const d4 = run('sudo systemctl start demo-app', { id: 'd-fix' }) ctx.check('systemctl start demo-app', d4.exit === 0, d4.out) L('for i in 1 2 3 4 5 6 7 8 9 10; do ss -tln | grep -q 127.0.0.1:8000 && break; sleep 1; done') const d5 = run("sudo ss -tlnp 'sport = :8000'", { id: 'd-ss-after' }) ctx.check('ss: python3 listens on 127.0.0.1:8000', /127\.0\.0\.1:8000/.test(d5.out) && /"python3"/.test(d5.out), d5.out) const d6 = run('curl -s http://localhost/app/ok.txt', { id: 'd-ok' }) ctx.check('curl /app/ok.txt: "ok" from the app', /^ok$/.test(d6.out), d6.out) // ── cause E: response headers bigger than the buffer ──────────────────────────────────── const e1 = run('curl -sI http://localhost/big.php | head -n 1', { id: 'e-curl' }) ctx.check('an 8 KB header: HTTP/1.1 502 Bad Gateway', /^HTTP\/1\.1 502 Bad Gateway/.test(e1.out), e1.out) const e2 = run('sudo tail -n 1 /var/log/nginx/error.log', { id: 'e-log' }) ctx.check('error.log: upstream sent too big header while reading response header from upstream', /upstream sent too big header while reading response header from upstream/.test(e2.out), e2.out) const e3 = run("sudo sed -i 's#fastcgi_pass unix:/run/php/php-fpm.sock;#&\\n fastcgi_buffer_size 16k;\\n fastcgi_buffers 16 16k;#' /etc/nginx/sites-available/default", { id: 'e-sed' }) ctx.check('sed adds fastcgi_buffer_size 16k and fastcgi_buffers 16 16k under fastcgi_pass', e3.exit === 0 && /fastcgi_buffer_size 16k;/.test(L('cat /etc/nginx/sites-available/default')), e3.out) const e4 = run('sudo nginx -t && sudo systemctl reload nginx', { id: 'e-reload' }) ctx.check('nginx -t is successful with the bigger buffers', /test is successful/.test(e4.out), e4.out) const e5 = run('curl -s http://localhost/big.php', { id: 'e-ok' }) ctx.check('curl big.php: "big header": the page loads', /^big header$/.test(e5.out), e5.out) // ── cause F: the PHP worker dies in the middle of a request ───────────────────────────── const f1 = run('curl -sI http://localhost/crash.php | head -n 1', { id: 'f-curl' }) ctx.check('a worker killed mid-request: HTTP/1.1 502 Bad Gateway', /^HTTP\/1\.1 502 Bad Gateway/.test(f1.out), f1.out) const f2 = run('sudo tail -n 1 /var/log/nginx/error.log', { id: 'f-log' }) ctx.check('error.log: the upstream closed the connection while reading the response header', /(upstream prematurely closed|Connection reset by peer).*while reading response header from upstream/.test(f2.out), f2.out) const f3 = run(`sudo tail -n 2 /var/log/${FPM}.log`, { id: 'f-fpmlog' }) ctx.check(`/var/log/${FPM}.log: WARNING: [pool www] child … exited on signal 9 (SIGKILL)`, /WARNING: \[pool www\] child \d+ exited on signal 9 \(SIGKILL\)/.test(f3.out), f3.out) const f4 = run('curl -s http://localhost/test.php', { id: 'f-ok' }) ctx.check('the other pages still work: PHP-FPM started a new worker', /^PHP \d+\.\d+\.\d+ OK$/.test(f4.out), f4.out) // ── end state ─────────────────────────────────────────────────────────────────────────── const s1 = L("systemctl list-units --state=running --no-legend 'php*-fpm.service' | grep -c fpm") ctx.check('(a PHP-FPM service is running at the end of the steps)', /^[1-9]/.test(s1), s1, { reader: { cmd: meta.reader[0].cmd, expect: meta.reader[0].expect }, }) const s2 = L(meta.reader[1].cmd) ctx.check('(every socket the enabled sites name exists at the end of the steps)', /^0$/.test(s2.trim()), s2, { reader: { cmd: meta.reader[1].cmd, expect: meta.reader[1].expect }, }) const s3 = L('curl -s -o /dev/null -w "%{http_code}" http://localhost/test.php') ctx.check('(test.php answers 200 at the end of the steps)', s3 === '200', s3) ctx.stepsDone() reportBox( ctx, L, `A fresh Ubuntu ${RELEASE} server (${process.env.LIVE_VPS ? 'a rented cloud server, rebuilt to the provider’s stock image' : 'a VM from Ubuntu’s cloud image'}) with Nginx and PHP-FPM from Ubuntu’s archive, the default site serving PHP through /run/php/php-fpm.sock and proxying /app/ to a small app on 127.0.0.1:8000, reached over SSH as the sudo user deploy. Each cause is created first, then found from the error log and fixed.`, ['nginx', 'php-fpm', FPM, 'python3', 'iproute2', 'curl'], ) // ── undo: remove the test pages ───────────────────────────────────────────────────────── const un = run('sudo rm /var/www/html/big.php /var/www/html/crash.php', { id: 'undo' }) ctx.check('undo: the two test pages are removed', un.exit === 0 && /No such file/.test(L('ls /var/www/html/crash.php 2>&1')), un.out) }) export default check