Halloween DealsAmazon USHalloween week deals: useful picks before month-endAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix NowHalloween DealsAmazon USOctober picks are moving - compare before checkoutAmazon US: current deals, useful picks and tech finds.See Picks×
FREEDOM251

Freedom251 › Blog › Debian

How to Add Swap Space to a WordPress VPS (Ubuntu and Debian)

By Freedom251 Team · · 9 min read

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

You can add swap to most WordPress VPSs by creating a protected file, formatting it with mkswap, enabling it with swapon, and adding an /etc/fstab entry so it returns after reboot. A 1 GiB file is a reasonable example for a small server, but the right size depends on your RAM, workload, and free disk. Swap is a slower, disk-backed safety buffer for short memory spikes; it does not replace adequate RAM or fix a continuously overloaded WordPress stack.

What swap does for a WordPress VPS

Swap is disk space reserved for virtual memory. When Linux cannot keep every inactive memory page in RAM, it can move some pages to the swap area and reclaim RAM for active work. This can keep a web server responsive during a short-lived spike, such as a backup, image conversion, plugin update, traffic burst, or simultaneous PHP workers.

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.

Storage access has substantially higher latency than RAM. A server that is constantly moving pages between RAM and swap can become slow before it eventually runs out of memory. Treat swap as an emergency margin, not as a way to turn a small VPS into a larger one. Sustained memory pressure still calls for fewer or better-configured services, a review of plugins and PHP workers, database tuning, or additional RAM.

Check the VPS before creating swap

Do not create a second swap area blindly. Log in through SSH with an account that can use sudo, and check what is already active, how much disk is available, and whether the root filesystem can hold the file.

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

sudo swapon --show lists active swap devices and files. If it prints no rows and the Swap line in free -h shows 0B, no active swap is configured. The df result tells you whether the chosen size would leave enough room for WordPress core files, uploads, logs, database files, temporary files, and backups. A full root filesystem can break services even when swap itself is configured correctly.

  • Confirm that you have root or sudo access and a recovery route through your VPS provider.
  • Check that the server has enough free disk for the swap file plus normal operating headroom.
  • Identify the filesystem mounted at the intended path. Ordinary Linux filesystems generally support swap files, while Btrfs has additional restrictions.
  • If the server is already failing from memory pressure, record current symptoms and service logs before changing settings. That gives you a baseline for judging whether the change helped.

Choose a sensible swap size

WordPress does not mandate a swap size. A useful starting heuristic is swap equal to the VPS RAM, or up to twice the RAM on a small server that needs a larger safety margin. When swap is only a fallback, more than 4 GiB is often unnecessary; allocating a huge file can consume disk that WordPress and its database need more urgently. The final choice must account for your workload and available storage.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Situation Practical starting point Qualification
Small VPS with no active swap 1 GiB example This is an example, not a WordPress requirement; verify free disk and memory usage first.
Swap as a general RAM fallback About the same size as installed RAM A heuristic described in Linux hosting guidance; observe actual pressure rather than assuming it is optimal.
Very small-memory server with occasional spikes Up to roughly twice installed RAM Only if disk capacity and workload justify it; larger swap does not provide RAM-level performance.
More than 4 GiB requested Usually reconsider the design Large swap areas are often unnecessary for a fallback and may hide a need for more RAM or service tuning.

For example, a 2 GiB VPS might start with a 1 or 2 GiB file, while a 4 GiB VPS might use 2 to 4 GiB. Those are planning examples, not measured requirements. Keep enough free space for growth, backups, and database maintenance; do not allocate nearly all remaining disk to swap.

Add a swap file on Ubuntu or Debian

1. Create the file without filesystem surprises

On filesystems where it produces a valid, hole-free file, fallocate is quick:

sudo fallocate -l 1G /swapfile

Some filesystems or kernel combinations reject swap files containing holes. Ubuntu’s swapon documentation also notes special restrictions for Btrfs. If you see a holes error, or if you are unsure whether fallocate creates a suitable file on this filesystem, use dd to write every block explicitly:

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

In this command, count=1024 with a 1 MiB block size creates approximately 1 GiB. Change the count only after deciding on a different size. The command can take longer because it writes the entire file, but the resulting file has no sparse holes.

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.

2. Restrict access to the file

sudo chmod 600 /swapfile

Mode 600 allows only root to read or write the file. This matters because swap can contain pages originating from processes and should not be readable by ordinary users.

3. Format and enable swap

sudo mkswap /swapfile
sudo swapon /swapfile

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

4. Verify the live configuration

sudo swapon --show
free -h

You should see /swapfile in the first command and a non-zero Swap total in the second. The used amount may remain near zero until the kernel has a reason to move pages; that is normal and does not mean activation failed.

Make swap survive a reboot

Append this exact line to /etc/fstab:

/swapfile none swap sw 0 0

Use an absolute path for a regular-file swap source. After saving the file, ask Linux to process all swap entries and check for errors:

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

If swapon -a returns an error, fix the /etc/fstab entry before rebooting. A typo in the path, a missing swap signature, an incorrect option, or a file that still contains holes can prevent activation. Once the command succeeds, reboot during a maintenance window and run the two verification commands again. This confirms that persistence works rather than merely assuming it.

Check and tune Linux swappiness carefully

Swappiness is a kernel setting from 0 to 100 that influences how readily Linux considers moving memory pages to swap. Many distributions use a default around 60, but the best value depends on the workload, storage latency, and available RAM. A lower value is not automatically better: avoiding swap too aggressively can leave less room for filesystem cache or cause an abrupt out-of-memory event.

See the current value with:

cat /proc/sys/vm/swappiness

For a temporary test, set a value such as 10 without editing configuration files:

sudo sysctl vm.swappiness=10

Observe request latency, RAM availability, swap-in activity, and service logs while the normal workload runs. If the result is beneficial and repeatable, persist the selected value in /etc/sysctl.conf; otherwise, revert to the distribution default. Do not copy a “magic” swappiness value from an unrelated server. A database-heavy site, a busy PHP-FPM pool, and a lightly used blog can react very differently.

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.

Swap file or swap partition?

Characteristic Swap file Swap partition
Changing size later Usually easier: disable it, recreate or resize it, then enable it. Normally requires repartitioning or provisioning another device.
Provisioning Can be added to an existing mounted filesystem without reinstalling the VPS. Must be allocated as a separate block-device area, often at installation or through provider controls.
Filesystem compatibility Must be a valid, hole-free file; Btrfs has extra rules. A supported partition avoids file-hole issues, but still depends on the kernel and device being available.
Disk planning Consumes ordinary filesystem space that is also needed by WordPress and backups. Reserves a fixed area outside the filesystem.
Best fit for this procedure Most existing Ubuntu or Debian VPSs where you need a reversible change. Servers provisioned with a dedicated swap partition or filesystems where a swap file is unsupported.

If a Btrfs-hosted VPS rejects the file with Invalid argument or a filesystem-specific error, consult the distribution’s documented Btrfs swap-file method. If that method is unavailable, use a supported swap partition or another block device instead of repeatedly retrying the same file.

How swap relates to WordPress and PHP memory

System swap and PHP’s memory limit are separate controls. Current WordPress requirements recommend PHP 8.3 or newer, MariaDB 10.11 or newer or MySQL 8.0 or newer, HTTPS, and an Apache or Nginx web server. Swap is not listed as a WordPress requirement; it is an operating-system facility beneath WordPress.

WordPress documentation identifies a default requested PHP memory limit of 40 MB for a single-site installation and 64 MB for multisite. The effective limit can be changed by PHP configuration, the hosting environment, or WordPress constants. Raising it may allow one request to use more memory, but the documentation warns that doing so can hide the underlying cause. A higher PHP limit multiplied across many PHP-FPM workers can make a VPS run out of RAM faster.

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

When a site has recurring out-of-memory events, examine the whole stack rather than enlarging swap alone:

  • Check PHP-FPM worker counts, process limits, and whether each worker’s real memory use fits within available RAM.
  • Review plugins, themes, scheduled tasks, import jobs, image processing, and backup jobs for spikes or leaks.
  • Inspect database activity, slow queries, buffer settings, and concurrent connections.
  • Confirm that web-server and PHP versions meet WordPress’s current requirements.
  • Compare peak memory demand with installed RAM. If pressure is sustained, adding RAM or moving to a larger VPS is more reliable than adding progressively more swap.
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 is sparse or otherwise unsuitable. Turn off any partial activation, remove the file, recreate it with the dd command shown above, apply mode 600, run mkswap, and activate it again. Do not assume that a successful fallocate command means the file is valid for swap.

Invalid argument or a Btrfs error

Verify the filesystem type and its swap-file support. Btrfs imposes additional requirements, and some combinations of kernel and filesystem features reject ordinary swap files. Use the distribution’s supported Btrfs procedure or provision a swap partition/block device. Changing the file size or repeating swapon without addressing filesystem support will not solve this class of error.

Swap works now but disappears after reboot

Check that /etc/fstab contains exactly /swapfile none swap sw 0 0, that the path matches the actual file, and that the file still exists after boot. Run sudo swapon -a manually and read its error output. Then confirm with sudo swapon --show. If the file is on a mount that is not available when swap activation runs, place it on a reliably mounted filesystem or use a supported partition.

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

The VPS still reports out-of-memory kills

More swap can postpone an OOM event but cannot cure a workload whose demand remains above the server’s capacity. Inspect the kernel log with dmesg, web-server and PHP-FPM logs, database logs, worker counts, and scheduled jobs. Identify which process is growing or spawning too many workers, reduce the load, and retest. If normal peak demand still exceeds physical RAM, increase RAM or reduce the number of services sharing the VPS.

Performance became erratic after enabling swap

Look for sustained swap-in and swap-out activity, rising request latency, and low available RAM. Check the current swappiness and test a conservative temporary value only while observing the workload. If the server spends its time paging, treat that as evidence of insufficient RAM or oversized service concurrency, not as a reason to create an even larger swap file.

Verify and monitor the result

Activation is only the first check. After enabling swap and again after a reboot, verify both the configured area and the system’s memory view:

sudo swapon --show
free -h

During a representative traffic period, watch available RAM, swap used, PHP-FPM process counts, database memory, and application latency. A small amount of swap use during a transient spike can be acceptable. Continuously increasing swap use, frequent paging, or a sharp slowdown indicates that the underlying workload needs attention.

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

Keep an eye on disk usage as well. A swap file occupies real storage even when its used counter is low, and WordPress uploads, logs, database growth, and backups continue to consume the same volume. Include the file in your server inventory and document the /etc/fstab entry so another administrator knows why it exists.

A practical decision checklist

  • Existing swap checked with swapon --show and free -h.
  • Free disk confirmed with df -h /, while reserving room for WordPress, databases, logs, and backups.
  • Size selected from RAM, workload, and disk capacity rather than a WordPress rule.
  • File created with fallocate only where valid, or with dd when hole-free allocation is required.
  • Permissions set to 600 before activation.
  • mkswap, swapon, and verification completed successfully.
  • The exact /etc/fstab line added and tested with swapon -a.
  • Swappiness changed only after observing the workload.
  • Persistent OOM symptoms investigated at the PHP, database, plugin, and RAM levels.

This process gives a WordPress VPS a controlled buffer for temporary memory spikes while keeping the important distinction clear: swap improves resilience during brief pressure, whereas adequate RAM and right-sized services determine sustained performance.

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.

DebianLinuxserver administrationSwapUbuntuVPSWordPress

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.

Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Crashes, No Sound, or Screen Glitches?Free driver scan

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.