ScaleScaleScaleScale

Tips / Nginx


How to mount a directory into RAM memory for better performance

RAM cache

One of the best things you can do for your web apps is to create/activate a cache system to cache most accessed files or sections of your website. There are many types of cache, the most popular ones are PHP cache into static HTML and SQL cache for the most common MySQL queries. Today we will learn how to create a RAM directory to serve static cache files, mostly used for HTML files.

A RAM directory/folder is always much faster than a directory based on normal hard disk, but it can lost all data when you reboot the server, so the ideal is always to use this RAM directory to store cache files, that can easily re-created after the reboot.

I personally used this same method to speed up the cache files of Nginxtips.com and it’s working really great.

System requirements for this tutorial

  • Dedicated  server or VPS, I can only recommend the best companies for this:
    • A Small Orange
    • A2 Hosting
  • At least 512MB or 1GB of extra RAM memory to use with this cache system.
  • SSH and root access.

Create a tmpfs filesystem to cache the files on RAM

First, stop the webserver you are using:

For Nginx:

service nginx stop

For Apache:

service httpd stop
service apache2 stop

Tmpfs is a special filesystem type which was originally created for temporary files. On the following example I will configure the tmpfs new partition to serve everything inside the wp-content/cache directory.

mount -t tmpfs -o size=2G tmpfs /var/www/nginxtips.com/wp-content/cache

You can tweak the size of the partition depending on how much free RAM you have by altering the -o option passed to the mount command.

Start the web server:

service nginx start
service httpd start
service apache2 start

Create a tmpfs filesystem to serve Nginx cache from RAM

You can do the same to improve Nginx performance by caching the proxy_cache or fastcgi_cache files into RAM, the only difference will be the cache directory in the mount point, that will depend on the proxy_cache_path or fastcgi_cache_path. Examples:

proxy_cache_path /var/cache/nginx keys_zone=one:10m;
fastcgi_cache_path /var/cache/nginx levels=1:2 keys_zone=one:10m;

On this case, the mount point will be:

mount -t tmpfs -o size=2G tmpfs /var/cache/nginx

Configure the cache directory to be re-created into RAM automatically after reboot

nano -w /etc/fstab

Add this line at the end:

tmpfs /var/www/nginxtips.com/wp-content/cache tmpfs defaults,size=2G 0 0

Test the RAM directory

[root@server.myserver.com:~]df -ah | grep tmpfs
tmpfs 2.0G 29M 1996M 1% /var/www/nginxtips.com/wp-content/cache
[root@server.myserver.com:~]

If you see it mounted with df then it’s working. Now the only thing left for you is to test something that involves those files hosted there, or maybe you can load a few files using your web browser.

Popular search terms:

  • nginx create ram folder
  • nginx ramdisk
  • https://www scalescale com/tips/nginx/mount-directory-into-ram-memory-better-performance/
  • nginx cache ram
profile

Esteban Borges

Linux Geek, Webperf Addict, Nginx Fan. CTO @Infranetworking

  • Demetre

    In this configuration performance is improved but lost аuthorization on the sites, so it is impossible to log in particular on such engines as Drupal and xenforo.
    How is it possible to fix it? My system is Debian 7 PHP 5.4+Apache+Nginx

    • Weird. Did you take a look into the system log files? What exact directories did you serve from RAM?

  • Pranita

    I have my /etc/fstab entry as follows on my raspberry pi device :
    ————————————————————————————————————————
    proc /proc proc defaults 0 0
    tmpfs/tmptmpfsnodev,nosuid,size=30M,mode=177700
    tmpfs/var/libtmpfsnodev,nosuid,size=30M,mode=177700
    tmpfs/var/logtmpfsnodev,nosuid,size=30M,mode=177700
    tmpfs/var/runtmpfsnodev,nosuid,size=30M,mode=177700
    /dev/mmcblk0p1 /boot vfat defaults0 2
    /dev/mmcblk0p2 /ext4ro01
    /dev/mmcblk0p4 /mntext4defaults,noatime01
    —————————————————————————————————————————–
    I am unable to start nginx at boot time,
    it’s throwing an error as :
    ####################################################################
    Starting nginx: nginx: [alert] could not open error log file: open() “/var/log/nginx/error.log” failed (2: No such file or directory)
    nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
    2015/02/11 04:20:56 [emerg] 2270#0: mkdir() “/var/lib/nginx/body” failed (2: No such file or directory)
    nginx: configuration file /etc/nginx/nginx.conf test failed
    ###################################################################
    I checked contents of /var/log/ directory, by mounting the SD card on another machine, its having nginx at that location, but when I try to list out the contents on raspberry pi device, it’s unable to show it.

    My question is why the contents of that directory is missing when device is boot up? as when you see them after mounting?

    Thanks in advance!

    • Hey Pranita,

      Guess you are missing the content of those directories because the content is lost when you reboot the device (remember it’s on RAM, and all is deleted when you reboot). A possible solution would be: to create a cron and rsync the contents of those directories every 1 minute into another directory /var/log2 for example, then after you reboot, create an init script that can rsync the content from /var/log2 again back to the memory directory, then try to start Nginx, it should work. I did it a few times when this entire blog was running directly from RAM.

      Good luck.

      • Pranita

        Thanks for your quick reply!

        Yes, I know it’s on RAM, and I will loose everything on every reboot, but the contents are present on that specific location (/var/lib/nginx/) when you mount that SD card on another machine.

        I dont know much about tmpfs. Does it mean, already present content will also not get accessible through it?
        And, regarding cron and the solution which you suggested, Can you tell me about it little more?

        I want to create Read only FS for my raspberry pi device.
        Any other suggestions?

        Thanks a lot again!

      • Pranita

        Thanks a lot for your quick reply!

        It worked for me.
        I was trying with almost similar thing, but was including that respective script inside the /etc/init.d/ directory, I read about the cron and now things working smoothly.

        Thanks… 🙂

  • Do you have any benchmarks numbers vs ram disk vs disk ?