Introduction
This article outlines how you can enable SWAP on your server. The below examples assume you are using CentOS or another RedHat RHEL-based operating system.
cPanel support can not assist or support enabling SWAP on your server. The procedure below is intended to serve as a guide and involves editing sensitive files that are part of your file system configuration. If you are uncomfortable making the changes, we recommend contacting a qualified System Administrator for detailed assistance.
Procedure
For CentOS, AlmaLinux, and RockyLinux:
-
Determine if SWAP is enabled on the server already. The output from the following command can tell you. If SWAP is not enabled, the following command will produce no output:
root@server [~] # swapon -s
[No Output] - If SWAP is already enabled and you are trying to resize the partition, follow the instructions in this companion article instead:
How to resize SWAP on your server - If SWAP is not enabled, we can set it up start to set it up by creating a disk image. We will use an example path for the SWAP disk image. The following is an example of setting up such an image using the 'dd' tool:
dd if=/dev/zero of=/root/cp-swap bs=1M count=2000
- Set permissions on the disk image:
chmod 0600 /root/cp-swap
- Verify that the image was created and has the proper permissions:
root@server [~] # ls -al /root/cp-swap
-rw------- 1 root root 2097152000 Jan 1 00:00 /root/cp-swap - Before we can use it, we must format the disk image to be useable as SWAP:
mkswap /root/cp-swap
- Now we can enable SWAP using the disk image we created:
swapon /root/cp-swap
- We can use the command from Step 1 to verify SWAP is enabled now:
root@server [~] # swapon -s
Filename Type Size Used Priority
/root/cp-swap file 2047996 0 -3 - To make the change permanent, you will need to add the following entry to your server's /etc/fstab file. Please use extreme caution when modifying the /etc/fstab file. It is responsible for the mounting of the partitions on the server. If you are unfamiliar with the file, we would recommend having a qualified System Administrator make the change:
/root/cp-swap swap swap defaults 0 0
Resources
How to check available swap space on your server.