Create swap on CentOS 7

1. Check if the server has swap configured

swapon -s

If nothing is shown, that means no swap is enabled.

free -m

If swap is 0, that means no swap is enabled.

2. Create a swap file

sudo fallocate -l 4G /swapfile

3. Change file permission to make sure the swap file can only be accessed by root

sudo chmod 600 /swapfile

4. Setup swap

sudo mkswap /swapfile

5. Enable swap

sudo swapon /swapfile

6. Verify swap

swapon -s
free -m

7. Make the swap File Permanent

sudo vim /etc/fstab
#add this line to the bottom
/swapfile swap swap sw 0 0