Speed up Apache2 Banana Pi Web Server with RAM Cache

Lightning Web Server

This is for people who want to shave a fraction of a second off their website loading time on Banana Pi! So you have your nice and speedy Apache 2 or Nginx website hosted on your Banana Pi, but it is not fast enough or you want to push it to the limits? Well, this is where the 1GB RAM on Banana Pi comes in handy. 

Options before us

System cache (automatic)
Apache cache module (mod_cache)
Manual cache (tmpfs)

note: all above caching options are for server sided caching. Also can be thought of as the first visit, upon the next visit during the same session, your computer will go to a local cache on your device.

RAM basics

The basics steps of loading a website:
  1. Client send request to server
  2. Server reads file from storage and send it back to client

Storage Devices


  1. Random Access Memory - Really fast, limited in size (expensive), volatile (will lose data on reboot)
  2. SD card - Much slower than RAM, larger in size (cheaper), non-volatile (will retain data without power)
RAM in Banana Pi writes at 120MB/s compared to SD card <30MB/s

In this guide, what we are trying to do is to speed up the loading speed of a website by storing the website on the RAM instead of the slower SD card. 

Automatic System Caching  (Recommended)

Guess what? Your website is already cached in the RAM by default, well at least hopefully. Modern Linux operating systems automatically caches files that are frequently accessed in to the RAM for a faster system performance. This process is only slower for the first load of a website after server restart.

Manual Cache 

tmpfs is a file system in Linux that mounts a folder in the RAM of a computer. Manual caching will place website files in a RAM folder and direct Apache2 the location of the website on the RAM server. 

To start we need to create a folder for the tmpfs file system
mkdir /var/wwwc
Then we need to mount this file system at start up
nano /etc/fstab
 Add the line below to '/etc/fstab'
tmpfs /var/wwwc tmpfs defaults,size=128M 0 0
You can change the directory and the default size to your customization

Now we have to copy the files from the old folder to the new RAM folder
If you have questions about uploading your files, see my Apache2 web server guide.
rsync -av /var/www/ /mnt/wwwc/
It sync files from the first directory to the second one, you can see '/var/wwwc' take up 61MBs



Now we can edit the configuration in Apache2 to direct requests to the RAM folder
nano /etc/apache2/sites-enabled/000-default
change document root and directory to '/var/wwwc/' in lines 4 and 9
DocumentRoot /var/wwwc/
<Directory /var/wwwc/>
restart Apache server
service apache2 restart

Apache Cache Module 

An integrated module is built into Apache 2 that will cache websites with correct HTTP headers.
Enabled the module by
a2enmod cache
a2enmod file_cache
service apache2 restart
 Now that we enabled all the modules, it is time to configure them.
nano /etc/apache2/mods-enabled/file_cache.load

It should open up a text editor,edit the files by adding lines at the end
MMapFile [directory to file] [directory to file] ...
'MMap' Directive in Apache2 maps one or more files (note spaces in between) into memory at server start up time.

A example of the code can look like this
MMapFile /var/www/index.html /var/www/images/banner.jpg
So this is how to cache files with the Apache cache module
service apache2 restart

So All for Nothing?

I did some intense testing on all options, though some data varied, I can conclude that both Manual cache and system cache are the better options. Apache module cache gets on the complicated side. 

Which one is faster? Well test it out on your own website!

Real world testing

I uploaded a real site to Apache2 and did some testing. I used both Android and Windows platform and different browsers. 

notes: I did not include every file with Apache Module Cache
notes: The results may not be the most accurate
notes: The Android website speed app might be the most reliable

Automatic System Cache Android

Apache Module Cache Android

Manual Cache Android

I made sure to reset cache on my local device after each test.

Automatic System Cache 1st request on server Chrome

Automatic System Cache Chrome

Apache Module Cache Chrome

Manual Cache Chrome

Automatic System Cache Firefox

Manual Cache Firefox

Conclusion

So is it worth your time going through all the steps to see no real performance gains? Well, it definitely gives us valuable experiences. 

Sorry to spoil the ending :) but you should still try to RAM cache your server manually!

If i made any mistakes regarding this topic, feel free to comment. We are all learning.



13 comments:

  1. This is my first time visit to your blog and I am very interested in the articles that you serve. Provide enough knowledge for me. Thank you for sharing useful and don't forget, keep sharing useful info: sayapro bin checker

    ReplyDelete
  2. Excellent article. Very interesting to read. I really love to read such a nice article. Thanks! keep rocking. best place to hire freelancers

    ReplyDelete
  3. Thank you a bunch for sharing this with all of us you actually realize what you are talking about! Bookmarked. Please also seek advice from my site =). We could have a hyperlink change contract between us! Medium

    ReplyDelete
  4. Staggering post. I'm investigating this issue. So a guarantee of appreciation is assembled in the post. Totally cool post. This is an unfathomably outstanding alright and valuable post. Much appreciated dark web

    ReplyDelete
  5. Wow, What a Excellent post. I really found this to much informatics. It is what i was searching for.I would like to suggest you that please keep sharing such type of info.Thanks Web Design Bangkok

    ReplyDelete
  6. Nice to be visiting your blog again, it has been months for me. Well this article that i've been waited for so long. I need this article to complete my assignment in the college, and it has same topic with your article. Thanks, great share.
    search engine optimization solutions

    ReplyDelete
  7. Wow, What a Excellent post. I really found this to much informatics. It is what i was searching for.I would like to suggest you that please keep sharing such type of info.Thanks https://www.sorrisieservizi.it/

    ReplyDelete
  8. Cool stuff you have got and you keep update all of us. small business website designers

    ReplyDelete
  9. I can’t believe focusing long enough to research; much less write this kind of article. You’ve outdone yourself with this material without a doubt. It is one of the greatest contents. cheap website designer sydney

    ReplyDelete
  10. Really nice and interesting post. I was looking for this kind of information and enjoyed reading this one. Keep posting. Thanks for sharing. servicio de diseƱo web en veracruz

    ReplyDelete
  11. Speed Up Apache2 Banana Pi Web Server With Ram Cache ~ Project Banana Pi >>>>> Download Now

    >>>>> Download Full

    Speed Up Apache2 Banana Pi Web Server With Ram Cache ~ Project Banana Pi >>>>> Download LINK

    >>>>> Download Now

    Speed Up Apache2 Banana Pi Web Server With Ram Cache ~ Project Banana Pi >>>>> Download Full

    >>>>> Download LINK H3

    ReplyDelete
  12. Speed Up Apache2 Banana Pi Web Server With Ram Cache ~ Project Banana Pi >>>>> Download Now

    >>>>> Download Full

    Speed Up Apache2 Banana Pi Web Server With Ram Cache ~ Project Banana Pi >>>>> Download LINK

    >>>>> Download Now

    Speed Up Apache2 Banana Pi Web Server With Ram Cache ~ Project Banana Pi >>>>> Download Full

    >>>>> Download LINK Gt

    ReplyDelete