WordPress on AWS EC2 | Database Crash Fix + Nginx Page Cache by Raja SheryarWordPress on AWS EC2 | Database Crash Fix + Nginx Page Cache by Raja Sheryar
Live Mauritius and Lapointe Apartments are two WordPress (Elementor) sites on one small AWS EC2 server running Nginx, PHP-FPM and MariaDB. Three times (1 Aug, 4 Sep and 15 Oct 2026) both sites went down with the message Error establishing a database connection. I traced the cause on the server and fixed it at the root.
What was wrong
The kernel log showed MariaDB being killed by the Linux out-of-memory killer
PHP-FPM allowed up to 50 workers on a server with about 1 GB of RAM, so busy moments used up the memory and the database was the first thing to go
What I changed (15 Oct 2026)
Capped PHP-FPM at 8 workers to fit the server's memory
Added a systemd drop-in so MariaDB restarts on failure and is far less likely to be picked by the out-of-memory killer
Added an Nginx FastCGI page cache (10 minutes) that skips admin, login, cart and logged-in visitors and serves the cached page if PHP errors
Added an X-Cache header to check cache hits, and kept backups of the configs I changed
Result (checked 23 Oct 2026)
0 database crashes and 0 MariaDB restarts since the fix
Cached homepage TTFB about 0.7 s against 1.9 s uncached in my test on live-mauritius.com
Two WordPress sites on one AWS EC2 server kept going down (MariaDB killed for memory). Capped PHP-FPM, auto-restart DB, Nginx page cache: 0 crashes since.