Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

To add swap to a WordPress VPS, first confirm that no swap is already active, create a disk-backed file, restrict it to mode 600, format it with mkswap, enable it with swapon, and add an /etc/fstab entry so it returns after reboot. A 1 GiB swap file is a reasonable starting point for a small VPS, but the right size depends on your RAM, workload and free disk space. Swap is a slower safety buffer for short memory spikes—not a substitute for adequate RAM or for fixing a leaking plugin, oversized PHP-FPM pool or overloaded database.

What swap does for a WordPress VPS

Swap is disk-backed virtual memory. When Linux cannot keep every inactive memory page in RAM, it can move some pages to a reserved area on storage and reclaim RAM for active work. The storage device is far slower than RAM, so swap protects availability during a spike but can make a heavily swapped server sluggish.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For WordPress, swap can give PHP-FPM, the web server and the database a little room during bursts such as traffic surges, cron jobs, backups or cache warm-ups. It does not increase CPU capacity, make slow storage faster or permanently solve sustained memory pressure. If the kernel is constantly moving pages in and out of swap, the VPS may spend more time waiting on storage and still be killed by the out-of-memory (OOM) manager.

  • Use swap as a safety margin: it can reduce the chance that a brief allocation spike immediately kills a service.
  • Do not treat it as extra RAM: sustained demand still requires smaller services, fewer memory-heavy plugins, better database/PHP-FPM sizing or more physical memory.
  • Keep disk headroom: the swap file shares storage with WordPress core, uploads, logs, database files and backups.

Check whether swap already exists

Do not create a second file until you know the current state. Run these commands as a user with sudo access:

sudo swapon --show
free -h
df -h /

swapon --show lists active swap areas. In free -h, the Swap row shows total, used and available swap in readable units. If the command produces no rows and the Swap total is 0B, no active swap is configured. df -h / confirms that the root filesystem has enough free space for the file while leaving room for normal WordPress operations.

If an existing swap area is already adequate, adding another one may add complexity without solving the underlying issue. Record its path and size before changing anything.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Choose the swap size

WordPress does not mandate a swap size. A practical starting point is 1 GiB on a small VPS, then adjust after observing real memory use. DigitalOcean’s Debian guidance describes swap equal to RAM or twice RAM as a starting heuristic and notes that more than 4 GiB is often unnecessary when swap is only a fallback. Those are heuristics, not WordPress requirements.

Situation Starting approach What to verify
Small site with occasional spikes 1 GiB file is a reasonable example Free disk after creating it; whether OOM events stop
VPS with more RAM and bursty jobs Consider a RAM-sized file, subject to disk capacity Peak usage during backups, imports and cron tasks
Persistent memory pressure Do not simply keep enlarging swap PHP-FPM workers, database memory, plugins and logs; consider more RAM
Very limited disk Use the smallest safety buffer that leaves operational headroom WordPress uploads, database growth and backup space

Swap space is reserved on disk, so a 4 GiB file consumes 4 GiB that cannot be used for other data. Size it for a temporary buffer, not for the total memory your services need during normal operation.

Create a swap file on Ubuntu or Debian

The following example creates /swapfile with 1 GiB. Replace 1G or the dd count only after checking your free space and workload.

1. Create a hole-free file

On filesystems where it produces a valid, fully allocated file, use:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
sudo fallocate -l 1G /swapfile

Some filesystems or kernels reject swap files containing holes (unallocated ranges that look like zeros). If swapon later reports that the file has holes, create it with dd, which writes every block:

sudo dd if=/dev/zero of=/swapfile bs=1M count=1024 status=progress

The dd command writes 1,024 blocks of 1 MiB, approximately 1 GiB. It can take longer than fallocate because it actually writes the data. Btrfs has additional restrictions around swap files; check the filesystem type and use a distribution-supported method, a supported swap partition or the documented Btrfs procedure rather than forcing a regular file.

2. Restrict access to the file

sudo chmod 600 /swapfile

Mode 600 allows only root to read or write the file. This matters because swapped pages can contain credentials, session data or other information that ordinary users should not be able to inspect. Verify the mode if needed with ls -l /swapfile; the permissions should show read and write access for root only.

3. Format and activate it

sudo mkswap /swapfile
sudo swapon /swapfile

mkswap writes the swap signature and metadata. swapon makes the area available immediately; it does not yet make the configuration persistent across a reboot.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

4. Verify activation

sudo swapon --show
free -h

You should now see /swapfile in the listing and a non-zero Swap total in free -h. A small Used value is normal; Linux may use swap for inactive pages even when RAM is not completely full.

Make swap survive a reboot

Add this exact line to /etc/fstab:

/swapfile none swap sw 0 0

You can edit the file with an editor such as sudo nano /etc/fstab. Keep the path absolute. For a regular-file source, Ubuntu’s swapon documentation expects an absolute path.

After saving, test the entry without rebooting:

sudo swapon -a
sudo swapon --show

swapon -a (the same operation exposed as swapon --all) reads swap entries from /etc/fstab and enables any that are not already active. If it prints an error, fix the line before rebooting. Once the test succeeds, reboot during a maintenance window and run sudo swapon --show and free -h again to confirm that the setting persisted.

Understand and tune swappiness

Swappiness is a Linux kernel value from 0 to 100 that influences how readily the system considers swapping. Many distributions use 60 as a default, but the best value depends on the storage device, available RAM and workload. It is not a measure of how much swap is allocated or currently used.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Check the current value

cat /proc/sys/vm/swappiness

Test a temporary value

sudo sysctl vm.swappiness=10

This change lasts until reboot. A lower value can make Linux prefer reclaiming other cache before swapping anonymous memory, but setting it blindly can also increase allocation pressure and trigger an OOM event sooner. Observe latency, swap-in activity, available RAM and application errors under your normal traffic before making a permanent change.

Persist a value only after testing

If measurements show that a different value helps, add a single setting to /etc/sysctl.conf, for example:

vm.swappiness=10

Load it with:

sudo sysctl -p

There is no universal “magic” number for WordPress. A busy database, a PHP-heavy site and a lightly used blog can behave differently even on the same VPS plan.

Swap file versus swap partition

Characteristic Swap file Swap partition
Adding space to an existing VPS Usually easiest; can be created without repartitioning May require an existing unused partition or disk changes
Resizing Remove and recreate, or add another file, with care Often requires partition work and downtime
Filesystem requirements Must be hole-free; Btrfs has extra restrictions Not subject to regular-file hole rules
Persistence Absolute path in /etc/fstab Partition device or UUID in /etc/fstab
Operational risk Low when permissions and filesystem support are correct Partitioning mistakes can affect other data

For a typical Ubuntu or Debian WordPress VPS with free space, a swap file is the least disruptive option. A partition can be appropriate when the provider or filesystem does not support regular-file swap reliably, but creating one may require provider-specific recovery or partitioning steps not covered by this procedure.

WordPress memory limits are separate from system swap

Swap operates at the operating-system level. WordPress and PHP impose separate limits on what a request may allocate. Current WordPress requirements recommend PHP 8.3 or newer, MariaDB 10.11 or newer or MySQL 8.0 or newer, and HTTPS; Apache or Nginx is recommended. Swap is not listed as a WordPress requirement.

WordPress documentation states that the default requested PHP memory is 40 MB for a single site and 64 MB for multisite. Increasing that limit can hide the cause of a memory problem instead of fixing it. A PHP fatal error such as “Allowed memory size exhausted” can therefore remain after swap is added, depending on how PHP is configured and whether the process reaches its own limit first.

What to inspect when WordPress still runs out of memory

  • PHP-FPM: review the number of workers, each worker’s memory footprint and the pool’s limits. Too many concurrent workers can exhaust RAM even when each request is within its PHP limit.
  • Plugins and themes: identify extensions that allocate large arrays, process images, run imports or schedule frequent jobs. Disable or replace a confirmed memory-heavy component rather than masking it with more swap.
  • Database: inspect query patterns, buffer settings and concurrent connections. A database can consume most of the VPS independently of PHP.
  • Background work: check WP-Cron, queues, backups, image conversion and search indexing, especially if failures coincide with scheduled tasks.
  • Capacity: if memory pressure is sustained during ordinary traffic, move to a VPS with more RAM or a managed service sized for the workload.

Monitor whether swap is helping

After activation, check the server during the traffic or job that previously caused trouble. Compare RAM available, swap used and application response times rather than judging success from a non-zero swap total alone.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
free -h
sudo swapon --show

A swap area that remains mostly unused can still be useful as an emergency buffer. Conversely, rapidly rising swap use, high I/O wait, slow WordPress responses or repeated service restarts indicate that the VPS is under continuing pressure. Review kernel and service logs, including:

dmesg
journalctl -k
journalctl -u php*-fpm

The exact PHP-FPM unit name varies by distribution and PHP version, so list available units if the wildcard does not match your system. Look for OOM-killer messages, worker-limit errors, database restarts and filesystem-full errors. Swap cannot compensate for a full disk; reserve capacity for logs, uploads, database growth and backups.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Troubleshoot common failures

swapon: /swapfile has holes

The file was created with unallocated ranges that the kernel will not accept for swap. Remove and recreate it with dd:

sudo swapoff /swapfile 2>/dev/null || true
sudo rm -f /swapfile
sudo dd if=/dev/zero of=/swapfile bs=1M count=1024 status=progress
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile

Do not run the removal sequence if /swapfile is an existing, valuable configuration you have not backed up; confirm the path first. The Ubuntu swapon manual documents the hole restriction.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Invalid argument or a Btrfs-related error

Check the filesystem that contains the file:

findmnt -no FSTYPE,TARGET /

On Btrfs, regular swap files have additional requirements and restrictions. Use the distribution’s documented Btrfs method, a supported swap partition or another filesystem approved by your provider. Do not assume that a command that works on ext4 is valid on Btrfs.

Swap disappears after reboot

Inspect /etc/fstab for the exact line /swapfile none swap sw 0 0, then test it:

sudo swapon -a
sudo swapon --show

Common causes include a typo in the path, a missing swap type, an extra character in the file or a failed activation that was not noticed before reboot. Read the error printed by swapon -a and check the kernel log.

The VPS still OOMs

More swap may only postpone failure. Inspect dmesg, service logs, PHP-FPM worker counts, database usage and plugin behavior. If the machine reaches its physical and swap limits during normal operation, reduce concurrent work, fix the memory consumer or add RAM. Also verify that the disk is not full and that the provider has not imposed a storage or I/O limit.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

mkswap or swapon reports permission problems

Run administrative commands with sudo, confirm that /swapfile is owned by root and has mode 600, and check that no existing mount or file occupies the path. A file on a read-only filesystem cannot be formatted or enabled.

Removing or replacing a swap file

If you need to replace the file, disable it first and remove its persistent entry:

sudo swapoff /swapfile
sudo rm /swapfile

Then delete the corresponding /swapfile none swap sw 0 0 line from /etc/fstab. Verify with sudo swapon --show and free -h. Never delete an active swap file before running swapoff; processes may depend on pages currently stored there. Ensure that the system has enough free RAM before disabling it, particularly on a busy production site.

A safe operating checklist

  • Confirm current swap with sudo swapon --show and free -h.
  • Check free space with df -h / and leave room for WordPress data, logs, databases and backups.
  • Choose a temporary-buffer size based on RAM and observed peaks; 1 GiB is only an example.
  • Create a hole-free file; use dd if fallocate produces a hole-related failure.
  • Set mode 600 before enabling the file.
  • Run mkswap, swapon and both verification commands.
  • Add the exact absolute path to /etc/fstab and test with sudo swapon -a.
  • Change swappiness only after observing the workload; test temporary values first.
  • Investigate PHP, database, plugin and worker behavior when OOM events persist.
  • Use more RAM or a managed WordPress/Linux service when memory demand is consistently above the VPS’s capacity.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.