2026年11月12日
2026年11月12日
Set Up Swap on a VPS to Fix Low Memory (WordPress Server Guidelines)
This article is a localized edition of the Japanese original. Read the Japanese original
If you run WordPress on a VPS with little memory and see symptoms such as MySQL going down or PHP being forcibly terminated, creating a swap file can absorb sudden memory shortages and make processes less likely to be killed. But swap is not a substitute for memory; it is only a temporary escape route. This article covers how to tell whether you need swap, how to create a swap file and make it persistent, how to adjust swappiness, and what to do in cases where swap alone does not solve the problem.
First, check the current state
First, check the state of memory and swap.
free -h
swapon --show
If the Swap row of free -h is all 0 and swapon --show prints nothing, swap is not configured. OS images for the cloud are often set up to boot without swap.
Next, check whether memory shortages are actually occurring. When memory runs out, Linux uses a mechanism called the OOM Killer to forcibly terminate processes that use a lot of memory. The record is left in the kernel log.
sudo journalctl -k | grep -i -E "out of memory|oom"
# If you are not using journald
sudo dmesg -T | grep -i -E "out of memory|oom"
If you see a line such as "Out of memory: Killed process 1234 (mysqld)", processes are being stopped because of a memory shortage. On WordPress, a typical symptom is the database going down and the "Error establishing a database connection" message appearing.
What swap can and cannot solve
Swap is a mechanism that moves the contents of unused memory out to disk and gives the freed memory to other processes.
Cases that are easy to solve
- Memory usage jumps temporarily because of traffic spikes, plugin updates, backup jobs, and so on
- Processes that started but are rarely used in normal operation (such as services that just stay resident) are taking up memory
Cases that are hard to solve
- Memory is used up all the time, and swap is constantly being read and written
- The number of concurrent PHP-FPM processes or the MySQL buffers are too large for the amount of memory
If you add swap in the latter situation, instead of processes dying, the whole site becomes extremely slow while waiting on disk reads and writes. In this case you need to review the settings or add memory rather than use swap (covered in the second half).
Some environments cannot create swap
On a VPS that uses container-based virtualization such as OpenVZ or LXC, users may not be able to create swap. If swapon, described below, shows "Operation not permitted", this is the case, and you have to follow the provider's specifications. You can check the virtualization type with systemd-detect-virt. On a VPS that uses hypervisor-based virtualization such as KVM, you can normally create it.
How to create a swap file
You can also create a dedicated partition, but on a VPS a swap file, whose size is easy to change later, is easier to work with. Before you start, check the free disk space with df -h /.
Sizing guidelines
When running WordPress on a small VPS with 1 to 2 GB of memory, it is common to start with about the same size as the memory (1 to 2 GB). Making it too large only eats into your disk and makes the cause of slowness harder to see. On servers that do not use hibernation, you do not need to stick to the old rule of thumb of twice the memory.
Create and enable
Here is an example that creates a 2 GB swap file.
sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
swapon --show
free -h
chmod 600 keeps anyone other than root from reading the file, since the contents of memory are written to it. If you skip it, swapon prints a warning.
If swapon shows "skipping - it appears to have holes", the file system cannot treat the file created with fallocate as swap. Delete it once and recreate it with dd.
sudo rm /swapfile
sudo dd if=/dev/zero of=/swapfile bs=1M count=2048 status=progress
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
If the root file system is Btrfs, swap files need special handling. Newer btrfs-progs has a command called btrfs filesystem mkswapfile, so check the documentation for your environment.
Keep it enabled after a reboot
swapon alone is reverted by a reboot, so add it to /etc/fstab. A mistake in fstab affects booting, so take a backup first.
sudo cp /etc/fstab /etc/fstab.bak
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
sudo swapoff /swapfile
sudo swapon -a
swapon --show
If swapon -a can enable it from the contents of fstab, the entry is being read correctly. On some VPS providers, if a reboot fails you can only work from the console, so it is reassuring to confirm in advance that you can connect to the console from the provider's control panel.
Adjust swappiness
vm.swappiness is a value that sets how actively swap is used while there is still free memory. The default on many distributions is 60. On a server like WordPress, where the database and PHP use memory, you want swap to be an "escape route for when memory is really short," so it is common to set a lower value.
cat /proc/sys/vm/swappiness
echo 'vm.swappiness = 10' | sudo tee /etc/sysctl.d/99-swappiness.conf
sudo sysctl --system
cat /proc/sys/vm/swappiness
At 0, swap is hardly used and the OOM Killer is more likely to act first. Start by trying around 10, then watch and adjust.
Check that swap is working
After setting it up, run for a while and then check how swap is being used.
vmstat 5
If the si (read from swap) and so (write to swap) columns are normally close to 0 and increase only under high load, swap is working as an escape route. If numbers keep showing up all the time, memory itself is not enough.
What to do when swap alone is not enough
If swap is in constant use, review the side that uses memory.
- Concurrent PHP-FPM processes: If
pm.max_childrenis too large, PHP processes use up all the memory when traffic spikes. Measure the usage per process withpsor similar, and work backward from the memory available to set the upper limit. - MySQL / MariaDB buffers: Check whether
innodb_buffer_pool_sizeis too large for the memory. On a small VPS, set it conservatively on the assumption that it shares memory with the web server and PHP. - Unneeded services and plugins: Stop resident services you do not use, and review plugins that do heavy processing. Adding a page cache reduces the PHP and database work itself.
- Change the plan: If the above is not enough, moving to a plan with more memory is the fundamental fix.
Before changing settings, back up the relevant configuration files, and after the change, check the state of memory and swap during busy hours.
If you want to remove the swap file
If you no longer need it or want to recreate it, remove it in this order.
sudo swapoff /swapfile
sudo sed -i.bak '/^\/swapfile /d' /etc/fstab
sudo rm /swapfile
swapoff moves the swap contents back into memory, so if you run it when memory is tight, it can take a long time or fail. Do it during hours with little traffic.
There is also a method called zram, which compresses memory and uses it like swap without using disk. It is worth considering on a VPS with slow disk reads and writes, but the setup differs by distribution, so check the documentation for the OS you use (as of October 2026).
Summary
- Check whether swap exists with
free -handswapon --show, and check the kernel log for OOM Killer records. - Create the swap file with
fallocate(orddif that fails), thenchmod 600,mkswap,swapon, and make it persistent by adding it to fstab. Back up fstab before editing it. - Lower
vm.swappinessto around 10 and use swap as a temporary escape route. - If
vmstatshows si / so all the time, you need to review the PHP-FPM and MySQL settings or add memory.
- How to review memory settings when the OOM Killer takes down processes
- How to optimize PHP-FPM on a VPS
Questions about WordPress? The publisher of this site is based in Japan and replies in Japanese. Contact (Japanese)