The Community Forums

Interact with an entire community of cPanel & WHM users!
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

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

Discussion in 'Workarounds and Optimization' started by HostXNow_Chris, Sep 28, 2016.

Tags:
  1. HostXNow_Chris

    HostXNow_Chris Registered

    Joined:
    Jan 22, 2016
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    United Kingdom
    cPanel Access Level:
    Root Administrator
    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
     
  2. linux4me2

    linux4me2 Well-Known Member

    Joined:
    Aug 21, 2015
    Messages:
    149
    Likes Received:
    34
    Trophy Points:
    28
    Location:
    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.
     
  3. cPanelMichael

    cPanelMichael Forums Analyst
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    37,086
    Likes Received:
    1,288
    Trophy Points:
    363
    cPanel Access Level:
    Root Administrator
    Hello @HostXNow_Chris,

    The steps in the previous response should work. Let us know if you have any additional questions.

    Thanks!
     
    HostXNow_Chris likes this.
  4. Ulysse

    Ulysse Registered

    Joined:
    Jan 23, 2017
    Messages:
    1
    Likes Received:
    1
    Trophy Points:
    3
    Location:
    Montreal
    cPanel Access Level:
    Root Administrator
    Thanks a lot this really save me some time!
     
    cPanelMichael likes this.
  5. davidatfuzzylim

    davidatfuzzylim Registered

    Joined:
    Oct 25, 2010
    Messages:
    4
    Likes Received:
    1
    Trophy Points:
    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.
     
  6. cPanelMichael

    cPanelMichael Forums Analyst
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    37,086
    Likes Received:
    1,288
    Trophy Points:
    363
    cPanel Access Level:
    Root Administrator
  7. davidatfuzzylim

    davidatfuzzylim Registered

    Joined:
    Oct 25, 2010
    Messages:
    4
    Likes Received:
    1
    Trophy Points:
    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!
     
  8. cPanelMichael

    cPanelMichael Forums Analyst
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    37,086
    Likes Received:
    1,288
    Trophy Points:
    363
    cPanel Access Level:
    Root Administrator
    Hello @davidatfuzzylim,

    Could you let us know what method you are using to test if compression is enabled?

    Thank you.
     
  9. davidatfuzzylim

    davidatfuzzylim Registered

    Joined:
    Oct 25, 2010
    Messages:
    4
    Likes Received:
    1
    Trophy Points:
    53
  10. cPanelMichael

    cPanelMichael Forums Analyst
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    37,086
    Likes Received:
    1,288
    Trophy Points:
    363
    cPanel Access Level:
    Root Administrator
    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.
     
  11. davidatfuzzylim

    davidatfuzzylim Registered

    Joined:
    Oct 25, 2010
    Messages:
    4
    Likes Received:
    1
    Trophy Points:
    53
    Perfect, that did the trick! Thanks.
     
    cPanelMichael likes this.
  12. vlee

    vlee Well-Known Member

    Joined:
    Oct 13, 2005
    Messages:
    335
    Likes Received:
    18
    Trophy Points:
    168
    Location:
    Spokane, Washington
    cPanel Access Level:
    Root Administrator

    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"
     
  13. cPanelMichael

    cPanelMichael Forums Analyst
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    37,086
    Likes Received:
    1,288
    Trophy Points:
    363
    cPanel Access Level:
    Root Administrator
    Hello @vlee,

    Yes, the full entry works for Apache 2.4.

    Thank you.
     
  14. vlee

    vlee Well-Known Member

    Joined:
    Oct 13, 2005
    Messages:
    335
    Likes Received:
    18
    Trophy Points:
    168
    Location:
    Spokane, Washington
    cPanel Access Level:
    Root Administrator
    Thank you
     
    cPanelMichael likes this.
  15. HostXNow_Chris

    HostXNow_Chris Registered

    Joined:
    Jan 22, 2016
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    United Kingdom
    cPanel Access Level:
    Root Administrator
    A lot of good feedback here. Great stuff.
     
Loading...

Share This Page