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.
Before we get started, you need to install CyberPanel and create your website.
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.
From left sidebar click ‘Create Database’, select website from drop down so that you can create a database.
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. 🙂
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.
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
You can read more about them here.
Let see how well OpenLiteSpeed performs against Nginx. I am going to use standard DigitalOcean droplet with following specs:
Command used to perform benchmarks:
ab -n 5000 -k -H "Accept-Encoding: gzip,deflate" -c 25 example.com/path
Average = 3.9196 Seconds.
Command used to perform benchmarks:
ab -s 120 -n 5000 -k -H "Accept-Encoding: gzip,deflate" -c 25 example.com/path
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.
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.
10 Comments
GeorgeL
Technically you’re still testing Apache’s processing of PHP if you have Nginx setup as a reverse proxy. What are the average benchmark run times for ? time taken for tests ? or average time per request ?
Could you show the full output for ab benchmarks for both runs ?
Usman Nasir
For some reason when I ran a test against pure Nginx results was even slower. These results are too good to be true, the point here is it was a clean install of CyberPanel with no other optimization tweaks and how easy it is to set up these rewrite rules.
And even if you consider setting rewrite rules as for burden there are many LiteSpeed plugins that do not require setting up these rules and out of the box, you get this performance. It is actually the ease of you, that how easily you can get this performance. Because it was OpenLiteSpeed out of the Box vs Nginx (Apache backend). I am sure that even if you don’t cache, and let OpenLiteSpeed process PHP, OpenLiteSpeed still wins.
And I ran these tests 5 times to be sure (and took the average). Since it is a blog comment, when I have time I will re-run the same test and produce output on CyberPanel forums.
Regards
GeorgeL
Yeah was curious so looking forward to your forum post. FYI here’s pure Nginx 1.13.10 + PHP-FPM 7.1.15 + MariaDB 10.1.31 CentOS 7.4 on 2GB OpenVZ 2cpu E5-1650v3 VPS
fastcgi_cache + memcached
ab -n 5000 -k -H “Accept-Encoding: gzip,deflate” -c 25 http://vanilla.domain.com/
Server Software: nginx
Server Hostname: vanilla.domain.com
Server Port: 80
Document Path: /
Document Length: 2617 bytes
Concurrency Level: 25
Time taken for tests: 0.912 seconds
Complete requests: 5000
Failed requests: 0
Write errors: 0
Keep-Alive requests: 0
Total transferred: 14785000 bytes
HTML transferred: 13085000 bytes
Requests per second: 5482.35 [#/sec] (mean)
Time per request: 4.560 [ms] (mean)
Time per request: 0.182 [ms] (mean, across all concurrent requests)
Transfer rate: 15831.35 [Kbytes/sec] received
Usman Nasir
Was it out of the box nginx setup?
GeorgeL
Was out of box default Centmin Mod 123.09beta01 Nginx install + enabling fastcgi_cache and memcached backend in Vanilla config.php. But I didn’t use vanilla’s official documentation nginx rules as they didn’t work for me. I used my own nginx rules at https://community.centminmod.com/posts/61464/
Usman Nasir
On OpenVZ 2GB Ram and 2 Core VPS.
Model name : Intel(R) Xeon(R) CPU X3440 @ 2.53GHz
ab -n 5000 -k -H “Accept-Encoding: gzip,deflate” -c 25 http://vanilla.cyberpanel.net/
This is ApacheBench, Version 2.3
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking vanilla.cyberpanel.net (be patient)
Completed 500 requests
Completed 1000 requests
Completed 1500 requests
Completed 2000 requests
Completed 2500 requests
Completed 3000 requests
Completed 3500 requests
Completed 4000 requests
Completed 4500 requests
Completed 5000 requests
Finished 5000 requests
Server Software: LiteSpeed
Server Hostname: vanilla.cyberpanel.net
Server Port: 80
Document Path: /
Document Length: 2516 bytes
Concurrency Level: 25
Time taken for tests: 0.348 seconds
Complete requests: 5000
Failed requests: 0
Write errors: 0
Keep-Alive requests: 5000
Total transferred: 14175000 bytes
HTML transferred: 12580000 bytes
Requests per second: 14363.28 [#/sec] (mean)
Time per request: 1.741 [ms] (mean)
Time per request: 0.070 [ms] (mean, across all concurrent requests)
Transfer rate: 39765.52 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.1 0 1
Processing: 0 2 2.3 2 24
Waiting: 0 2 2.3 2 24
Total: 1 2 2.3 2 24
Percentage of the requests served within a certain time (ms)
50% 2
66% 2
75% 2
80% 2
90% 2
95% 2
98% 2
99% 20
100% 24 (longest request)
GeorgeL
Very nice. so the above original blog average times are for time taken for test or time per request ? Just trying to get a bearing on what the blog results mean in terms of ab average times.
Roy
Thanks for this awesome tutorial. My site is up and running. Just facing one small issue.
Litespeed cache is not working. I am getting ” X-Litespeed-Cache: Miss”
what to do?
usmannasir
Hows your rewrite rule looks like?