Have gzip compression enabled for accounts on the server (including newly created accounts)

HostXNow_Chris

Well-Known Member
PartnerNOC
Jan 22, 2016
49
35
18
United Kingdom
cPanel Access Level
DataCenter Provider
Twitter
Hi,

How to have gzip compression enabled for accounts on the server (including newly created accounts) [Optimize Website in cPanel]

I know there are ways to enable it with EasyApache 3 but now we're using EasyApache 4 so just wondering if there's a better way without having to update it the old way eg adding code to Apache conf! Is there a new setting to enable it? There should be after all these years :)

Kind regards,
Chris
 

linux4me2

Well-Known Member
Aug 21, 2015
259
76
28
USA
cPanel Access Level
Root Administrator
In EasyApache4, you can add GZIP compression server-wide, including new accounts, by adding the following to Service Configuration → Apache Configuration → Include Editor → Pre Virtualhost Include → All Versions:
Code:
# Enable GZIP Compression.
<IfModule mod_deflate.c>
    SetOutputFilter DEFLATE
    <IfModule mod_setenvif.c>
        # Netscape 4.x has some problems...
        BrowserMatch ^Mozilla/4 gzip-only-text/html
      
        # Netscape 4.06-4.08 have some more problems
        BrowserMatch ^Mozilla/4\.0[678] no-gzip
      
        # MSIE masquerades as Netscape, but it is fine
        # BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
      
        # NOTE: Due to a bug in mod_setenvif up to Apache 2.0.48
        # the above regex won't work. You can use the following
        # workaround to get the desired effect:
        BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html
      
        # Don't compress images
        SetEnvIfNoCase Request_URI .(?:gif|jpe?g|png)$ no-gzip dont-vary
    </IfModule>
  
    <IfModule mod_headers.c>
        # Make sure proxies don't deliver the wrong content
        Header append Vary User-Agent env=!dont-vary
    </IfModule>
</IfModule>
I realize there's some out-of-date stuff in there regarding browsers and Apache versions, but it seems to work just fine on my server.

I have disabled the "Optimize Website" feature in cPanel to make sure users don't unnecessarily add GZIP via their own .htaccess.
 

Ulysse

Registered
Jan 23, 2017
1
1
3
Montreal
cPanel Access Level
Root Administrator
In EasyApache4, you can add GZIP compression server-wide, including new accounts, by adding the following to Service Configuration → Apache Configuration → Include Editor → Pre Virtualhost Include → All Versions:
Code:
# Enable GZIP Compression.
<IfModule mod_deflate.c>
    SetOutputFilter DEFLATE
    <IfModule mod_setenvif.c>
        # Netscape 4.x has some problems...
        BrowserMatch ^Mozilla/4 gzip-only-text/html
     
        # Netscape 4.06-4.08 have some more problems
        BrowserMatch ^Mozilla/4\.0[678] no-gzip
     
        # MSIE masquerades as Netscape, but it is fine
        # BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
     
        # NOTE: Due to a bug in mod_setenvif up to Apache 2.0.48
        # the above regex won't work. You can use the following
        # workaround to get the desired effect:
        BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html
     
        # Don't compress images
        SetEnvIfNoCase Request_URI .(?:gif|jpe?g|png)$ no-gzip dont-vary
    </IfModule>
 
    <IfModule mod_headers.c>
        # Make sure proxies don't deliver the wrong content
        Header append Vary User-Agent env=!dont-vary
    </IfModule>
</IfModule>
I realize there's some out-of-date stuff in there regarding browsers and Apache versions, but it seems to work just fine on my server.

I have disabled the "Optimize Website" feature in cPanel to make sure users don't unnecessarily add GZIP via their own .htaccess.
Thanks a lot this really save me some time!
 
  • Like
Reactions: cPanelMichael

davidatfuzzylim

Registered
Oct 25, 2010
4
1
53
Sorry for bumping such an old post but does anyone know if there's a way I can adapt this to work for all but one account on our server? The account in question has a script that doesn't work with GZIP enabled but I'd very much like it to be turned on for everyone else.
 

davidatfuzzylim

Registered
Oct 25, 2010
4
1
53
Thanks for the info! I'm still struggling to make this work, though.

I tried this just now. I added the appropriate info through WHM for all hosts and then created the /etc/apache2/conf.d/userdata/ssl/2_4/{username} and /etc/apache2/conf.d/userdata/std/2_4/{username} directories.

I then made a file called pre_virtualhost.conf and put it into both directories, rebuilt the HTTPD conf and restarted Apache, but this didn't seem to work. GZIP is still enabled for all domains under that username along with all others on the server.

The includes are definitely being processed, as I deliberately put a typo into them and tried to rebuild the conf and it didn't work.

Any ideas why this wouldn't be working? For what it's worth I tried inserting this code immediately after the block I'd just added into the pre_virtualhost include file through WHM and it didn't seem to work there, either.

Thanks again!
 

cPanelMichael

Administrator
Staff member
Apr 11, 2011
47,913
2,203
363
Hello,

You could try using an entry like this in the /etc/apache2/conf.d/userdata/std/2_4/exampleuser/example.tld/no-deflate.conf file:

Code:
<IfModule mod_deflate.c>
SetOutputFilter DEFLATE
    <IfModule mod_setenvif.c>
      # Don't compress PHP or HTML
SetEnvIfNoCase Request_URI .(?:php|html)$ no-gzip dont-vary
    </IfModule>
</IfModule>

Remember to run the following command after making the change:

Code:
/usr/local/cpanel/scripts/ensure_vhost_includes --all-users
Then, check to see if PHP and HTML files are compressed. Note that you can change this custom entry to suite your own preferences.

Thank you.
 

vlee

Well-Known Member
Oct 13, 2005
374
26
178
Spokane, Washington
cPanel Access Level
Root Administrator
In EasyApache4, you can add GZIP compression server-wide, including new accounts, by adding the following to Service Configuration → Apache Configuration → Include Editor → Pre Virtualhost Include → All Versions:
Code:
# Enable GZIP Compression.
<IfModule mod_deflate.c>
    SetOutputFilter DEFLATE
    <IfModule mod_setenvif.c>
        # Netscape 4.x has some problems...
        BrowserMatch ^Mozilla/4 gzip-only-text/html
     
        # Netscape 4.06-4.08 have some more problems
        BrowserMatch ^Mozilla/4\.0[678] no-gzip
     
        # MSIE masquerades as Netscape, but it is fine
        # BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
     
        # NOTE: Due to a bug in mod_setenvif up to Apache 2.0.48
        # the above regex won't work. You can use the following
        # workaround to get the desired effect:
        BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html
     
        # Don't compress images
        SetEnvIfNoCase Request_URI .(?:gif|jpe?g|png)$ no-gzip dont-vary
    </IfModule>
 
    <IfModule mod_headers.c>
        # Make sure proxies don't deliver the wrong content
        Header append Vary User-Agent env=!dont-vary
    </IfModule>
</IfModule>
I realize there's some out-of-date stuff in there regarding browsers and Apache versions, but it seems to work just fine on my server.

I have disabled the "Optimize Website" feature in cPanel to make sure users don't unnecessarily add GZIP via their own .htaccess.

Does this work with Apache 2.4.x?

I do not use Apache 2.0.48 anymore"# NOTE: Due to a bug in mod_setenvif up to Apache 2.0.48"