Guide – Setup a Swap on a CentOS 6.x VPS

One of my sites awhile back started having memory issues, MySQL seemed to be crashing because it would run out of memory whenever it was being hit too hard. Originally I thought it was just a database issue, but the site was really simple — wordpress powered, it shouldn’t be eating through that much ram that fast I thought. Eventually I came across an offhand comment about DigitalOcean not configuring swap memory files on their droplets (by default). Since one of DO’s features is that they host on Solid-State Drives, it means swap memory in comparison is much much faster than on normal style hard drives, and it’s not that uncommon for mysql, specifically, to sometimes need some extra memory now and then.

I then went searching around for how to address setting one up, and came across a few different guides but I wanted to document my procedure in a simple “X-step” guide I can refer myself to later.

Prep

Before we begin, make sure you have sudo or root access to your VPS. Please note that while I use CentOS for my webservers, this same procedure can work for Ubuntu or other distros — setting up swap files is pretty basic as far as linux administration goes.

Note: You’ll need these commands to be available: free, swapon, swapoff, dd, mkswap

» Skip to the abbreviated list of commands

Does your system already have Swap setup?

You can check this by utilizing the free command:

free -m

If your system does not have swap enabled, then you’ll see something like this:

             total       used       free     shared    buffers     cached
Mem:           490        484          6          0          1         17
-/+ buffers/cache:        465         25
Swap:            0          0          0

If you already have a swap setup then you’ll see something like this:

             total       used       free     shared    buffers     cached
Mem:           490        484          6          0          1         17
-/+ buffers/cache:        465         25
Swap:         1499        337       1161

If you’d like to resize a swap file I found the quickest way to do that was to simply create a second swap file in the desired size you want, enable it, then disable the original swap file. This guide, however, is focused solely on creating a new swap file, I may post an addendum later for my procedure to resizing a swap.

Step One – Create the Swap file

Reading the various guides I came across there were multiple places to put a swap file, and a couple of different ways to make it, but generally the idea is to create a file and chmod it to 600. In this guide we’ll be placing our swap file in /var/.

cd /var
touch swap.img
chmod 600 swap.img

Step Two – Sizing

How big of a swap file do you need? Well that’s not really something I can tell you. Generally 1~2x the size of the ram on your server is a good metric, but it can be higher or lower. Generally on servers with lots of ram (4gb+) you may not even want to setup swap memory, and if you do it might be small.

In this case we’ll be making our swap 1gb in size:

dd if=/dev/zero of=/var/swap.img bs=1024k count=1000

Note: This command may take a few moments to complete, be patient.

The number in the command 1024k represents how large you want the file to be. For your server you may want to set it to 512k, 256k, or even 2048k. Be aware that the swap file will count against the total disk space you’ve been allocated. For instance if you have 20gb and you create a 1gb swap file, you will then have “lost” 1gb of space to your swap file resulting in only 19gb available. However losing some diskspace instead of upgrading your droplet for more ram is a pretty minor price to pay, right?

Step Three – Prep the Swap File

In this step we’ll prepare the file for use swap memory by running the mkswap command:

mkswap /var/swap.img

Note: You might get a warning running this command, you can safely ignore it, it’s simply letting you know you shouldn’t overwrite boot sectors and such. It’s more of a “note” then an error/warning.

Step Four – Enable the Swap!

Enabling the swap file is pretty easy:

swapon /var/swap.img

You can also disable it by using the swapoff command like so:

swapoff /var/swap.img

Note: From this point on, your swap memory is configured and running! However to make sure your swap is enabled when your server reboots requires another step.

Step Five – Enabling your swap on boot.

Enabling your swapfile on boot is pretty easy, you just have to add the following to the fstab file, personally I use nano so I would do something like:

 nano /etc/fstab

When opened, add the following at the end of the file:

/var/swap.img           swap                    swap    defaults        0 0

Note: Be very careful editing fstab as it can break thinks if you’re not careful.

When you’ve finished adding the line, save (in nano it’s CTRL+O) and then exit (CTRL+X). Now when your server reboots it will enable the swap automatically!

Optional Step Six: Swappiness

Swappiness tells the system how often to use the swap file. You can just leave this setting alone, however you may wish to configure it yourself. By default my server’s swappiness level was set at 60 (0 being don’t use swap unless absolutely necessary and 100 being use swap all the time even if you don’t really need to).

Check on your system’s swappiness level with:

cat /proc/sys/vm/swappiness

If the number is one you’re happy with, feel free to skip the rest of this step. If you’d like to tweak it, then you can do so by using the following command:

sysctl vm.swappiness=10

The number “10” in the above command corresponds to the level of swappiness you want your system to use. I keep it low personally just so the system doesn’t overuse it. Memory is faster than a swap file, even one on an SSD after all!

Done!

Just the Commands

Step 1)

cd /var
touch swap.img
chmod 600 swap.img

Step 2)

dd if=/dev/zero of=/var/swap.img bs=1024k count=1000

Step 3)

mkswap /var/swap.img

Step 4)

swapon /var/swap.img

Step 5)

nano /etc/fstab

Add the following to the end of the file:

/var/swap.img           swap                    swap    defaults        0 0

Save the file and exist nano/vim/etc…

Done!

Further Reading

I originally moved from Dreamhost to DigitalOcean, my first post on that transition is over yonder if you’d be interested to read about my experience!

Thoughts?