Advertisement
niammuddin

bikin swap di vps ubuntu

Jan 5th, 2015
337
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.74 KB | None | 0 0
  1. Add permanent swap on your ubuntu 14.04 Digital Ocean Droplet and vps
  2. What is swap area?
  3.  
  4. A swap is space allocated in hard drive where your OS can store files in temporary basis when RAM is full. Creating a swap area in your VPS can boost speed to a certain extent. If your VPS or droplet (as digital ocean calls it) is suffering memory limit, in this case, adding swap could boost performance of your system. (system=droplet=vps)
  5. How to add swap area in your VPS?
  6.  
  7. Adding swap is not that hard. Follow the instructions below to add swap area permanently in your digital ocean droplet or any other vps with ubuntu 14.04. You will neet root access ofcourse. My current configuration has 20 Gb HDD and 512 MB RAM and I'm planning to add 1GB swap. Get dirt cheap vps for yourself.
  8.  
  9. Step one: Check is swap has already been allocated or not. To run the check follow the command below:
  10.  
  11. free -m
  12.  
  13. The command should give the result below if swap has not been allocated in your droplet.
  14.  
  15. total used free shared buffers cached
  16. Mem: 490 286 204 0 11 234
  17. -/+ buffers/cache: 40 449
  18. Swap: 0 0 0
  19.  
  20. Here we can clearly see that the total swap allocated is 0, which means, no swap allocated. Now we can proceed to next step.
  21.  
  22. Step two: Since we are working on a VPS, its not always possible to create a separate partition dedicated for swap. In this case, we can create a swap file in existing drive. Before that, lets get information on our current disk usage with the commands below.
  23.  
  24. df -h
  25.  
  26. This command should give us the information on our current disk usage, which should look like below.(this will be different in each setup)
  27.  
  28. Filesystem Size Used Avail Use% Mounted on
  29. /dev/vda1 20G 1.6G 18G 9% /
  30. none 4.0K 0 4.0K 0% /sys/fs/cgroup
  31. udev 235M 4.0K 235M 1% /dev
  32. tmpfs 50M 328K 49M 1% /run
  33. none 5.0M 0 5.0M 0% /run/lock
  34. none 246M 0 246M 0% /run/shm
  35. none 100M 0 100M 0% /run/user
  36.  
  37. From the information above, we now know that our main partition /dev/vda1 has 20G of total space and 18G is available and we will create swap in here.
  38. Step 3: Now we are ready to create swap file. There are few methods to create a swap file, in our case we will use the fastast method. Follow the command below to create swap file.
  39.  
  40. sudo fallocate -l 1G /swapfile
  41.  
  42. In above command we can see that, we have allocated a file of 1GB as "swapfile" where we will let ubuntu 14.04 store required information as required. This will prompt immediately and we can confirm this with following command:
  43.  
  44. ls -lh /swapfile
  45.  
  46. This will return us the details on swap file that we just created as below:
  47.  
  48. -rw-r--r-- 1 root root 1.0G Aug 12 00:06 /swapfile
  49.  
  50. Now, that we have created and confirmed our swap file. Its time for us to enable it.
  51. Step 4: In order to enable swap file, we have to first configure permissions on the file. We can to that with the command below:
  52.  
  53. sudo chmod 600 /swapfile
  54.  
  55. and again to confirm if the permissions have been configured properly follow the commands below.
  56.  
  57. ls -lh /swapfile
  58.  
  59. and this should return the updated information on swap file as follows:
  60.  
  61. -rw------- 1 root root 1.0G Aug 12 00:06 /swapfile
  62.  
  63. Now that our swap file has been created and permission has been updated to make it secure, we can tell system to set it up as swap file with the commands below:
  64.  
  65. sudo mkswap /swapfile
  66.  
  67. and this should give us information as below:
  68.  
  69. Setting up swapspace version 1, size = 1048572 KiB
  70. no label, UUID=1118d955-c1cb-4106-82bf-8dfc46ea3b58
  71.  
  72. Now, we have everything ready and its time to enable the swap file. For this, use the command below:
  73.  
  74. sudo swapon /swapfile
  75.  
  76. We have successfully created, configured and enabled our swap file. To confirm this we can run the command below and see the updated information with command below:
  77.  
  78. free -m
  79.  
  80. result:
  81.  
  82. total used free shared buffers cached
  83. Mem: 490 286 203 0 11 234
  84. -/+ buffers/cache: 40 449
  85. Swap: 1023 0 1023
  86.  
  87. Here we can see that our swap file has been created and is being recognized by our system. But it is still not permanent. If we restart our Droplet, our system will loose its swap configuration. In order to make it permanent we have to tweak few things. We will cover this in our next step
  88. Step 5: In order to make our swap file permanent, we need to add few information to "fstab" file. In order to edit fstab file, follow command:
  89.  
  90. sudo nano /etc/fstab
  91.  
  92. here, add the line below at the bottom of the fstab file.
  93.  
  94. /swapfile none swap sw 0 0
  95.  
  96. and update the file. This will make sure that Ubuntu 14.04 will use swapfile every time it boots.
  97. Step 6: Now since everything is properly configured, its time to make most out of the swap file. In order to do that, we need to configure swappiness. Swappiness is a parameter that tells OS how often it can use swap to store data out of RAM. The value can be anything between 0 to 100. when used 0, swap file will be used only when absolutely necessary and when used 100, just the opposite. To check the current swappiness value you can run cat /proc/sys/vm/swappiness and it will return the current swappiness value. Since we are using a VPS, we will use value of 10 as swappiness. To do that, we can follow the command below.
  98.  
  99. sudo sysctl vm.swappiness=10
  100.  
  101. This will update the swappiness value to 10 and to make it permanent, we can follow the commands below:
  102.  
  103. sudo nano /etc/sysctl.conf
  104.  
  105. And add the line vm.swappiness=10 at the bottom of the page and save and close the file when done.
  106. Step 7: Now, the last step is to configure vfs_cache_pressur. Basically vfs_cache_pressure is a parameter that allows system to choose how much it can store inode and dentry information over other data. To check the current value, you can use cat /proc/sys/vm/vfs_cache_pressure. To assign your value(in our case 50), follow the command below:
  107.  
  108. sudo sysctl vm.vfs_cache_pressure=50
  109.  
  110. and to make it permament, follow the command below:
  111.  
  112. sudo nano /etc/sysctl.conf
  113.  
  114. and add the line vm.vfs_cache_pressure = 50 at the bottom and save and close.
  115.  
  116. Now, we have configured everything correctly and we have a swap on our system that is permanent. You can reboot once and check it with the command below to see if swap is being registered or not.
  117.  
  118. sudo swapon -s
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement