I have a virtual private server (VPS) and plans to run a WordPress blog in it. A limited amount of RAM and swap size will be an issue because some software applications like MySQL, Apache, NGINX, PHP, HHVM and Varnish requires more memory to operate.
From my past experience in Ubiquity Hosting and Linode, the pre-allocated swap sizes of 2GB and 1GB VPS are 1GB and 512MB only. After switched to 2GB plan for RamNode VPS, linux swap also provided in the size of 1GB.
Why we need Linux Swap Space ?
To make your server more responsive and prevent crash when it runs out of memory, giving additional linux swap space will help. Linux swap is an area on the storage or hard disk where the operating system can temporarily store data that it no longer can hold in memory. Without the Linux swap, the VPS that runs out of memory can crash or start kill some applications to free up memory. This can cause lose for unsaved data or experience downtime.
However, swapping does have drawbacks. Reading from and writing to swap is slower than using memory. But swapping should take advantages on the SSD server. In the hard disk drive, If VPS started using linux swap space, the VPS performance can slow down significantly.
As a reference for webmasters and system administrators, here are the steps on how to increase the size of the linux swap on CentOS 6 VPS. This steps may also works and tested on CentOS 7 / RHEL 7 and oracle linux 7.
1. Check and verify the allocated disk partition for swap using “fdisl -l” command :
[root@vps ~]# fdisk -l Disk /dev/vda: 53.7 GB, 53687091200 bytes 16 heads, 63 sectors/track, 104025 cylinders Units = cylinders of 1008 * 512 = 516096 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x00059ca1 Device Boot Start End Blocks Id System /dev/vda1 * 3 1018 512000 83 Linux Partition 1 does not end on cylinder boundary. /dev/vda2 1018 3099 1048576 82 Linux swap / Solaris Partition 2 does not end on cylinder boundary. /dev/vda3 3099 104023 50865920 83 Linux Partition 3 does not end on cylinder boundary.
2. From the operating system, verify the swap size from command line utility “swapon -s” and “free -m“. The current swap size is 1GB :
[root@vps ~]# swapon -s Filename Type Size Used Priority /dev/vda2 partition 1048568 0 -1
[root@vps ~]# free -m total used free shared buffers cached Mem: 1877 1624 253 0 210 503 -/+ buffers/cache: 909 967 Swap: 1023 0 1023
3. Start Create the Swap file by determine the size of the new swap file and assign the swap file name. At a shell prompt as root, type the following dd command with count being equal to the desired block size. As an example i will add 2048MB as additional space size and assigned swap file name is /swapfile-ext.
[root@vps ~]# dd if=/dev/zero of=/swapfile-ext bs=1M count=2048 2048+0 records in 2048+0 records out 2147483648 bytes (2.1 GB) copied, 4.05978 s, 529 MB/s
4. Creating a linux swap area with mkswap command :
[root@vps ~]# mkswap /swapfile-ext mkswap: /swapfile-ext: warning: don't erase bootbits sectors on whole disk. Use -f to force. Setting up swapspace version 1, size = 2097148 KiB no label, UUID=cc49d098-52e3-4fdf-8157-c40b23e00eef
5. Assign the correct permission to prevent swap space from world-readable :
[root@vps ~]# chmod 600 /swapfile-ext
6. Activatethe swap file immediately :
[root@vps ~]# swapon /swapfile-ext
7. Verify by viewing the output using “free -m” utility. 1GB plus 2GB equal to 3GB new swap size :
[root@vps ~]# free -m total used free shared buffers cached Mem: 1877 1801 76 0 137 765 -/+ buffers/cache: 897 979 Swap: 3071 0 3071
Verify by viewing the output using “swapon -s” utility
[root@vps ~]# swapon -s Filename Type Size Used Priority /dev/vda2 partition 1048568 556 -1 /swapfile-ext file 2097144 0 -2
8. To enable it at boot time, edit /etc/fstab to include the following entry:
/swapfile-ext swap swap defaults 0 0