Why Rob, Why!
I find most of my projects stem from boredom and the inability to not f*ck with my server. So to that, I say, “I don’t know.” Sometimes I have to do dumb things because they can be done, and this is one of those times. Why not load my entire web directory into tmpfs mount? I have a little extra RAM and a questionable amount of common sense. Let’s do it.
I take no responsibility if you do this, and there is no warranty. If you break your site or server, you are on your own!
If what you’ve done is stupid, but it works… then it really isn’t all that stupid.David Letterman
Let the stupidity begin!
So we are going to load the website into memory, allowing it to be served with less IO drag. The first thing we need to do is create a folder that points to the RAM. I chose to test with the mount command, and once I was satisfied, I moved the command to fstab.
tmpfs /srv/www tmpfs nosuid,nodev,noatime 0 0
/srv/www is created and added to fstab. The first hurdle I ran into was with the RAM and its volatility. RAM is cleared when the server restarts, loses power, or crashes. So I first dealt with the controllable issues. When the server starts up, you must load your files into memory. Rsync works pretty well for this and will help when we save our files later. While we are at it, let’s fix the permissions on the copied files.
#!/bin/bash echo "Copy to Memory" rsync -rucP /var/www/ /srv/www/ echo "Modify Ownership SRV" chown -R apache:apache /srv/www
Our website lives in memory. Once configured, the server will serve our site from there. This will boost our website speed substantially, even if it was initially on an SSD. But what happens if we make a change to the site files? Where do we make those changes? Using just the code block above, you would have to make changes in the /srv/www folder. Not super intuitive, and now we have to change every script and program to modify that folder. Not a huge issue, but laziness will always win. Let’s go ahead and put that code into cron now and make it run every 2 minutes.
*/2 * * * * root /path/to/script/mem.sh
Let’s sync that back to permanent storage.
I’m sure some of you have noticed a small problem. What happens when the server crashes? Any changes from the web interface to the site files will be lost. let’s fix that and add a sync back to the
/var/www/ folder to stash our changes, we can rerun chown for both folders, too, just in case.
#!/bin/bash echo "Copy back changes" rsync -rucP /srv/www/ /var/www/ echo "Copy to Memory" rsync -rucP /var/www/ /srv/www/ echo "Modify Ownership VAR" chown -R apache:apache /var/www echo "Modify Ownership SRV" chown -R apache:apache /srv/www
I achieved much faster load times, and once I added the sync back to storage, it was crash-resistant. My sites are around 5GB, and the initial sync takes about 15 seconds. Syncs after the initial only take about 5 seconds, depending on how many files were changed and their sizes.
I hope you are not an idiot and do this like me. Please don’t be stupid. You can lose a chunk of your data if you exceed your free memory or do something wrong. Just because it worked for me does not mean it will work for you.