ROR or even simple WordPress project is now require a minimum of 1GB memory of a vps, although some vps start selling 1GB at $5 such as Vultr, Linode
DigitalOcean….. (oh my DO whyy oh why) and amazon lightsail and some other majorities of vps provider still selling 512 at $5
BUT yeah we wont use 512 for production, except development, staging and experiment or weekend projects.
so how do we run the apps with 512… well introducing SWAP
What is Swap?
Swap is an area on a hard drive that has been designated as a place where the operating system can temporarily store data that it can no longer hold in RAM. Basically, this gives you the ability to increase the amount of information that your server can keep in its working “memory”, with some caveats. The swap space on the hard drive will be used mainly when there is no longer sufficient space in RAM to hold in-use application data.
The information written to disk will be significantly slower than information kept in RAM, but the operating system will prefer to keep running application data in memory and use swap for the older data. Overall, having swap space as a fall back for when your system’s RAM is depleted can be a good safety net against out-of-memory exceptions on systems with non-SSD storage available.
Thanks to SSD ??
so i strongly advice don’t do this on production server, be a good man! dont be ?
Ok, lets start
i assume the vps is up and you have already gain your control over your vps on root access
first check if the swap is there
sudo swapon --show
if nothing comes up, then there is no swap config, you’re good to go
next, check if you still have space on the disk, don’t get confidence high before busting your HDD
so everything is good, 2G is okay? kay next
Create the swapfile, you can go to the root folder and create one
cd / sudo fallocate -l 2G /swapfile
check now if its there
ls -lh /swapfile
now enabling the swap, first make it sure only accessible for root
sudo chmod 600 /swapfile
if you ls -lh should return something like this
-rw------- 1 root root 1.0G Apr 25 11:14 /swapfile
and mark the swap
sudo mkswap /swapfile
and activate it
sudo swapon /swapfile
voila! the swap done ✅ but it still available only for current session, if you reboot the machine it might be gone… lets make it permanent on our fstab, back it up first!
sudo cp /etc/fstab /etc/fstab.bak
and put it
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
done ✅ ✅ ✅ ✅
now check it on your htop
should be look like this
there you go the Swp