Moving WordPress into RAM

There are a lot of things you can do to optimize WordPress to run as smooth as possible including optimizing Apache and MySQL, introducing cache layers and more. That is just fine, and they should be done in a production environment. In a development environment however, it is another story. Here you have the option to do radical things for radical results. Like, moving all data into RAM!

I like WordPress, and I do WordPress development on a daily basis. But WordPress has VERY bad performance and that will eventually become quite annoying when developing and testing a lot. Introducing a cache layer on a development machine is not really an option since you would like to see the results of your work instantly. So, why not move all WordPress files including the database into RAM and boost that performance quite a bit.

The concept is quite simple:

  1. Create a boot script that creates a RAM-drive and copies data to it.
  2. Configure Apache and MySQL to work against this drive.
  3. Create a shutdown script that copies back everything from RAM to the HDD again.

I am using an Ubuntu Server 11.04 with Apache, MySQL and WordPress installed; I will not cover how to install them in this blog post (Google it).

File structure

The RAM-drive will be mounted on /ram, and on that drive the copied files will maintain their relative file structure, kind of. /var/www will become /ram/var/www etc.

Configuring MySQL

First of all you need to stop the MySQL daemon using

stop mysql

Once stopped, we need to edit the /etc/init/mysql.conf in order to disable MySQL autostart:

nano /etc/init/mysql.conf

Comment out these three lines:

#start on (net-device-up
#          and local-filesystems
#          and runlevel [2345])

Save your changes. Next we need to change the path were MySQL stores its data. Edit /etc/mysql/my.cnf:

nano /etc/mysql/my.cnf

Two lines should be altered here:

datadir = /ram/var/lib/mysql
tmpdir = /ram/tmp

If you are running AppArmour (most likely if you are running Ubuntu), you will need to allow access to the new paths. Edit /etc/apparmor.d/usr.sbin.mysqld:

Add /ram before the two lines starting with /var/lib/mysql like so:

/ram/var/lib/mysql/ r,
/ram/var/lib/mysql/** rwk,

Also add the following line below the previously altered rows:

/ram/tmp/** rwk,

Save your changes.

Configuring Apache

Apache is very simple to configure, just make sure your VHOST-files are pointing to the RAM-drive. My document root for the default site (or WordPress installation) is:


Then you need to stop Apache from auto starting, we will start it using a startup script instead. An easy way to do this is to install sys-rc-conf:

apt-get install sysv-rc-conf

Then just start it:


Uncheck every checkbox on the apache2 line using space. When you are done, press ‘q’ to quit.

Create a mount point

This is where it all will happen:

mkdir /ram

Startup script

Now on to the interesting parts, the startup script. By all means, this script could certainly be optimized, but I have been successfully used it for some time.

Create a new file in your home directory, or any other location you prefer. I have placed mine in /home/puffy/scripts/

Insert the following script:

# Create RAM drive.
mount -t ramfs -o size=3072m ramfs /ram

# Copy Apache data.
mkdir /ram/var
cp -a /var/www /ram/var/www
chown -R www-data:www-data /ram/var/www
chmod -R 775 /ram/var/www
/etc/init.d/apache2 start

# Copy MySQL data.
mkdir /ram/var/lib
cp -a /var/lib/mysql /ram/var/lib/mysql
chown -R mysql:mysql /ram/var/lib/mysql
chmod -R 700 /ram/var/lib/mysql

# Create tmp folder.
mkdir /ram/tmp
chmod 777 /ram/tmp
start mysql

Save the file and then make it executable:

chmod +x /home/puffy/scripts/

Now make this execute during startup by adding it to the /etc/rc.local file. My rc.local looks like this:

exit 0

Shutdown script

Create a new file named /home/puffy/scripts/ and insert this script:

# Backup Apache
/etc/init.d/apache2 stop
rm -R /var/www
cp -a /ram/var/www /var/www

# Backup MySQL
stop mysql
rm -R /var/lib/mysql/*
cp -Rf /ram/var/lib/mysql/* /var/lib/mysql/

Save the file and then make it executable:

chmod +x /home/puffy/scripts/

Now we need to make this file execute prior to shutting down our server. Do this by creating a symbolic link:

ln –s /home/puffy/scripts/ /etc/rc6.d/

That’s it!

Did it work?

Restart your server and make sure that the RAM-drive is mounted properly. Run:

df –a

You should be able to see the ramfs drive mounted on /ram:

Filesystem           1K-blocks      Used Available Use% Mounted on
 /dev/sda1              7852740   2259924   5193920  31% /
 proc                         0         0         0   -  /proc
 none                         0         0         0   -  /sys
 fusectl                      0         0         0   -  /sys/fs/fuse/connections
 none                         0         0         0   -  /sys/kernel/debug
 none                         0         0         0   -  /sys/kernel/security
 none                   2020756       188   2020568   1% /dev
 none                         0         0         0   -  /dev/pts
 none                   2028324         0   2028324   0% /dev/shm
 none                   2028324        68   2028256   1% /var/run
 none                   2028324         0   2028324   0% /var/lock
 ramfs                        0         0         0   -  /ram

Now, check your WordPress installation by surfing to it using your browser. You will probably notice quite a difference!

One thought on “Moving WordPress into RAM

Leave a Reply

Your email address will not be published.