WordPress

[WordPress on VPS] How To Set and Enable Swap Space on Ubuntu

A Swap Space is a space on a hard disk used as the virtual memory extension of a computer’s real memory. Having a swap space enabled allows your computer’s operating system to pretend that you have more RAM than you actually installed, only being used mainly when there is no sufficient space in RAM left to run applications efficiently.

If you VPS is running low on memory or the MySQL server is constantly shutting down, it’s time to look the memory usage and consider to add Swap Space on your VPS if you haven’t.

Check the system for Swap information

sudo swapon --show

If you don’t see anything output back after you run the command, it means that the system doesn’t have any Swap Space available at the moment.

You can also confirm it by

free -h

Before you go ahead, you should also make sure you have enough space left to make the Swap file.

df -h

Steps to create and enable Swap Space

The general rule when it comes to how big the Swap Space should be is to double the amount of RAM you have on your system or at least the same. So, since I have a server that has 512MB or RAM, I will be creating a 1GB file here.

Create a Swap Space

sudo fallocate -1 1G /swapfile

Once succeeded, you can verify the correct amount of space was reserved by

ls -lh /swapfile

You also need to lock the file down so only the users with the root privilege can get access to the contents of the file.

sudo chmod 600 /swapfile

Check again if needed.

Enable the Swap Space

sudo mkswap /swapfile
sudo swapon /swapfile

To verify what the configuration for the Swap Space you just created and enabled, use:

sudo swapon --show
free -h

Make the Swap Space permanent

The last thing to do is to make the newly created Swap Space permanently available even after the reboot. To do so, you need to add the swap space file to the /etc/fstab file. Nano the file and add the following line at the end of the file.

/swapfile none swap sw 0 0

That’s all you need to.

edge

Share
Published by
edge

Recent Posts

Disable Copilot on Windows 11 via Group Policy GPO

If using Copilot right from the Taskbar isn't your thing, you should disable it. Even…

8 months ago

Setting Default Fonts in Word, Excel, Outlook, and PowerPoint via Group Policy

In an environment where standardizing things does matter, setting default fonts in Microsoft Office apps…

8 months ago

Wake-On-LAN (WOL) with Windows and PowerShell

Wake-On-LAN is a networking standard that lets you wake up a computer from either a…

8 months ago

How To Remove Restrictions Set in A Password-Protected PDF File

First of all, this is not to bypass a PDF file that requires a password…

9 months ago

How To Move My Outlook Navigation Bar Back From Left Back To the Bottom

Microsoft has been lurking about the idea of placing the Outlook navigation bar to the…

2 years ago

Headset with Microphone Echoing My Own Voice on Windows, What To Do?

One colleague came up to me the other day asking me to take look at…

2 years ago