Applicable to:
- Plesk for Linux
Question
How to enable gzip compression in Apache?
Answer
Note: Since Plesk Obsidian 18.0.33 it is possible to use BROTLI compression which is considered to be better compression than GZIP.
Note: In case Smart static files processing is enabled in Domains > example.com > Apache & nginx Settings, disable it, or use the following article instead: How to enable gzip compression in nginx on a Plesk server
-
Make sure that 'deflate' and 'filter' modules ('filter' might be absent on CentOS 6) are enabled in Tools & Settings > Apache Web Server.
-
Go to Domains > example.com > File Manager and open the
.htaccess
file. -
Add the following content to the bottom of the file:
CONFIG_TEXT: <IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/vnd.ms-fontobject
AddOutputFilterByType DEFLATE application/x-font
AddOutputFilterByType DEFLATE application/x-font-opentype
AddOutputFilterByType DEFLATE application/x-font-otf
AddOutputFilterByType DEFLATE application/x-font-truetype
AddOutputFilterByType DEFLATE application/x-font-ttf
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE font/opentype
AddOutputFilterByType DEFLATE font/otf
AddOutputFilterByType DEFLATE font/ttf
AddOutputFilterByType DEFLATE image/svg+xml
AddOutputFilterByType DEFLATE image/x-icon
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/xml
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
Header append Vary User-Agent
</IfModule>
Note: In case Smart static files processing is enabled on domain in Domains > example.com > Apache & nginx Settings, disable it, or use the following article instead: How to enable gzip compression in nginx on a Plesk server
-
Connect to the server via SSH
Note: if direct SSH access to the server is not possible, contact server administrator for further assistance.
-
Make sure that module
mod_deflate
is enabled in apache:# httpd -M | grep deflate
deflate_module (shared) -
Create a file
deflate.conf
in/etc/httpd/conf.d:
# vi /etc/httpd/conf.d/deflate.conf
-
Add the following into the file:
CONFIG_TEXT: <IfModule mod_deflate.c>
# Compress HTML, CSS, JavaScript, Text, XML and fonts
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/vnd.ms-fontobject
AddOutputFilterByType DEFLATE application/x-font
AddOutputFilterByType DEFLATE application/x-font-opentype
AddOutputFilterByType DEFLATE application/x-font-otf
AddOutputFilterByType DEFLATE application/x-font-truetype
AddOutputFilterByType DEFLATE application/x-font-ttf
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE font/opentype
AddOutputFilterByType DEFLATE font/otf
AddOutputFilterByType DEFLATE font/ttf
AddOutputFilterByType DEFLATE image/svg+xml
AddOutputFilterByType DEFLATE image/x-icon
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/xml
# Remove browser bugs (only needed for really old browsers)
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
Header append Vary User-Agent
</IfModule> -
Note in case of Centos OS, if the server has Apache 2.4 (httpd-2.4), the following module should be enabled:
# grep -ir "mod_filter.so" /etc/httpd/conf.modules.d/00-base.conf
LoadModule filter_module modules/mod_filter.soIn case of Debian and Ubuntu, check that the filter_module is enabled:
# apachectl -M | grep filter
filter_module (shared) -
Restart Apache:
# service httpd restart
Note: On Debian and Ubuntu the service is named apache2 instead of httpd, the list of all modules is called using apache2ctl utility and the path for deflate.conf
file is /etc/apache2/mods-available/
instead of /etc/httpd/conf.d/
Comments
20 comments
Thank you. This was very helpful for my Amazon Ec2 instance!
Disculpa pero cuando realizo lo siguiente me deja de funcionar apache!
@Deyvi,
Could you please share the information about the error? Apache version? The article was verified and no errors with apache were found.
Please correct the row number two on the ssh code section:
# Compress HTML, CSS, JavaScript, Text, XML and fonts AddOutputFilterByType DEFLATE application/javascript
Hello @Gianluca,
Thank you for noticing. The article was updated.
@Ivan:
there should be an option in plesk to do that serverwide alone...
per domain a "infobox" where you can grab the needed code
Hello @Jan Bludau,
>there should be an option in plesk to do that serverwide alone...
Take part in our product improvement and vote for this feature on Plesk UserVoice. The top-ranked suggestions are likely to be included in the next versions of Plesk.
As for "infobox" could you please share the details and clarify the usecase?
On my server I didn't do such configuration, I don't have such files, but all replies are still compressed (HTTP Response Header contains Content-Encoding: gzip).
@Marco Marsala
Have you checked this article as well: https://support.plesk.com/hc/en-us/articles/213380049 ? If sites are using WordPress, there could be plugins that enable gzip compression.
When you enable the gzip option in apache, do you need to enable it in nginx as well, or do you have to choose between the two? I'm saying this because I think it's usual to have nginx as an apache proxy.
Hi @Nerque,
Since Nginx is used as an Apache proxy, gzip compression should be enabled for Nginx and disabled for Apache. In this configuration, static files are served by Nginx.
Information on how to enable compression for Nginx can be found here:
https://support.plesk.com/hc/en-us/articles/213380049
Thanks @...
The truth is that I'm still in doubt, due to the nginx option of "Serve static files directly by nginx".
In my case, nginx is used as a proxy (it is enabled) but the commented option is disabled, due to the accompanying note "Caution: Apache rewrite rules will not be applied."
I have several Prestashop web pages that use rewrite rules, so I guess I can't enable the commented option. So, who really serves the static content?
It's pretty confusing how to set these parameters... I don't understand how I can use nginx as a proxy and have the option "Serve static files directly by nginx" disabled by default... Even more confusing is that it warns of problems if enabled in case of apache rewrites.
And for this reason I don't know if compression should be enabled in nginx or in apache...
Hello,
If "Smart static files processing" and "Serve static files directly by nginx" checkboxes are not enabled - both static and dynamic content are served by Apache
If "Smart static files processing" or "Serve static files directly by nginx" are enabled - statick content is served by Nginx.
You can check documentation for more details: https://docs.plesk.com/en-US/obsidian/administrator-guide/website-management/websites-and-domains/hosting-settings/web-server-settings/apache-and-nginx-settings.72320/
Note: .htaccess is not used If "FPM application served by nginx" is selected in "PHP Settings".
In case of "FPM application served by Apache" is set in "PHP Settings" and option "Smart static files processing" is selected - .htaccess file will be used by Apache:
https://docs.plesk.com/en-US/obsidian/administrator-guide/web-servers/apache-and-nginx-web-servers-linux/adjusting-nginx-settings-for-virtual-hosts.71997/
Thanks @...
"Smart static files processing" Enabled
"Serve static files directly by nginx" disabled
So I guess, from the article you're talking about, I'm in the "Serve all static content via nginx".
About .htaccess, I'm sure they're all applied correctly.
With this situation, I guess from what I've read around here, that the best option is to enable the gzip options in nginx.
But still, if I use the pagespeed module... What? Pagespeed with apache and gzip in nginx? Pagespeed and gzip in nginx?
As far as I know, if I activate the pagespeed module in apache, then I need to disable the "smart static", and all the static content will be served by apache. So I guess in this case gzip must be in apache.
If I activate Pagespeed in nginx, I don't know if it will be fully compatible with this context. And I don't know if Plesk works as well with this module activated in nginx, as it can if I activate it in apache.
However, if I activate Pagespeed, I may not need to activate the gzip and I'm just fooling around.
Many pieces to this puzzle and I can't find a place that deals with all these issues together. That's why all my doubts.
I will try to extrapolate the reference I found in the Plesk forums (https://dh42.com/blog/fastest-prestashop/) on all this. But I will have to fight with the things that have changed in the last years, with the possibilities of Plesk, and with the context of nginx as a proxy and apache to serve a dynamic content.
Hi Nerque,
If you want to use Pagespeed module, then you must use Apache or nginx, as it's available for both webservices.
Plesk's Pagespeed module isn't configured from Plesk, the only possibility is to enable or disable it, further configuration has to be done from your side.
Apart from that, the choice is on your side: to use nginx, apache, or nginx+apache.
Hi,
I have 'deflate' and 'filter' modules enabled, updated the .htaccess file using Plesk interface, but tools.pingdom.com replies that GZIP it is not enabled.
Thank you.
Hi SergioEA,
As far as I see (with CURL), the domain is using already GZIP.
I could not find the Apache Web Server or Apache & Nginx Settings in my Plesk control panel and I am an administrator user. I am using PHP with Plesk version 18.0.33 and IIS server setting
Hi fatimah alqadheeb
Apache web server is not available in Plesk for Windows, IIS is used instead. Therefore here is the right article:
https://support.plesk.com/hc/en-us/articles/115000209874-How-to-configure-compression-and-caching-in-Plesk-on-Windows-
I've got WP optimise setup on the domain of my server.
I want to enable GZIP server wide using SSH as advised on this topic.
I've done all the checks and followed the instructions and I am receiving this message from the Plugin.
The only error I get using SSH is this, and it may be the reason why I can't get GZIP enabled.
[root@server ~]# grep -ir "mod_filter.so" /etc/httpd/conf.modules.d/00-base.conf
However, now I don't know what else I can do, so that the filter module is found. It's definitely enabled on the Server
Please sign in to leave a comment.