Website Output Compression For Faster Page Loading Time
- by Joe Jr Yamut
For the longest time I did not do anything to improve my websites’ loading time. Well that changed the other day. I forced myself to do it. Partially that is.
I have not done much to change the settings on the configuration files of Apache2, MySQL and PHP. If I remember correctly, I have done those way back. So I left it as is. Perhaps I will revisit it one of these days.
What I did instead was to make sure I have compression enabled, and included some additions site-specific. Secondly, shift to php-fpm (which I will write about in another post). FPM is short for the FastCGI Process Manager. It really does wonders to site loading time if you have not tried it yet. The difference is very obvious.
The compression module of the Apache2 web server should be installed by default for the newer versions. Perhaps it is distro-specific. Again I am not sure on this one. Take this with a grain of salt. I think it is comes with the package. Basing this on the fact that mod_deflate does not have its own package when I searched for it using apt-cache search. While it is loaded, it may not be doing its purpose if there is no configuration for it. I don’t remember enabling it or configuring it on my server. Also, modern and mainstream browsers will also take advantage of gzip compression so there is no reason why it is not enabled on your site’s server.
Note: I am using Ubuntu. While I can find my way on CentOS/Red Hat for example, the commands will be different, as well as the locations of packages and files.
Commands will be prefixed with a :~$ to be more readable and obvious. But do not include it when typing the same commands in your terminal. e.g. :~$ sudo ls -la should just be sudo ls -la when you copy/type it in.
Apache2 mod_deflate for gzip compression
Let’s start. You can check if it’s enabled by listing the modules enabled and/or loaded on Apache2.
:-$ sudo apache2ctl -t -D DUMP_MODULES
Look for deflate_module in the list. If it is not, perhaps it is not installed or not loaded. Load it with the command below.
:-$ sudo a2enmod deflate
The following files for Ubuntu distros – /etc/apache2/mods-available/deflate.conf and /etc/apache2/mods-enabled/deflate.load tells me that I’m good to go, while these will have its corresponding symlinks for it to be working at the /etc/apache2/mods-enabled directory. The a2enmod command takes care of the latter. I do not need to have the configuration files in the mods-available directory set up, the package installation takes care of that automatically.
The deflate.conf file looks like this:
While deflate.load is:
Restart the Apache2 service as necessary for it to take effect.
This is just basic and will work for all configured sites. It is good enough for me. It can be be site-specific and have more control on what I want. That can be done so. To customize other things with regards to mod_deflate such as pre-caching and options for proxies, check out the official documentation at this page – https://httpd.apache.org/docs/2.4/mod/mod_deflate.html – for more details.
A particular thing of interest is on the Serving pre-compressed content section. And I quote:
mod_deflatere-compresses content each time a request is made, some performance benefit can be derived by pre-compressing the content and telling
mod_deflateto serve them without re-compressing them. This may be accomplished using a configuration like the following:
One thing to remember is when customizing and using the Header directive in the configuration files, enable its corresponding module. Or else Apache2 will fail to start. I made this silly mistake and panicked for a short while.
:-$ sudo a2enmod headers
Best to test the configuration before restarting too.
:-$ sudo apache2ctl configtest
- > Running on PHP-FPM May 19, 2020
- > Upgrading Ubuntu LTS, PHP and WordPress May 17, 2020
- > Enable Realtek USB 2.0 Card Reader On Ubuntu December 19, 2011
- > Save A File When Not Opened With Root Permission On Vim February 23, 2015
- > Totem Bug When Playing With Subtitles? December 8, 2010
For the longest time I did not do anything to improve my websites’ loading time. Well that changed the other day. I forced myself to do it. Partially that is. I have not done much to change the settings on the configuration files of Apache2, MySQL and PHP. If I remember correctly, I have done…