SWAP space, also known as virtual memory, is a crucial component of a Linux system that helps manage memory usage efficiently.
On a Hostinger VPS, setting up SWAP can provide additional memory resources and improve the overall performance of your server. In this article, we will guide you through the process of setting up SWAP on your Hostinger VPS.
Prerequisites
Before proceeding with the steps below, please ensure that you have the following:
Basic knowledge of the Linux command line
Step 1 – Check for Existing SWAP Space
Connect to your VPS via SSH using your preferred terminal application and run the following command to check if there is an existing SWAP space:
swapon --show
If the command returns output, you already have SWAP space set up. Skip to Step 4.
Step 2 – Create a SWAP File
Run the following command to create a SWAP file of the desired size (in this example, we'll use 2GB):
fallocate -l 2G /swapfile
Set the appropriate permissions on the file to restrict access:
chmod 600 /swapfile
Format the file as SWAP:
mkswap /swapfile
Step 3 – Enable the SWAP File
Activate the SWAP file by running the following command:
swapon /swapfile
To make the SWAP file persist across reboots, open the /etc/fstab file in a text editor:
nano /etc/fstab
Add the following line at the end of the file:
/swapfile none swap sw 0 0
Save the changes and exit the text editor.
Step 4 – Adjust SWAP Settings (Optional)
By default, Linux manages SWAP usage automatically. However, you can adjust the behavior according to your requirements.
Open the /etc/sysctl.conf file:
nano /etc/sysctl.conf
Add or modify the following line to change the SWAP behavior:
vm.swappiness=10
NOTES
A value of 0 makes the kernel avoid SWAP as much as possible
A value of 100 makes the kernel use SWAP aggressively
Step 5 – Verify SWAP Setup
Run the command below to verify that the SWAP space is active and being utilized:
swapon --show
It should display information about the SWAP file. You can also check SWAP usage using the free command:
free -h
The output will show information about the SWAP space and its usage.
And that's it – you have successfully set up SWAP space on your Hostinger VPS! This additional memory resource can help improve the performance and stability of your server by efficiently managing memory usage 🎉