Linux RAM Disk: Creating A Filesystem In RAM

Software RAM disks use the normal RAM in main memory as if it were a partition on a hard drive rather than actually accessing the data bus normally used for secondary storage such as hard disk. How do I create and store a web cache on a RAM disk to improve the speed of loading pages under Linux operating systems?

You can create the ram disk as follows (8192 = 8M, no need to format the ramdisk as a journaling file system) :
# mkfs -q /dev/ram1 8192
# mkdir -p /ramcache
# mount /dev/ram1 /ramcache
# df -H | grep ramcache

Sample outputs:

/dev/ram1              8.2M   1.1M   6.7M  15% /ramcache

Next you copy images or caching objects to /ramcache
# cp /var/www/html/images/*.jpg /ramcache
Now you can edit Apache or squid reverse proxy to use /ramcache to map to images.example.com:

 
<VirtualHost 1.2.3.4:80>
ServerAdmin admin@example.com
ServerName images.example.com
DocumentRoot /ramcache
#ErrorLog /var/logs/httpd/images.example.com_error.log
#CustomLog /var/logs/httpd/images.example.com_access.log combined
</VirtualHost>
 

Reload httpd:
# service httpd reload

Now all hits to images.example.com will be served from the ram. This can improve the speed of loading pages or images. However, if server rebooted all data will be lost. So you may want to write /etc/init.d/ script to copy back files to /ramcache. Create a script called initramcache.sh:

#!/bin/sh
mkfs -t ext2 -q /dev/ram1 8192
[ ! -d /ramcache ] && mkdir -p /ramcache
mount /dev/ram1 /ramcache
/bin/cp /var/www/html/images/*.jpg /ramcache

Call it from /etc/rc.local or create softlink in /etc/rc3.d/
# chmod +x /path/to/initramcache.sh
# echo '/path/to/initramcache.sh' >> /etc/rc.local

Was this answer helpful?

 Print this Article

Also Read

CentOS / Redhat: Create Software RAID 1 Array

RAID devices are virtual devices created from two or more real block devices. Linux supports...

HowTo: Verify My NTP Working Or Not

You can use any one of the following program to verify ntp client configuration: ntpq -...

Linux/UNIX: Configure OpenSSH To Listen On an IPv6 Address

How do I enable OpenSSH SSH server to listen on an IPv6 address under Linux or UNIX operating...

All About YUM

up2date command was part of RHEL v4.x or older version. You need to use yum command to update...

CentOS / Redhat: Install nginx As Reverse Proxy Load Balancer

nginx is a Web and Reverse proxy server. Nginx used in front of Apache Web servers. All...