惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Cloudbric
Cloudbric
E
Exploit-DB.com RSS Feed
SecWiki News
SecWiki News
Forbes - Security
Forbes - Security
N
News | PayPal Newsroom
S
Security @ Cisco Blogs
Schneier on Security
Schneier on Security
V
V2EX - 技术
S
Secure Thoughts
W
WeLiveSecurity
Google DeepMind News
Google DeepMind News
C
CERT Recently Published Vulnerability Notes
NISL@THU
NISL@THU
S
Securelist
S
Security Archives - TechRepublic
Know Your Adversary
Know Your Adversary
V
Vulnerabilities – Threatpost
Security Latest
Security Latest
Recent Commits to openclaw:main
Recent Commits to openclaw:main
G
GRAHAM CLULEY
H
Hacker News: Front Page
Microsoft Azure Blog
Microsoft Azure Blog
I
Intezer
Google Online Security Blog
Google Online Security Blog
美团技术团队
阮一峰的网络日志
阮一峰的网络日志
T
The Exploit Database - CXSecurity.com
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Webroot Blog
Webroot Blog
Jina AI
Jina AI
Engineering at Meta
Engineering at Meta
P
Proofpoint News Feed
The Cloudflare Blog
I
InfoQ
L
LangChain Blog
U
Unit 42
P
Proofpoint News Feed
S
Schneier on Security
S
Security Affairs
Y
Y Combinator Blog
T
Tenable Blog
N
News and Events Feed by Topic
MyScale Blog
MyScale Blog
量子位
Google DeepMind News
Google DeepMind News
Cyberwarzone
Cyberwarzone
博客园 - 聂微东
D
Darknet – Hacking Tools, Hacker News & Cyber Security
GbyAI
GbyAI
AWS News Blog
AWS News Blog

风雪之隅

深入理解PHP7内核之OBJECT - 风雪之隅 PHP 8新特性之Attributes(注解) - 风雪之隅 博客迁移到腾讯云 - 风雪之隅 在Qcon 2015 北京上的演讲PPT - PHP7 Yar-2.1 新功能介绍 - 风雪之隅 Yaf and Phalcon, which is faster? HTTPOXY漏洞说明 - 风雪之隅 一个关于Zend O+的小分享 - 风雪之隅 在PHP中使用协程实现多任务调度 - 风雪之隅 Curl的毫秒超时的一个"Bug" - 风雪之隅 PHP8.0的Named Parameter - 风雪之隅 关于PHP,关于Realsee - 风雪之隅 PHP8新特性之match表达式 - 风雪之隅 PHP 8新特性之JIT简介 - 风雪之隅 使用SSE2指令高效实现strtolower - 风雪之隅 使用OSC52实现iTerm2远程pbcopy - 风雪之隅 Yaf 3.2 发布 - 风雪之隅 使用PHP Socket开发Yar TCP服务 - 风雪之隅 Yac 2.1 升级说明 - 风雪之隅 Yaf-3.1 10%性能提升版 - 风雪之隅 Yaconf-1.1 40%速度提升版 - 风雪之隅 PHP FFI详解 - 一种全新的PHP扩展方式 - 风雪之隅 使用SSE2指令集加速字符替换 - 风雪之隅 Yaf_Loader重构测试 - 风雪之隅 PHP_INT_MIN 和 -9223372036854775808 - 风雪之隅 深入理解PHP7内核之FAST_ZPP - 风雪之隅 深入理解PHP7内核之HashTable - 风雪之隅 var_dump(1...9)输出什么? - 风雪之隅 print不是函数 - 风雪之隅 令人困惑的strtotime - 风雪之隅 深入理解PHP7内核之Reference - 风雪之隅 深入理解PHP7内核之zval - 风雪之隅 PHP的性能演进(从PHP5.0到PHP7.1的性能全评测) - 风雪之隅 让PHP7达到最高性能的几个Tips - 风雪之隅 写在PHP7发布之际的一些话 - 风雪之隅 让你的PHP7更快之Hugepage - 风雪之隅 让你的PHP7更快(GCC PGO) - 风雪之隅 Yaconf - 一个高性能的配置管理扩展 - 风雪之隅 记录一场没有胜利的局部战斗 - 风雪之隅 PHP7 VS HHVM (Wordpress) - 风雪之隅 GCC优化引起的一个"问题" - 风雪之隅 Weibo LAMP演变 - 6月在上海分享的PPT - 风雪之隅 一个小玩意PHP-Valgrind的介绍 - 风雪之隅 PHP浮点数的一个常见问题的解答 - 风雪之隅 Yac (Yet Another Cache) - 无锁共享内存Cache PDOStatement::bindParam的一个陷阱 - 风雪之隅 Mcrypt响应慢的一个原因 - 风雪之隅 一个程序员眼中的价值 - 风雪之隅 一个关于if else容易迷惑的问题 - 风雪之隅
使用内存硬盘(tmpfs)来加速你的网站 - 风雪之隅
laruence · 2020-02-15 · via 风雪之隅

博客迁移完腾讯云以后, 又配置好了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