Supercharge your Vanilla Community with LSCache!

Vanilla is a lightweight PHP based community forums. CyberPanel forums also run on Vanilla, it is already very good performance wise but with large communities and fewer server resources, you might want to make use of LiteSpeed cache to boost the performance of your Vanilla community.

Since LiteSpeed does not have an official plugin yet for Vanilla we can utilize rewrite rules to cache guest visitors of Vanilla, logged in users will not be cached using rewrite rules since they require more logic which can not be simply achieved via rewrite rules.

Step 1: Install CyberPanel and create your website!

Before we get started, you need to install CyberPanel and create your website.

Step 2: Download and install Vanilla Forums!

At the time of this writing, the latest version available to download from Vanilla is 2.5.1. Which you can download from here, you will get a file named ‘vanilla-core-2-5-1.zip‘. Upload this file to your website either using the File manager in CyberPanel or FTP. In this tutorial, we are going to leverage File manager.

Once a website is launched scroll down to find the File manager.

This will launch File manager, using which you can upload your vanilla forums zip file, make sure you have increased your upload limit to at least 12MB, because vanilla forum file size is ~10MB.

Once uploaded and unzipped, you can visit your domain to start the installation. Before that let’s create a database that vanilla is going to use.

Create Database!

From left sidebar click ‘Create Database’, select website from drop down so that you can create a database.

Start Installation!

To install Vanilla open your website URL where you have uploaded and unzipped vanilla, in this case, it is ‘vanilla.cyberpanel.net’.

It is going to ask you your database details which you just created in last step and some other information which you can easily figure out. Once the installation is successful it will redirect you to vanilla administrator dashboard, and your main site should look something similar to:

Now let see how we can supercharge our vanilla forum using LSCache. 🙂

Step 3: Supercharge your site with LSCache!

This will be the most fun part of this article, will see how you can use rewrite rules to cache responses for logged out users.

So, after the installation is completed, I visited my site and noted down the state of cookies, here are the cookies when you are not logged into vanilla forums.

__cfduid=da901b6948cfb38c4f9a8dfd6204800441515084444; 
__auc=333e18a216200e164e948c936d1; 
Vanilla-tk=GGe13EXsZhBwdO1U%3A0%3A1521119895%3A885429e1b4255fc6b04636310edb9671

And once logged in I see some additional cookies:

__cfduid=da901b6948cfb38c4f9a8dfd6204800441515084444;
 __auc=333e18a216200e164e948c936d1; 
__asc=1c029fb416256da64456a8fb16d; 
Vanilla=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE1MjQ0Njc2MjQsImlhdCI6MTUyMTg3NTYyNCwic3ViIjoxMjV9.Svh-EWQmDK2BiSaCZdwzVRioCWDluwF9yNALQzV2Zzk; 
Vanilla-tk=MBsPBOajlcj1kiCN%3A125%3A1521875625%3A008881e362a509c7c581fed621ddb904; 
Vanilla-Vv=1521875626

Vanilla and Vanilla-Vv seem to be additional cookies once we are logged into the community. We can leverage one of these cookies to construct our rewrite rules. (Use rewrite rule guide to learn how you can add rewrite rules)

<IfModule LiteSpeed>
RewriteEngine On
CacheLookup on
RewriteCond %{REQUEST_METHOD} ^GET|HEAD|PURGE$
RewriteCond %{HTTP_COOKIE} !Vanilla-Vv [NC]
RewriteRule .* - [E=Cache-Control:max-age=120]
</IfModule>

In rewrite rules above note “RewriteCond %{HTTP_COOKIE} !Vanilla-Vv [NC] which means that if “Vanilla-Vv” is not set in the request you can serve this request from the cache if a cached copy of this page is available.

If you want to go further and exclude some pages from being cached you can do that to, an example of these pages are login or dashboard pages, these are the pages which should not be usually cached.

RewriteCond %{REQUEST_URI} !dashboard [NC]

Which means do not cache pages containing “dashboard” in the URL. So now your final rules should look like:

<IfModule LiteSpeed>
RewriteEngine On
CacheLookup on
RewriteCond %{REQUEST_METHOD} ^GET|HEAD|PURGE$
RewriteCond %{HTTP_COOKIE} !Vanilla-Vv [NC]
RewriteCond %{REQUEST_URI} !dashboard [NC]
RewriteRule .* - [E=Cache-Control:max-age=120]

# no cache
RewriteCond %{HTTP_COOKIE} Vanilla-Vv [NC]
RewriteRule .* - [E=Cache-Control:vary=loggedin]

</IfModule>

max-age takes time in seconds you want to cache the pages, you can change it to whatever you like. In this example, I set it to 120 seconds (2 minutes) after which page is going to be fetched from the backend.

Now on the command line you can see that you are getting X-Litespeed-Cache: hit

curl http://vanilla.cyberpanel.net/index.php?p=/ --head

HTTP/1.1 200 OK
X-Powered-By: PHP/7.2.2
P3P: CP="CAO PSA OUR"
X-Garden-Version: Vanilla 2.5.1
Content-Type: text/html; charset=utf-8
X-Frame-Options: SAMEORIGIN
X-Litespeed-Cache: hit
Date: Sat, 24 Mar 2018 12:05:39 GMT
Server: LiteSpeed
Connection: Keep-Alive

Which means the cache is working correctly.

Trouble shooting the cache!

There are probable chances that you might be getting cached responses even when you are logged in, the first thing you can do is to temporarily solve this issue by turning of cache lookup by removing “CacheLookup on” from rewrite rules.

In your test environment, you can see that what cookies are being set by Vanilla when you are logged in so that you can set them accordingly in rewrite rules.

reqCookieCache and respCookieCache

Note: Do this only if you are getting cache hits for logged in users too.

In your httpd_config.conf (which is normally located at /usr/local/lsws/conf/httpd_config.conf) you can find these two parameters and set them to 0

  • respCookieCache 0
  • reqCookieCache 0

You can read more about them here.

Benchmarks!

Let see how well OpenLiteSpeed performs against Nginx. I am going to use standard DigitalOcean droplet with following specs:

  • 1 GB Memory
  • 25 GB Disk
  • 1 vCPU.

OpenLiteSpeed Performance

Command used to perform benchmarks:

ab -n 5000 -k -H "Accept-Encoding: gzip,deflate" -c 25 example.com/path

  • 1st Run: 4.010 Seconds.
  • 2nd Run: 3.834 Seconds.
  • Third Run: 3.822 Seconds.
  • 4th Run: 3.997 Seconds.
  • 5th Run: 3.935 Seconds.

Average = 3.9196 Seconds.

Nginx (Apache on Backend) Performance

Command used to perform benchmarks:

ab -s 120 -n 5000 -k -H “Accept-Encoding: gzip,deflate” -c 25 example.com/path

  • 1st Run: 479.182 Seconds.
  • 2nd Run: 389.429 Seconds.
  • Third Run: 408.600 Seconds.
  • 4th Run: 422.308 Seconds.
  • 5th Run: 594.049 Seconds.

Average = 458.7136 Seconds.

I had to set additional parameter -s 120 because without this parameter test was timing out on Nginx with apache backend.

Conclusion

It is too good to be true as we got average of 3.9196 Seconds on OpenLiteSpeed vs 458.7136 Seconds for Nginx (with apache as backend). But its true, you can spin a small VPS and run these tests.

Please note that there were no extra optimizations it was a clean install of CyberPanel. You just need to know how you can use rewrite rules to cache pages.