- I need to host 3 websites:
- WHMCS: For my hosting billing
- WordPress 1x: For my blog
- WordPress 1x: For my testing
- 1x AWS EC2 t2.nano for MariaDB
- 1x AWS EC2 t2.nano for web-WHMCS
- 1x AWS EC2 t2.nano for web-WordPress (2 websites)
- Using Apache + PHP8.2-FPM
- Under heavy load (simulated with Grafana K6), PHP-FPM killed by OOM (Out-Of-Memory) Killer.
- OOM is a features by an Operating System (OS) to automatically kill a process.
- OOM do have scoring which dictates what process to be killed.
- Process with higher memory usage will have higher OOM Scoring.
- PHP-FPM easily become a main target by the OS since by nature it will spawn many child to serve concurrent request.
- WordPress cron executed from php wp-cron.php use huge portion of available RAM and thus, killing PHP-FPM.
- Each time PHP-FPM killed, http request will failed.
- Disable OOM Killer. Not a good solution since OOM Killer means to stabilize the OS. ❌
- Set vm_overcommit_memory to value (2) which means don't allow overcommit. Not a good solution as RAM consumption may be higher than avaialable RAM at any point of time. ❌
- Reduce apache from spawning too many child: Failed ❌
- Reduce php-cli memory limit: Failed and theoritically unacceptable ❌
- Limit PHP-FPM from spawning too much child: Failed ❌
- Reduce Apache MaxKeepAliveRequests: Failed ❌
- Reduce Apache MaxRequestWorkers in mpm_prefork.conf module: Website load becomes incredibly slow and still Failed ❌
Failed means, PHP-FPM still get killed and requires manual restart
- Define always restart in PHP-FPM service
$ sudo nano /lib/systemd/system/php8.2-fpm.service
...
[Service]
Restart=always
- Define 503 meta refresh to automatically reattempt in apache (/etc/apache/apache2.conf) or .htaccess
ErrorDocument 503 "<head><meta http-equiv=\"refresh\" content=\"5\"></head>"
- https://www.percona.com/blog/out-of-memory-killer-or-savior/
- https://serverfault.com/questions/1109212/limit-php-fpm-memory-usage-on-a-constrained-server
- https://stackoverflow.com/questions/19725490/is-there-an-easy-way-to-automatically-do-a-php-fpm-restart-after-a-502-gateway-t
- https://freshman.tech/snippets/linux/auto-restart-systemd-service/