There will be times where you need to increase the responsiveness of your server to prevent out of memory issues.\u00a0Out of memory issues happen when an application running on your server starts consuming a large amount of memory.<\/strong>\u00a0Swap is designed as\u00a0virtual memory<\/em>, which uses your harddrive to store data that cannot be held in RAM. This tutorial will show you how to create a swap file,\u00a0which should work under Ubuntu, CentOS, and Debian.<\/strong>\u00a0This tutorial is not meant for any Custom ISO, but it is possible to follow along.<\/p>\n To prevent any issues during this tutorial<\/strong>, you will need to run the following to verify that a swap space is currently not active:<\/p>\n After\u00a0running that command<\/strong>\u00a0you should see something similar to this output:<\/p>\n If you see a value of\u00a0 Alternatively, you can run the following command to see if there is a configured swap file:<\/p>\n If you do not see any output from\u00a0 You will need to choose a location for your file.\u00a0In this tutorial<\/strong>, it will be stored at the root of the server.\u00a0We will create a 2GB swap<\/strong>\u00a0file by running the following command:<\/p>\n The\u00a0 Next, verify that the file is located at the root of your AKLWEB Host VPS by running:<\/strong><\/p>\n Proceed if you see the\u00a0 Swap files are not recognized automatically.\u00a0We will need to tell the server how to format the file and enable<\/strong>\u00a0it so it can be used as a valid swap file. As a security measure, update the swapfile permissions to only\u00a0allow R\/W<\/strong>\u00a0for root and no other users. Run:<\/p>\n The permission change can be verified by running the following command:<\/strong><\/p>\n You will see a file display:<\/strong><\/p>\n Next, tell the server to setup the swap file by running:<\/strong><\/p>\n After running it, you will see the following output:<\/strong><\/p>\n If everything is shown as above, you are\u00a0now ready to move<\/strong>\u00a0on to the next step.<\/p>\n Once your file is\u00a0ready to be used as swap<\/strong>, you need to enable it by running:<\/p>\n You can\u00a0verify that the swap file<\/strong>\u00a0is active by running the\u00a0 If\u00a0 By default,<\/strong>\u00a0your server will not automatically enable this new swap file.\u00a0To enable it on boot, you can update the<\/strong>\u00a0 Add the following line at the end of the file:<\/strong><\/p>\n Save and close when you are finished editing the file. We are all done!<\/strong><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"open","ping_status":"closed","template":"","format":"standard","manualknowledgebasecat":[231,233,242,247],"manual_kb_tag":[359],"_links":{"self":[{"href":"https:\/\/support.aklwebhost.com\/wp-json\/wp\/v2\/manual_kb\/2580"}],"collection":[{"href":"https:\/\/support.aklwebhost.com\/wp-json\/wp\/v2\/manual_kb"}],"about":[{"href":"https:\/\/support.aklwebhost.com\/wp-json\/wp\/v2\/types\/manual_kb"}],"author":[{"embeddable":true,"href":"https:\/\/support.aklwebhost.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/support.aklwebhost.com\/wp-json\/wp\/v2\/comments?post=2580"}],"version-history":[{"count":1,"href":"https:\/\/support.aklwebhost.com\/wp-json\/wp\/v2\/manual_kb\/2580\/revisions"}],"predecessor-version":[{"id":2581,"href":"https:\/\/support.aklwebhost.com\/wp-json\/wp\/v2\/manual_kb\/2580\/revisions\/2581"}],"wp:attachment":[{"href":"https:\/\/support.aklwebhost.com\/wp-json\/wp\/v2\/media?parent=2580"}],"wp:term":[{"taxonomy":"manualknowledgebasecat","embeddable":true,"href":"https:\/\/support.aklwebhost.com\/wp-json\/wp\/v2\/manualknowledgebasecat?post=2580"},{"taxonomy":"manual_kb_tag","embeddable":true,"href":"https:\/\/support.aklwebhost.com\/wp-json\/wp\/v2\/manual_kb_tag?post=2580"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}Step 1: Verify that swap does not exist<\/strong><\/h3>\n
free -m\r\n<\/code><\/pre>\n
total used free shared buffers cached\r\nMem: 1840 1614 226 15 36 1340\r\n-\/+ buffers\/cache: 238 1602\r\nSwap: 0 0 0\r\n<\/code><\/pre>\n
0<\/code>\u00a0in the\u00a0
Swap<\/code>\u00a0section, then you can proceed to step 2.<\/p>\n
swapon -s\r\n<\/code><\/pre>\n
swapon<\/code>, then proceed to step 2.<\/p>\n
Step 2: Create swap file<\/strong><\/h3>\n
dd if=\/dev\/zero of=\/swapfile count=2048 bs=1M\r\n<\/code><\/pre>\n
dd<\/code>\u00a0command will produce output in a similar format to:<\/p>\n
2048+0 records in\r\n2048+0 records out\r\n2147483648 bytes (2.1 GB) copied, 10.5356 s, 204 MB\/s\r\n<\/code><\/pre>\n
ls \/ | grep swapfile\r\n<\/code><\/pre>\n
swapfile<\/code>\u00a0file.<\/p>\n
Step 3: Activate the swap file<\/strong><\/h3>\n
chmod 600 \/swapfile\r\n<\/code><\/pre>\n
ls -lh \/swapfile\r\n<\/code><\/pre>\n
-rw------- 1 root root 2.0G Oct 2 18:47 \/swapfile\r\n<\/code><\/pre>\n
mkswap \/swapfile\r\n<\/code><\/pre>\n
Setting up swapspace version 1, size = 2097148 KiB\r\nno label, UUID=ff3fc469-9c4b-4913-b653-ec53d6460d0e\r\n<\/code><\/pre>\n
Step 4: Turn swap on<\/strong><\/h3>\n
swapon \/swapfile\r\n<\/code><\/pre>\n
free<\/code>\u00a0command again.<\/p>\n
free -m\r\n\r\ntotal used free shared buffers cached\r\nMem: 1840 1754 86 16 23 1519\r\n-\/+ buffers\/cache: 210 1630\r\nSwap: 2047 0 2047\r\n<\/code><\/pre>\n
Swap<\/code>\u00a0shows something other than\u00a0
0<\/code>,\u00a0then you have successfully setup swap.<\/strong><\/p>\n
Step 5: Enable swap on reboot<\/strong><\/h3>\n
\/etc\/fstab<\/code>\u00a0file. Any text editor will suffice. In this example, I will be using\u00a0
nano<\/code>.<\/p>\n
nano \/etc\/fstab\r\n<\/code><\/pre>\n
\/swapfile none swap sw 0 0\r\n<\/code><\/pre>\n