How do you enable gzip of HTML/JavaScript/CSS on Amazon Beanstalk and Tomcat

There is no better place than http://www.tonmoygoswami.com/2013/05/how-to-enable-gzip-on-amazon-elastic.html

for your answer

You can restart server from https://console.aws.amazon.com/elasticbeanstalk/

click on application name and then from top right section click action dropdown button and 'restart server'


Adding on to James answer

A cleaner way is to create a config file

.ebextensions/wsgi_custom.config

And place this in there

files:
  "/etc/httpd/conf.d/wsgi_custom.conf":
    mode: "000644"
    owner: root
    group: root
    content: |
      WSGIPassAuthorization On

      LoadModule deflate_module modules/mod_deflate.so

      SetOutputFilter DEFLATE

      # mod_deflate configuration
      <IfModule mod_deflate.c>
          # Restrict compression to these MIME types
          AddOutputFilterByType DEFLATE text/plain
          AddOutputFilterByType DEFLATE text/html
          AddOutputFilterByType DEFLATE application/xhtml+xml
          AddOutputFilterByType DEFLATE text/xml
          AddOutputFilterByType DEFLATE application/xml
          AddOutputFilterByType DEFLATE application/xml+rss
          AddOutputFilterByType DEFLATE application/x-javascript
          AddOutputFilterByType DEFLATE text/javascript
          AddOutputFilterByType DEFLATE text/css
          <IfModule mod_headers.c>
              # Make sure proxies don't deliver the wrong content
              Header append Vary User-Agent env=!dont-vary
          </IfModule>
      </IfModule>

I also added the WSGIPassAuthorization On in case you need to use this for django-rest-framework using jwt auth


I'll answer this myself. Just so its clear to everyone, you CAN connect to your instances of EC2 even though they are being managed by beanstalk. This is helpful because you get to see where things are located. In this case, I didn't know Apache was being used as the webserver for tomcat and had to search for that, but you can find it here as today:

/etc/httpd

Per making changes once you find info like this:
http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/customize-containers.html

If you create a folder called .elasticbeanstalk at the root of your project and then create a file called myapp.config.

Setup Apache:

cp conf/httpd/conf.d/enabledeflate.conf /etc/httpd/conf.d/enabledeflate.conf

Then create enabledeflate.conf with something like this:

SetOutputFilter DEFLATE
# mod_deflate configuration
<IfModule mod_deflate.c>
    # Restrict compression to these MIME types
    AddOutputFilterByType DEFLATE text/plain
    AddOutputFilterByType DEFLATE text/html
    AddOutputFilterByType DEFLATE application/xhtml+xml
    AddOutputFilterByType DEFLATE text/xml
    AddOutputFilterByType DEFLATE application/xml
    AddOutputFilterByType DEFLATE application/xml+rss
    AddOutputFilterByType DEFLATE application/x-javascript
    AddOutputFilterByType DEFLATE text/javascript
    AddOutputFilterByType DEFLATE text/css
    <IfModule mod_headers.c>
        # Make sure proxies don't deliver the wrong content
        Header append Vary User-Agent env=!dont-vary
    </IfModule>
</IfModule>

A couple of notes:
You may need to restart apache the first time you deploy this.
Make sure you put .elasticbeanstalk in the root of your war file (or git repo)


Seemed like there was a few ways to do this but no complete copy and paste solution. So here is mine, working without any issues.

Created file .ebextensions/01-environment.config

Added the following:

# Enable Server-side Compression
files:
  "/etc/httpd/conf.d/enable_mod_deflate.conf":
    mode: "000644"
    owner: root
    group: root
    content: |
        <IfModule mod_deflate.c>
            AddOutputFilterByType DEFLATE text/plain
            AddOutputFilterByType DEFLATE text/html
            AddOutputFilterByType DEFLATE application/xhtml+xml
            AddOutputFilterByType DEFLATE text/xml
            AddOutputFilterByType DEFLATE application/xml
            AddOutputFilterByType DEFLATE application/xml+rss
            AddOutputFilterByType DEFLATE application/x-javascript
            AddOutputFilterByType DEFLATE text/javascript
            AddOutputFilterByType DEFLATE text/css

            DeflateCompressionLevel 9

            BrowserMatch ^Mozilla/4 gzip-only-text/html
            BrowserMatch ^Mozilla/4\.0[678] no-gzip
            BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html

            <IfModule mod_headers.c>
                # Make sure proxies don't deliver the wrong content
                Header append Vary User-Agent env=!dont-vary
            </IfModule>

        </IfModule>

container_commands:
  02_restart_apache:
    command: sudo apachectl restart

What does this do?

  1. Creates a file /etc/httpd/conf.d/enable_mod_deflate.conf with the correct permissions
  2. Adds compression contents inside the file
  3. Finally create a container_commands (link to difference between this and command here), to restart the apache server. This is required to show the effects and is also important as if you are auto scaling and another instance is spun up, this will also create this file then restart apache on the new instance. Without this, the instance would not restart and would require manual restart.