Great Apache 2.4 VirtualHost template

Great Apache 2.4 VirtualHost template for good performance setup. Standard configuration, logging, headers, compression, deflate, caching, and other stuff:

<VirtualHost *:80>
    ServerAdmin administrator@domain.tld
    ServerName domain.tld
    ServerAlias www.domain.tld

    DocumentRoot /var/www/html/domain.tld

    ErrorLog /var/log/httpd/domain.tld_error_log
    CustomLog /var/log/httpd/domain.tld_access_log common

    <IfModule mod_headers.c>
      <FilesMatch "\.(js|json|css|xml|gz)$">
        Header append Vary: Accept-Encoding
      </FilesMatch>
    </IfModule>

    ## GZIP COMPRESSION ##
    <ifModule mod_gzip.c>
        mod_gzip_on Yes
        mod_gzip_dechunk Yes
        mod_gzip_item_include file .(html?|txt|css|js|json|php|pl)$
        mod_gzip_item_include handler ^cgi-script$
        mod_gzip_item_include mime ^text/.*
        mod_gzip_item_include mime ^application/x-javascript.*
        mod_gzip_item_exclude mime ^image/.*
        mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
    </ifModule>
    ## GZIP COMPRESSION ##

    ## DEFLATE ##
    <IfModule mod_deflate.c>

    # force deflate for mangled headers
    <IfModule mod_setenvif.c>
      <IfModule mod_headers.c>
        SetEnvIfNoCase ^(Accept-EncodXng|X-cept-Encoding|X{15}|~{15}|-{15})$ ^((gzip|deflate)\s*,?\s*)+|[X~-]{4,13}$ HAVE_Accept-Encoding
        RequestHeader append Accept-Encoding "gzip,deflate" env=HAVE_Accept-Encoding
      </IfModule>
    </IfModule>

    # HTML, TXT, CSS, JavaScript, JSON, XML, HTC:
    <IfModule filter_module>
        FilterDeclare   COMPRESS
        FilterProvider  COMPRESS  DEFLATE "%{Content_Type} = 'text/html'"
        FilterProvider  COMPRESS  DEFLATE "%{Content_Type} = 'text/css'"
        FilterProvider  COMPRESS  DEFLATE "%{Content_Type} = 'text/plain'"
        FilterProvider  COMPRESS  DEFLATE "%{Content_Type} = 'text/xml'"
        FilterProvider  COMPRESS  DEFLATE "%{Content_Type} = 'text/x-component'"
        FilterProvider  COMPRESS  DEFLATE "%{Content_Type} = 'application/javascript'"
        FilterProvider  COMPRESS  DEFLATE "%{Content_Type} = 'application/json'"
        FilterProvider  COMPRESS  DEFLATE "%{Content_Type} = 'application/xml'"
        FilterProvider  COMPRESS  DEFLATE "%{Content_Type} = 'application/xhtml+xml'"
        FilterProvider  COMPRESS  DEFLATE "%{Content_Type} = 'application/rss+xml'"
        FilterProvider  COMPRESS  DEFLATE "%{Content_Type} = 'application/atom+xml'"
        FilterProvider  COMPRESS  DEFLATE "%{Content_Type} = 'application/vnd.ms-fontobject'"
        FilterProvider  COMPRESS  DEFLATE "%{Content_Type} = 'image/svg+xml'"
        FilterProvider  COMPRESS  DEFLATE "%{Content_Type} = 'image/x-icon'"
        FilterProvider  COMPRESS  DEFLATE "%{Content_Type} = 'application/x-font-ttf'"
        FilterProvider  COMPRESS  DEFLATE "%{Content_Type} = 'font/opentype'"
        FilterChain     COMPRESS
        FilterProtocol  COMPRESS  DEFLATE change=yes;byteranges=no
    </IfModule>

    <IfModule !mod_filter.c>
      # Legacy versions of Apache
      AddOutputFilterByType DEFLATE text/html text/plain text/css application/json
      AddOutputFilterByType DEFLATE application/javascript
      AddOutputFilterByType DEFLATE text/xml application/xml text/x-component
      AddOutputFilterByType DEFLATE application/xhtml+xml application/rss+xml application/atom+xml
      AddOutputFilterByType DEFLATE image/svg+xml application/vnd.ms-fontobject application/x-font-ttf font/opentype
    </IfModule>
    </IfModule>

    ## EXPIRES CACHING ##
    <IfModule mod_expires.c>
      ExpiresActive on

    # Perhaps better to whitelist expires rules? Perhaps.
      ExpiresDefault                          "access plus 1 month"

    # cache.appcache needs re-requests in FF 3.6 (thx Remy ~Introducing HTML5)
      ExpiresByType text/cache-manifest       "access plus 0 seconds"

    # Your document html
    ExpiresByType text/html "access plus 0 seconds"

    # Data
    ExpiresByType text/xml "access plus 1 day"
    ExpiresByType application/xml "access plus 1 day"
    ExpiresByType application/json "access plus 4 hours"

    # RSS feed
    ExpiresByType application/rss+xml "access plus 1 day"

    # Favicon (cannot be renamed)
    ExpiresByType image/x-icon "access plus 1 week"

    # Media: images, video, audio
    ExpiresByType image/gif "access plus 1 day"
    ExpiresByType image/png "access plus 1 day"
    ExpiresByType image/jpg "access plus 1 day"
    ExpiresByType image/jpeg "access plus 1 day"
    ExpiresByType video/ogg "access plus 1 month"
    ExpiresByType audio/ogg "access plus 1 month"
    ExpiresByType video/mp4 "access plus 1 month"
    ExpiresByType video/webm "access plus 1 month"

    # HTC files (css3pie)
    ExpiresByType text/x-component "access plus 1 month"

    # Webfonts
    ExpiresByType font/truetype "access plus 1 month"
    ExpiresByType font/opentype "access plus 1 month"
    ExpiresByType application/x-font-woff "access plus 1 month"
    ExpiresByType image/svg+xml "access plus 1 day"
    ExpiresByType application/vnd.ms-fontobject "access plus 1 month"

    # CSS and JavaScript
    ExpiresByType text/css "access plus 4 hours"
    ExpiresByType application/javascript "access plus 4 hours"
    ExpiresByType text/javascript "access plus 4 hours"

      <IfModule mod_headers.c>
        Header append Cache-Control "public"
      </IfModule>

    </IfModule>
    # Keep alive for persistent connections
    <IfModule mod_headers.c>
        Header set Connection Keep-Alive
    </IfModule>

    <IfModule mod_headers.c>
      Header unset ETag
    </IfModule>

    FileETag None

    <Directory /var/www/html/domain.tld>
        AllowOverride All
    </Directory>
</VirtualHost>
comments powered by Disqus