How to add Swap file on Ubuntu Web Server ?

A tutorial on adding Swap file on Ubuntu based VPS web server. Also adding swappiness to prevent out of memory crashing of website at peak

Adding Swap file to Ubuntu OS based web server eliminate problem of out of memory crashing error. I previously use to get out of memory error for my DigitalOcean VPS web server.

Later I read some article and I finally found solution to eliminate and prevent out of memory problem by adding swap file on to my hard drive.

Here are steps to add Swap file on Ubuntu web-host server:

Check for Swap file on Ubuntu:

You can check whether swap file is present on your server by entering following code in SSH console.

If table is incomplete and have no records then you can confirm that server don’t have any swap file on it.

Check File System:

Since we have confirmed that we don’t have swap file on our server, we can check how much space is available on our server using df command.

Note that Swap file takes 256MB of space on your hard disk. If your use % is less than total available disk size, then continue with setting up swap file.

Create and Enable Swap file on Ubuntu:

Create your swap file using following dd command:

Related:  Enable cURL in PHP for Linux and Windows on XAMPP

swapfile is then name of swap file you created on hard disk. Keyword bs is block size and count is read write rate.

now create Linux swap area by entering following code in console:

Once done you will see some UUID and swapfile size message on console. Make a further move by activation of swap file.

Activate Swap file:

Created swap file can be activated by entering following command line:

Now as you have activated swap file, check and verify swap file using following code in console:

Adding Swap File on Ubuntu

Adding Swap File on Ubuntu

Make Swap File Permanent:

Swap file will last only till you restart you machine. Once your server is rebooted, the swap file is deleted automatically. So to make swap file permanent on server add following lines of code in fstab file.

To open and edit fstab file, enter following line in SSH console:

Now add or paste following line of code in fstab file and save:

Related:  Enable Apache mod_headers and mod_expires modules

Note: Give a TAB spacing between each keyword in above code.

Enable Swappiness:

It is recommended to add Swappiness of file set to be 10 to prevent out of memory crashing so as to act as buffer memory. If you avoid swappiness then it may cause poor performance of server.

It can be enabled by following command:

Finally you have successful added Swap file on Ubuntu OS and enabled swappiness. Now you wont have out of memory server crashing problem on your web-host.

On completion of all the sets re-started Apache server and reboot your machine using following command lines:

Restarting Apache server:

Rebooting Ubuntu machine:

Now your web server is completely enabled by Swap file which wont disable even on restart of server

You may also like...

Leave a Reply

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