Skip to content

Instantly share code, notes, and snippets.

@wzul
Last active April 2, 2023 10:13
Show Gist options
  • Save wzul/641ca420d6a4191f83ee08a9d0100c8e to your computer and use it in GitHub Desktop.
Save wzul/641ca420d6a4191f83ee08a9d0100c8e to your computer and use it in GitHub Desktop.
Deploying WordPress with AWS EC2 t2.nano

Situation

  • I need to host 3 websites:
    • WHMCS: For my hosting billing
    • WordPress 1x: For my blog
    • WordPress 1x: For my testing

Architecture

  • 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)

Server setup

  • Using Apache + PHP8.2-FPM

Issues

  • 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.

Possible solution

  • 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

Best solution ✅

  • 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>"

Reference

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment