This is a very basic guide: creating a Swap file on your CentOS server to act as a backup RAM space. Well, this task is optional but you better create one (if your VPS doesn’t come with Swap yet) to avoid OOM (Out of Memory) error. For your information -in case you didn’t know it yet- a Swap file can be described as a small amount of space created on a servers hard drive (or SSD) to simulate and act as backup RAM.
Using a HDD as RAM is probably not very good idea but it is a good practice especially in the event when your server is out of memory and you couldn’t effort to add more RAM immediately. In the ideal situation using SSD is really recommended.
The simple calculation is:
1024 block size x 512MB = 524288 block size. Now add that to the command:
it simply looks like this
Next, create a swap file area using this command syntax:
pic:
Define the owner of the newly created swap file and swap area then set correct permission on it:
Activate Swap you newly setup using this command:
Finally, issue command below to verify that Swap has been setup and enabled:
screenshot:
The last thing to do is to configure Swap to run and enabled each time your server reboots. Use Nano editor (or vi) to adjust fstab setting:
then add following line at the very bottom (Thanks to Adam):
which in my case it looks like this:
Save changes and exit the editor (in Nano it is Control+O then Control+X).
From here on, each time you check your RAM usage using free -m command, it should display your swap too.
Using a HDD as RAM is probably not very good idea but it is a good practice especially in the event when your server is out of memory and you couldn’t effort to add more RAM immediately. In the ideal situation using SSD is really recommended.
Ingredients of this tutorial
- A server / VPS with enough RAM and HDD or SSD. In this article I use a 512MB cloud server from Atlantic.net. Do not have one? Read my list of recommended VPS providers as well as this list of 20 low end cloud server providers.
- A CentOS Distro installed (I use CentOS 7)
- Putty and basic knowledge on how to use it.
- A computer or laptop to access your server
- A cup of coffee or tea.
The simple calculation is:
1024 block size x 512MB = 524288 block size. Now add that to the command:
1
| dd if = /dev/zero of= /swapfile bs=1024 count=524288 |
Next, create a swap file area using this command syntax:
1
| mkswap /swapfile |
Define the owner of the newly created swap file and swap area then set correct permission on it:
1
2
| chown root:root /swapfile chmod 0600 /swapfile |
1
| swapon /swapfile |
1
| swapon -s |
The last thing to do is to configure Swap to run and enabled each time your server reboots. Use Nano editor (or vi) to adjust fstab setting:
1
| nano /etc/fstab |
1
| /swapfile none swap sw 0 0 |
Save changes and exit the editor (in Nano it is Control+O then Control+X).
From here on, each time you check your RAM usage using free -m command, it should display your swap too.
No comments:
Post a Comment