Applicable to:
- Plesk for Linux
Question
How to enable gzip compression in Apache?
Answer
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
9 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.
Please sign in to leave a comment.