Create swap in Linux VPS
Some time your VPS provider wont create swap by default when they launch a new VPS for you. Swap play important role in cache process.
And technically its a good practice to have swap specially in cases where the RAM is low, small VPS servers for example. Lets get started.
Note: 4G = 4GB(size of the swap), change it to your desired number. If you are already root, remove sudo. swapfile is the name.
sudo fallocate -l 4G /swapfile
ls -lh /swapfile
Give it appropriate permissions:
sudo chmod 600 /swapfile
Now lets create swap:
sudo mkswap /swapfile
Enable swap on swapfile:
sudo swapon /swapfile
Verify that swap is on:
swapon -s free -m
To enable it on each boot, add it to fstab:
sudo vim /etc/fstab
/swapfile swap swap sw 0 0
Below are extra helpful steps which will make the things more smoother:
sudo vim /etc/sysctl.conf
Add the following lines at the end:
vm.swappiness = 10 vm.vfs_cache_pressure = 50