搬瓦工VPS安装Ubuntu扩大swap分区
最近买了个搬瓦工VPS,用wordpress搭建了一个博客
运行一个星期后,网站打开非常卡顿
root@ubuntu:~# free
total used free shared buff/cache available
Mem: 516324 273876 43020 28544 199428 178336
`Swap: 135164 135164 0`
登录上去一看,512M内存,swap分区只有128M,搬瓦工这是在搞笑吗?
按照老师教过的,swap分区至少是内存的2倍大小
然后我就开始扩大ubuntu的swap分区
根据网上说的,搬瓦工vps中swap是利用swap文件实现的,而不是swap分区,我不信,然后打开了/etc/fstab文件
root@ubuntu:~# cat /fstab
cat: /fstab: No such file or directory
root@ubuntu:~# cat /etc/fstab
# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point> <type> <options> <dump> <pass>
# / was on /dev/sda2 during installation
UUID=880df99e-8f69-4d9d-aa7b-c399b6a7cf73 / ext4 errors=remount-ro,discard,noatime 0 1
# /boot was on /dev/sda1 during installation
UUID=fd5f28c0-5105-449d-a8b7-fd937e80f438 /boot ext3 defaults 0 2
`/swap none swap sw 0 0`
还真有一个swap文件
ls再看看
root@ubuntu:/# ll
total 135265
drwxr-xr-x 23 root root 4096 Jul 2 17:56 ./
drwxr-xr-x 23 root root 4096 Jul 2 17:56 ../
drwxr-xr-x 2 root root 4096 May 23 16:30 bin/
...
`-rw------- 1 root root 138412032 May 21 23:38 swap`
...
网上大神是对的
然后我就关掉所有swap文件和swap分区
root@ubuntu:/# swapoff -a
删除原来的128Mswap文件
root@ubuntu:/# rm swap
在原位置新建1G的空文件,这样等下就不用改fstab文件了
root@ubuntu:/# dd if=/dev/zero of=/swap bs=1M count=1024
1024+0 records in
1024+0 records out
1073741824 bytes (1.1 GB, 1.0 GiB) copied, 2.96506 s, 362 MB/s
将新建的空文件格式化为swap文件
root@ubuntu:/# mkswap /swap
Setting up swapspace version 1, size = 1024 MiB (1073737728 bytes)
no label, UUID=d7638cc0-74d0-4948-ab9c-7898b47c3c5e
修改swap文件为安全的权限
root@ubuntu:/# chmod 0644 /swap
启用swap文件,提示文件繁忙
root@ubuntu:/# swapon /swap
swapon: /swap: insecure permissions 0644, 0600 suggested.
swapon: /swap: swapon failed: Device or resource busy
没事,重启一下就解决了
再进入搬瓦工控制台看,swap变成了1G
再运行1周看看效果如何!