Using TMPFS to speed up websites

Keywords: Nginx Ubuntu github


1. Backup the nginx root directory
2. View system version

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


1. Create a directory, such as / ramdisk, and then use the tmpfs command to mount part of the memory as a hard disk

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

Here, I mount the maximum 256M memory to the / tmp node. It is worth noting that the system will not occupy 256M memory in a short time, but the actual amount, no more than 256M.

2. View disk mount

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

3. Download and install anything sync daemon
In the past, you have to write your own corntab script to synchronize; anything sync daemon can automatically synchronize a specified directory to tmpfs, and can set the time, or automatically synchronize when the system starts. With this tool, all of this will become very simple.

###Download ASD (anything sync day):

### After unzipping, perform the installation
make install-systemd-all

There is a problem here. By default, it will install the systemd service to / usr/lib/systemd/system, but I don't know why I can't find this service in my system without systemd. I can solve this problem by copying several of its service scripts to / lib/systemd/system

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

systemctl daemon-reload

4. Configure and test
The root of nginx is / home/wwwroot/www/htdocs. Now we hope that this directory can be automatically synchronized to the tmpfs directory, that is, / ramdisk. So we modify the asd.conf file and add:

WHATTOSYNC=('/home/wwwroot/www/htdocs/') //Directories to synchronize
VOLATILE="/ramdisk" // tmpfs directory

You can also synchronize multiple files, just write them in the WHATTOSYNC array separated by commas, for example:

WHATTOSYNC=('/home/wwwroot/www/htdocs/', '/home/wwwroot/local/xxxx') //Directories to synchronize

Then we set to synchronize the changes on tmpfs to the hard disk once a day, and edit / lib / SYSTEMd / system / ASD resync.timer

Description=Timer for Anything-sync-daemon - 1Hour
PartOf=asd-resync.service asd.service

Then we use asd p to check:

$ 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:     wwwroot/1000
target to manage:   /home/wwwroot/www/htdocs
sync target:        /home/wwwroot/www/.htdocs-backup_asd
tmpfs target:       /ramdisk/asd-wwwroot/home/wwwroot/www/htdocs
dir size:           237M
recovery dirs:      none

asd will synchronize the / home/wwwroot/www/htdocs directory to / ramdisk / asd wwwroot / home/wwwroot/www/htdocs, and will write the updated content on tmpfs back to / home / wwwroot / www /. HtDocs backup "according to the time

When we stop the asd service, asd will convert. htdocs backup "asd into htdocs in mv, so that you don't have to worry about your content being lost due to the server's sudden power failure
Now, let's start asd:

service asd start

Now / home/wwwroot/www/htdocs will be copied to tmpfs, and the soft link is in the past. That is to say, nginx doesn't need to be modified at all. Just restart fpm and reset the cache of opcache:

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

Restart fpm.

For more information on Asd, please refer to: Anything-sync-daemon

Published 323 original articles, won praise 145, visited 830000+
His message board follow

Posted by eideticmnemonic on Thu, 27 Feb 2020 18:23:27 -0800