When you are using Apache2 server, then you are well aware of the fact that Apache2 is intended to be powerful, enterprise grade web server. Apache 2.4 disk cache is powerful cache and is content aware. Many users while trying to use old directives face no caching – that is intended to escape errors on many servers which are not closely managed. As Apache 2.4 has undergone many changes like HTTP/2 Server Push, many old features with vulnerabilities needed to be escaped and pushed for the managed, well monitored servers. Here is how to use Apache2 disk cache and also our opinion why, when not to use.
Apache 2.4 Disk Cache : How To Use
This article is for those who have installed Apache2 in our described way on server running newer operating system like Ubuntu 16.04 LTS.
First enable module cache
:
---
1 | a2enmod cache |
You will receive the message :
1 2 3 | Enabling module cache. To activate the new configuration, you need to run: systemctl restart apache2 |
Restart Apache :
1 | systemctl restart apache2 |
Create a directory for Apache2 :
1 | mkdir -p /var/apache-cache |
Give proper ownership :
1 2 3 | chown www-data /var/apache-cache chgrp www-data /var/apache-cache ls -al /var/apache-cache |
Enable module cache_disk
:
1 | sudo a2enmod cache_disk |
You’ll receive this message :
1 2 3 4 5 6 7 8 9 10 11 12 | Considering dependency cache for cache_disk: Module cache already enabled Enabling module cache_disk. Enabling external service apache-htcacheclean Synchronizing state of apache-htcacheclean.service with SysV init with /lib/systemd/systemd-sysv-install... Executing /lib/systemd/systemd-sysv-install enable apache-htcacheclean insserv: warning: current start runlevel(s) (empty) of script `apache-htcacheclean' overrides LSB defaults (2 3 4 5). insserv: warning: current stop runlevel(s) (0 1 2 3 4 5 6) of script `apache-htcacheclean' overrides LSB defaults (0 1 6). The service will be started on next reboot. To activate the new configuration, you need to run: systemctl restart apache2 systemctl start apache-htcacheclean |
Run the needed commands to activate cache_disk
:
1 2 | systemctl restart apache2 systemctl start apache-htcacheclean |
Change directory to /etc/apache2/sites-available
:
1 2 | cd /etc/apache2/sites-available ls -al |
Taking that your website is using HTTPS with Let’s Encrypt, your file for current vhost can be anything which you know, like :
1 2 | 000-default-le-ssl.conf default-ssl.conf |
Open the file, the files usually starts with :
1 2 3 4 | <IfModule mod_ssl.c> <VirtualHost *:443> # The ServerName directive sets the request scheme, hostname and port that ... |
Exactly like our guide on OSCP Stapling configuration on Apache2.4, you’ll add the directives after
and before
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | CacheQuickHandler off CacheLock on CacheLockPath /var/apache-cache CacheLockMaxAge 5 CacheIgnoreHeaders Set-Cookie <Location /> CacheEnable disk CacheHeader on CacheDefaultExpire 800 CacheMaxExpire 64000 CacheIgnoreNoLastMod On ExpiresActive on ExpiresDefault A300 </Location> |
Save the file and run configuration test :
1 | sudo apachectl configtest |
Upon success, restart Apache :
1 | sudo systemctl restart apache2 |
You’ll see files written at /var/apache-cache
. htcacheclean
is used to maintain a control over the size of the filesystem that disk_cache
uses. Run man htcacheclean
or search the web to use it from cron to flush cache on need.
Now for WordPress like CMS, the above configuration will seriously interfere and posts on frontend unlikely to open on click. First the above configuration needs some edit to escape caching like wp-admin
or some files with form or special need :
1 2 3 | CacheDisable /wp-admin CacheDisable /wp-login.php CacheDisable /wp-cron.php |
You need to cache only posts, pages. We need to modify WordPress Permalink from .htaccess
or whatever file you use :
1 2 3 4 5 6 | # WordPress Permalink rewrites RewriteBase / RewriteRule ^index.php$ – [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ /index.php/$1 [L] |
It is more practical to cache selectively the areas you will likely to need least update :
1 2 3 4 5 6 7 8 9 | # Cache the home page <LocationMatch "^/$"> CacheEnable disk </LocationMatch> # Cache category pages <Location /category> CacheEnable disk </Location> |
Apache 2.4 Disk Cache : Why Not To Use
These modules are really for fully managed and 24 hours monitored servers. Bugs, security flaws of these modules are common and often need custom fix. Odd HTTP errors not uncommon which needs active monitoring of log files. Most of the users self manage servers. Number of ordinary users of these modules are less. These are better for static websites and really WordPress has lot of ways of caching including adding a reverse proxy server at front running Nginx. It is very difficult to flush the cache after publication of a post.
Tagged With windows set apache service cach size , apache2 cache_disk , apache server not using disk , apache root directory of the disk cache , apache mod_disk_cache NOT mod_mem_cache , apache disk cache , apache cache location windows , apache 2 4 doesn\t write cache , apache 2 4 caching , apache 2 4 cache videos