Swap file – Beginners guide to Digital Ocean

Ernest Marcinko Hosting, Tutorials 3 Comments

If you reached this step, your server is up and running flawlessly. However after gaining some traffic it will run out of memory and some of the processes will fail. To avoid that, we need a swap file.

If you need more information I recommend reading the original swap file tutorial on digital ocean. This article is basically a shorter version of that.

Creating the swap file

Before creating a swap file, we need to figure out it's size. The proper size of the swap file should be the amount of memory your droplet has. If you created a 1 core 1 gigs of ram droplet, you should create a swap file of 1 gigabytes. To do that, use the following command:

sudo fallocate -l 1G /swapfile

You can change the 1G to the desired amount.

To make this swapfile usable, you need to change it's permissions:

sudo chmod 600 /swapfile

Nice. Now let's tell the system to use this file as a swap file:

sudo mkswap /swapfile

It should print out some information about the allocated memory and stuff. Now, let's turn on swapping:

sudo swapon /swapfile

Making the swap file permanent

Right now swapping is enabled, but after rebooting it's not. We need to tell the system to use this swap file after rebooting.

Open up the fstab file in nano:

sudo nano /etc/fstab

Go to the end of the file and add this line:

/swapfile   none    swap    sw    0   0

Save it and it's done. Now your system has a fully functional swap file.

Optional steps

There are two more options that can fine tune your swap file, you can read more about them on the swap file tutorial on digital oceans website.

Swappiness

The swappiness configures how often will the server change the data between the swap file and the memory. It's a value between 0-100. On values close to 0 the server will try to fill the memory first before using the swap, with values around 100 it's the other way around. For general use web servers 10 is a great value. Usually that's the default.

But to make sure you can check it by running this command:

cat /proc/sys/vm/swappiness

This should return the value. To change the value permanently, let's open up the file:

sudo nano /etc/sysctl.conf

and add this line at the bottom of the file:

vm.swappiness=10

Save the file and reboot the droplet completely:

sudo shutdown -r now

This will terminate your current connection as well.

VFS Cache pressure

By default ubuntu tries to cache some of the inode information – access data of your file system. Operations on the inode table are resource heavy, so caching it is a good option for most cases.

There is an option called vfs_cache_pressure which configures the caching other data over the inode table. It's value is usually set to 100. For better performance we can try to change it to 50, which will prioritize caching of the inode table.

To change that open up the sysctl.conf file:

sudo nano /etc/sysctl.conf

and to the end of that file put this line:

vm.vfs_cache_pressure = 50

Save the file and reboot the droplet completely:

sudo shutdown -r now

This will terminate your current connection as well.

Chapters

<< Chapter #7

Ernest Marcinko

WordPress developer, javascript enthusiast, photoshop clicker and the founder of wp-dreams.com

Stay connected

Related Posts
Filter by
Post Page
Tutorials Wordpress Hosting Blog Testing
Sort by

Saving space automatically – Beginners guide to Digital Ocean

After a while, you will notice an exponential growth of used space, without even copying anything to it. How
2016-05-06 09:24:05
admin

18

Mailing – Beginners guide to Digital Ocean

Sending mails properly is probably the hardest task to achieve with Digital Ocean. I will present two ways of doing
2015-01-20 21:38:53
admin

18

FTP & SFTP – Beginners guide to Digital Ocean

So far we have a running web server with a working database, but we don't have FTP access. SFTP However
2015-01-16 17:08:19
admin

18

Virtual Hosts - Beginners guide to Digital Ocean

By now we have a working private server with very basic features and a demo user. By default your website is data is
2015-01-15 19:23:28
admin

18

From XAMPP to Vagrant WordPress

2016-01-05 19:58:17
admin

8

Comments 3

  1. steve

    Hi Earnest,I followed your guide when setting up my server and it was really helpful, only bit I added not on here is webmin for users who don’t like working from terminal and would like from a familiar dash like cpanel.All in all,great post!

Leave a Reply

Your email address will not be published. Required fields are marked *