使用内存硬盘(tmpfs)来加速你的网站

介绍

博客迁移完腾讯云以后, 又配置好了ssl,一直在调优PHP的性能,中午调整了半天fpm和opcache, 晚上又突然想起来我之前在某个大会上分享过的使用tmpfs(把内存当成硬盘)来加速网站的做法。
重要的事情要说三遍, 开始之前,对你对 nginx root 目录要备份, 备份, 备份!

腾讯云这台机器是ubuntu的,版本是:

$ cat /etc/issue
Ubuntu 18.04.4 LTS \n \l

首先,我们创建一个目录,比如/ramdisk,然后使用tmpfs命令挂载一部分内存当作硬盘:

mount -t tmpfs -o size=256M tmpfs /ramdisk

此处我挂载了最大256M的内存到/tmp节点,值得注意的是系统不会一下子就会把256M内存占用,而是实际用多少占多少,最大不超过256M。

可以用df来确认是否成功操作:

$ df -h
Filesystem Size Used Avail Use% Mounted on
......
tmpfs 256M 0 256M 0% /ramdisk

接下来我们需要用到一个今天才发现的神器(以前还要自己写corntab脚本来做同步:<),Anything-sync-daemon, 它可以自动的把一个指定目录同步到tmpfs,并且能设置按时,或者系统启动时来自动同步,有了个这个工具,这一切都会变得非常简单。

首先下载asd(Anything-sync-daemon):

wget https://github.com/graysky2/anything-sync-daemon/archive/master.zip

解压缩之后,执行安装:

make install-systemd-all

(这里有个问题,默认的它会把systemd服务安装到/usr/lib/systemd/system, 但不知道为啥在我的这个系统不systemd找不到这个服务,我通过把它的几个服务脚本拷贝到/lib/systemd/system解决)

mv asd-resync.service  asd-resync.timer  asd.service /lib/systemd/system

然后通知 systemd:

systemctl daemon-reload

我的博客的nginx的root是/home/huixinchen/www/htdocs下, 我们现在希望是这个目录能自动同步到tmpfs目录,也就是/ramdisk, 于是我们修改asd.conf文件, 增加:

WHATTOSYNC=('/home/huixinchen/www/htdocs/')      //要同步的目录
VOLATILE="/ramdisk"       // tmpfs目录

你也可以同步多个文件,只需要逗号分隔写在WHATTOSYNC数组里即可,比如:

WHATTOSYNC=('/home/huixinchen/www/htdocs/',/home/huixinchen/local/xxxx’) //要同步的目录

然后我们设置每天同步一次tmpfs上的变更内容到硬盘,编辑/lib/systemd/system/asd-resync.timer,

[Unit]
Description=Timer for Anything-sync-daemon - 1Hour
PartOf=asd-resync.service asd.service
[Timer]
OnUnitActiveSec=24h

然后我们用asd p检查下:

$ asd p
Anything-sync-daemon v5.85 on Ubuntu 18.04.4 LTS

Daemon pid file is not present.
Resync cronjob is not present.
Overlayfs technology is currently inactive.

Asd will manage the following per /etc/asd.conf settings:
owner/group id:     huixinchen/1000
target to manage:   /home/huixinchen/www/htdocs
sync target:        /home/huixinchen/www/.htdocs-backup_asd
tmpfs target:       /ramdisk/asd-huixinchen/home/huixinchen/www/htdocs
dir size:           237M
recovery dirs:      none

asd 会把我的 /home/huixinchen/www/htdocs 目录同步到 /ramdisk/asd-huixinchen/home/huixinchen/www/htdocs,
并且会把tmpfs上的更新内容按照时间写回到

/home/huixinchen/www/.htdocs-backup_asd

当我们停止asd的服务的时候,asd会把.htdocs-backup_asd在mv成htdocs,这样就不用担心你的内容会因为服务器突然断电丢失了.
现在,让我们启动asd:

service asd start

现在/home/huixinchen/www/htdocs就会被复制到tmpfs, 并且软链接过去,也就是说nginx我们根本不需要修改, 只需要重启一下fpm, 重置一下opcache的cache即可:

$ ll
total 0K
lrwxrwxrwx 1 huixinchen huixinchen 50 Feb 15 22:27 htdocs -> /ramdisk/asd-huixinchen/home/huixinchen/www/htdocs/

重启完 fpm,一切works out of the box!

PS:关于Asd更多的信息,可以参考:Anything-sync-daemon

https://wiki.archlinux.org/index.php/anything-sync-daemon

来源:https://www.laruence.com/2020/02/15/4982.html,作者:laruence

参考链接 :

https://mp.weixin.qq.com/s/O-dro7GDbCtrnLLicoIc1A

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值