Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run Scan×
Skip to content
VGSources
Blog

How to Add Swap Space to a WordPress VPS Safely

A practical Ubuntu/Debian guide to adding swap on a WordPress VPS, with sizing heuristics, secure commands, filesystem caveats, persistence checks and OOM troubleshooting.
Length9 min Posted Quest giverVGSources Team

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.

Add swap as a safety buffer for short memory spikes, not as a substitute for RAM. On an Ubuntu or Debian WordPress VPS, the reliable process is to check existing swap, create a hole-free file, restrict it to mode 600, format and enable it, add an exact /etc/fstab entry, and verify it after activation and reboot.

Swap is disk-backed virtual memory, so it is dramatically slower than physical memory. If the server remains under pressure, investigate PHP-FPM, the database, plugins and workload—or add RAM—instead of continually enlarging swap.

As an Amazon Associate I earn from qualifying purchases.

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

What swap does for a WordPress VPS

Swap is disk storage reserved for pages that Linux cannot keep in RAM. DigitalOcean describes it as storage set aside so the operating system can temporarily hold data that no longer fits in memory. That reserve can keep a small VPS alive during a brief traffic burst, backup, image-processing job or database spike.

The trade-off is latency. RAM access is measured in nanoseconds; a swap-in requires storage I/O and can be thousands of times slower. When active WordPress workers are repeatedly moved between RAM and swap, the site may become sluggish even though the kernel has not killed a process. Treat swap as an emergency cushion that buys time to diagnose the cause, not as extra usable memory for normal operation.

Swap is not a WordPress requirement. Current WordPress guidance recommends PHP 8.3 or newer, MariaDB 10.11 or newer or MySQL 8.0 or newer, HTTPS, and a web server such as Apache or Nginx. None of those requirements is satisfied by adding swap, and swap cannot compensate for an undersized VPS running too many services.

Check whether swap already exists

Do not create a second swap area until you know what is active. Log in with an account that can use sudo and run:

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

Interpret the output

  • sudo swapon --show lists active swap devices and files. If it prints only column headings—or no rows—there is no active swap area.
  • In free -h, the Swap row shows total, used and free swap. A total of 0B confirms that none is active.
  • df -h / shows free space on the filesystem where /swapfile will be created. Leave room for WordPress core, uploads, logs, the database and backups; filling the root filesystem can break the site.

A swap entry can exist in /etc/fstab without currently being active after a failed boot or manual change. The commands above show the running state; later, test the persistent configuration with swapon -a.

Choose a sensible size

There is no WordPress-mandated swap size. The right amount depends on installed RAM, peak workload, disk capacity and whether the server is expected to survive a transient spike or sustained memory pressure.

Starting approach When it fits Qualification
Swap approximately equal to RAM A general starting point for a small VPS that needs a safety margin. DigitalOcean presents RAM-sized swap as a heuristic, not a WordPress rule.
Swap up to roughly twice RAM A low-memory server with occasional bursts and enough free disk. Also described by DigitalOcean as a starting heuristic; it does not make sustained overcommit safe.
More than 4 GiB Only when measurements and workload justify it. DigitalOcean notes that more than 4 GiB is often unnecessary when swap is only a fallback for RAM.
1 GiB example A practical example for a very small VPS with adequate free space. The command below creates 1 GiB; it is an example, not a universal recommendation.

Use the smallest size that gives the operating system breathing room while preserving disk space for the application. A server that continuously consumes swap is still short of RAM or misconfigured. Increasing the file again can postpone an out-of-memory kill while making response times worse.

Swap file or swap partition?

A swap file is usually the least disruptive option on an existing VPS: it can be created without repartitioning or reinstalling the operating system, and its size can be changed later. A dedicated swap partition is a separate block device and can be appropriate when the provider supplies one or when filesystem support for files is problematic, but changing partitions generally requires more planning and may be impossible on a running virtual machine.

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

Regardless of the form, compare these factors before choosing:

  • Filesystem support: regular files must not contain holes; Btrfs has additional restrictions.
  • Disk headroom: the reserved space must remain available alongside site data and backups.
  • Performance: neither form turns storage into RAM; fast local storage reduces, but does not remove, swap latency.
  • Operations: a file is easy to remove or resize, while a partition may be harder to alter after deployment.

Create a swap file on Ubuntu or Debian

The following sequence is suitable for a typical Ubuntu or Debian VPS. Replace the size or path only after checking the filesystem and free space. Keep the commands in order.

1. Create a hole-free file

On filesystems where it produces a valid swap file, fallocate is concise:

sudo fallocate -l 1G /swapfile

Some filesystems or kernels reject files containing holes. When compatibility is uncertain—or when swapon later reports holes—use dd, which writes every block:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
sudo dd if=/dev/zero of=/swapfile bs=1M count=1024 status=progress

Here, 1,024 blocks of 1 MiB create approximately 1 GiB. The command can take time on slower storage; wait for it to finish and check that it reports the expected number of bytes.

2. Restrict access to the file

sudo chmod 600 /swapfile

Swap may contain fragments of passwords, cookies, database records or other process data. Mode 600 allows root to read and write the file while preventing ordinary users from reading it. Verify the permission if you are troubleshooting:

ls -lh /swapfile

The permission string should show read/write access for the owner only (for example, -rw-------).

3. Format it as swap

sudo mkswap /swapfile

mkswap writes the swap signature and reports the usable size. If it warns about an unsuitable file, stop and resolve the filesystem or file-creation issue instead of forcing activation.

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

4. Activate it immediately

sudo swapon /swapfile

Activation changes the running kernel state but is not yet persistent across reboot. Check both views:

sudo swapon --show
free -h

You should see /swapfile in the first command and a non-zero total in the Swap row of the second.

Make the configuration survive a reboot

Add one line to /etc/fstab:

/swapfile none swap sw 0 0

Use an absolute path for a regular-file source. Edit the file with your preferred editor, save it, and then test every swap entry without rebooting:

sudo swapon -a
sudo swapon --show

swapon --all reads swap entries from /etc/fstab. If the test prints an error, fix the line before restarting. A typo, a missing file, an incorrect filesystem type or an extra character can leave the VPS without swap after the next boot.

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

After a maintenance window, reboot if you need to validate the complete boot path. Once the server returns, run:

sudo swapon --show
free -h

Do not consider the setup complete until the file is active after reboot. If it disappeared, inspect the exact /etc/fstab line and run sudo swapon -a manually to capture the error.

Understand and tune swappiness

The kernel’s swappiness value, from 0 to 100, influences how readily it reclaims anonymous memory to swap rather than keeping it in RAM. Debian documentation notes that many distributions default to 60. The value is a policy preference, not a promise that the kernel will wait until RAM reaches a particular percentage.

Free tools Windows power users keep installed

One-click scans. No signup required.

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

Read the current value:

cat /proc/sys/vm/swappiness

Change it temporarily for observation:

sudo sysctl vm.swappiness=10

A lower value can keep application pages in RAM longer, which may help latency on a busy WordPress stack, but it can also increase reclaim pressure and trigger an out-of-memory event sooner during a spike. Do not copy a “magic” value from an unrelated server. Observe page-in activity, request latency, available RAM and swap usage under your real workload. Only after testing should you persist a chosen value in /etc/sysctl.conf, for example:

vm.swappiness=10

Apply a persistent change with sudo sysctl -p and confirm the resulting value. If performance worsens, remove or revise the line and reload the configuration.

Why swap does not fix WordPress memory limits

There are several independent memory ceilings on a WordPress VPS:

  • Physical RAM: the capacity shared by the kernel, web server, PHP-FPM, database, caching services and monitoring tools.
  • PHP limits: WordPress documentation states that the default requested PHP memory is 40 MB for a single site and 64 MB for multisite. The effective limit can also be constrained by the PHP configuration and hosting setup.
  • Worker and process counts: each PHP-FPM worker consumes memory. Too many concurrent workers can exhaust RAM even when each request is individually modest.
  • Database workload: large buffers, complex queries, imports and concurrent connections compete with PHP for the same memory.
  • Plugin and theme behavior: a memory leak or an operation that loads a large catalog, image set or export can overwhelm an otherwise adequate server.

Raising a PHP memory limit may allow one request to allocate more, but WordPress documentation warns that increasing the limit can hide the underlying cause. Swap can keep the kernel from immediately killing a process, yet it cannot make an oversized request efficient. Resolve persistent failures by profiling the request, reviewing recent plugins and themes, adjusting PHP-FPM workers, tuning the database and adding RAM when demand is genuinely sustained.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Diagnose an out-of-memory event after adding swap

If the site still returns 502/503 errors, hangs or loses services, determine whether the kernel or an application component failed.

Check kernel evidence

dmesg | grep -i -E 'out of memory|oom|killed process'

Look for the process the kernel killed and the memory conditions at that moment. On systems where kernel messages are restricted, use the system journal or your distribution’s log viewer with equivalent OOM filtering.

Check service logs and concurrency

  • Review PHP-FPM logs for worker exhaustion, child crashes and slow requests.
  • Check the web server error log for upstream timeouts and refused connections.
  • Inspect database logs for connection limits, failed queries and restarts.
  • Compare PHP-FPM worker counts with the memory footprint of one worker; reducing concurrency can be safer than allowing every request to run at once.
  • Identify scheduled jobs, backups, image conversions and imports that coincide with the failures.

Check swap activity, not just capacity

A non-zero swap total does not prove that the server is healthy. Watch whether swap used grows continuously and whether swap-in activity coincides with latency. If the system spends most of its time moving pages between disk and RAM, adding more swap only creates a larger slow queue. Move to a larger VPS or redesign the workload when pressure is persistent.

Common activation failures and their fixes

swapon: /swapfile has holes

The file was created with holes that the kernel will not accept as swap. Remove the file, recreate it with the dd command shown above, run chmod 600, then repeat mkswap and swapon. Ubuntu’s swapon manual specifically warns about hole-containing files.

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

Invalid argument or a Btrfs error

Verify the filesystem:

findmnt -no FSTYPE /

Btrfs imposes additional swap-file restrictions. Follow the distribution’s documented Btrfs method, or use a supported swap partition or block device supplied by the VPS provider. Do not assume that a command that works on ext4 is valid on Btrfs.

Swap vanishes after reboot

Confirm that /swapfile still exists, inspect the exact /etc/fstab entry, and run sudo swapon -a. The command’s error output usually identifies a path, syntax or filesystem problem. Once corrected, verify with swapon --show after reboot.

The VPS continues to be killed by OOM

Swap is not a cure for sustained overcommit. Use kernel and service logs to identify the largest consumer, reduce memory-heavy plugins or jobs, right-size PHP-FPM and database settings, or provision more RAM. If you cannot safely administer these settings, a managed WordPress or managed Linux service can provide that operational layer, but it does not change the underlying resource requirement.

Operational checklist

  1. Record the current output of sudo swapon --show, free -h and df -h /.
  2. Choose a size based on RAM, observed peaks and remaining disk space; use the RAM-sized or double-RAM heuristics only as starting points.
  3. Confirm filesystem support, especially on Btrfs.
  4. Create a hole-free file, using dd when fallocate is not known to be safe.
  5. Set mode 600 before writing any swap signature.
  6. Run mkswap, activate with swapon, and verify both swapon --show and free -h.
  7. Add /swapfile none swap sw 0 0 to /etc/fstab and test with sudo swapon -a.
  8. Reboot during a planned window and verify that swap returns automatically.
  9. Monitor latency, RAM, swap used and OOM logs; tune swappiness only after observing the workload.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

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

More quests from Patch Notes

  1. How to Set Up a RedM (RDR2) Server in 2026: License Key, txAdmin and server.cfg, Step by StepBlog11min
  2. Best RedM (RDR2) Server Hosting in 2026: Comparing Four Hosts on Slots, Memory and PriceBlog10min
  3. How to Host a Mindustry Server in 2026: server-release.jar, Port 6567 and the Commands That MatterBlog7min
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.