Linux OOM Killer and Swap Tuning for WordPress Servers

The Linux OOM killer and swap on a WordPress server

When a Linux server runs out of memory, the kernel’s out-of-memory killer picks a process and kills it. On a WordPress server, the process with the most memory is usually MySQL, so the site goes down with “Error establishing a database connection”, although the real cause was too many PHP-FPM workers or a runaway import. Finding the cause, protecting the database and adding a small swap file turns a crash into, at worst, a slow minute.

Problem: A 4 GB VPS runs Nginx, PHP-FPM, MySQL and Redis. Two or three times a month MySQL disappears at night and has to be restarted; the MySQL log only says it was started again.

Solution: Confirm OOM kills in the kernel log, cap PHP-FPM’s worst case with pm.max_children and memory_limit, make the OOM killer prefer PHP workers over MySQL with OOMScoreAdjust, add a 1–2 GB swap file with a low vm.swappiness, and let systemd restart MySQL if it is killed anyway.

Was it the OOM killer?

journalctl -k --since "-7 days" | grep -iE 'out of memory|oom-kill|killed process'
# Out of memory: Killed process 1234 (mysqld) total-vm:2843120kB, anon-rss:1650432kB ...
dmesg -T | grep -i oom

The kernel log names the victim and lists the memory usage of all processes at that moment. Many php-fpm lines with 100–200 MB each next to mysqld show the usual picture: the workers together took the memory, MySQL was the biggest single process and was killed.

Cap the worst case

Memory use peaks when all PHP-FPM workers are busy with heavy requests at once. That number must fit next to MySQL’s buffer pool and Redis:

#  4 GB RAM
#  MySQL (buffer pool 1 GB + overhead)   ~1.4 GB
#  Redis                                  ~0.2 GB
#  OS, Nginx, page cache reserve          ~0.6 GB
#  left for PHP-FPM                       ~1.8 GB  ->  12 workers x 150 MB
; /etc/php/8.3/fpm/pool.d/www.conf
pm = dynamic
pm.max_children = 12
pm.max_requests = 500

pm.max_requests recycles workers after 500 requests, which contains slow memory leaks in plugins. With fewer workers, peak load produces queued requests instead of a dead database. How to measure the real memory per worker: tune the PHP-FPM pool.

Tell the OOM killer what to keep

sudo systemctl edit mysql
[Service]
OOMScoreAdjust=-600
Restart=on-failure
RestartSec=5
sudo systemctl edit php8.3-fpm
[Service]
OOMScoreAdjust=300

OOMScoreAdjust ranges from -1000 (never kill) to 1000 (kill first). With these values, the kernel kills a PHP worker before MySQL; the FPM master starts a new worker and the visitor of that one request gets an error. Do not use -1000 for MySQL: if MySQL itself is the problem, the kernel would kill everything else, including SSH. Restart=on-failure brings MySQL back automatically if it is killed anyway.

A small swap file

sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

echo 'vm.swappiness = 10' | sudo tee /etc/sysctl.d/99-swappiness.conf
sudo sysctl --system

Many VPS images come without swap. A little swap gives the kernel a place for memory pages that are rarely used (idle processes, startup code) and a buffer for short peaks, instead of killing something immediately. vm.swappiness = 10 tells the kernel to prefer dropping file cache over swapping process memory. Swap is not extra RAM: if the server swaps constantly (vmstat 1 shows non-zero si/so all the time), it needs fewer workers or more memory: htop, vmstat and iostat.

NOTE: WP-CLI imports and cron jobs run outside PHP-FPM with their own memory_limit (often unlimited on the CLI). A product import at night with -1 as memory limit is a classic OOM trigger; set php -d memory_limit=512M for such jobs or a limit in the CLI php.ini.