There will be times when you need to increase the responsiveness of your server to prevent out-of-memory issues. Memory issues happen when an application running on your server starts consuming a large amount of memory. Swap is designed as virtual memory, which uses your hard drive to store data that cannot be held in RAM. This tutorial will show you how to create a swap file, which should work under Ubuntu, CentOS, and Debian. This tutorial is not meant for any Custom ISO, but it is possible to follow along.
To prevent any issues during this tutorial, you will need to run the following to verify that a swap space is currently not active:
free -mAfter running that command, you should see something similar to this output:
total used free shared buffers cached
Mem: 1840 1614 226 15 36 1340
-/+ buffers/cache: 238 1602
Swap: 0 0 0If you see a value of 0 in the Swap section, then you can proceed to step 2.
Alternatively, you can run the following command to see if there is a configured swap file:
swapon -sIf you do not see any swapon output, proceed to step 2.
You will need to choose a location for your file. In this tutorial, it will be stored at the server's root. We will create a 2GB swap file by running the following command:
dd if=/dev/zero of=/swapfile count=2048 bs=1MThe dd The command will produce output in a similar format to:
2048+0 records in
2048+0 records out
2147483648 bytes (2.1 GB) copied, 10.5356 s, 204 MB/sNext, verify that the file is located at the root of your AKLWEB Host VPS by running:
ls / | grep swapfileProceed if you see the swapfile file.
Swap files are not recognized automatically. We will need to tell the server how to format the file and enable it so it can be used as a valid swap file. As a security measure, update the swapfile permissions to only allow R/W for root and no other users. Run:
chmod 600 /swapfileThe permission change can be verified by running the following command:
ls -lh /swapfileYou will see a file display:
-rw------- 1 root root 2.0G Oct 2 18:47 /swapfileNext, tell the server to set up the swap file by running:
mkswap /swapfileAfter running it, you will see the following output:
Setting up swapspace version 1, size = 2097148 KiB
no label, UUID=ff3fc469-9c4b-4913-b653-ec53d6460d0eIf everything is shown as above, you are now ready to move on to the next step.
Once your file is ready to be used as swap, you need to enable it by running:
swapon /swapfileYou can verify that the swap file is active by running the free command again.
free -m
total used free shared buffers cached
Mem: 1840 1754 86 16 23 1519
-/+ buffers/cache: 210 1630
Swap: 2047 0 2047If Swap shows something other than 0, then you have successfully set up swap.
By default, your server will not automatically enable this new swap file. To enable it on boot, you can update the /etc/fstab file. Any text editor will suffice. In this example, I will be using nano.
nano /etc/fstabAdd the following line at the end of the file:
/swapfile none swap sw 0 0Save and close when you are finished editing the file. We are all done!