2017-02-11

為 Apache 啟用 Brotli 網頁壓縮技術

在伺服器上,我並不是 Windows 愛用者…所以,雖然前篇我談了 IIS 8.5 + Brotli 的啟用,但現在就要來講講在 Linux + Apache 上啟用 Brotli

先當作已經有 Linux ,也用好了 Apache 了…

事前準備:

  1. 環境建置 (因為我有兩台伺服器)
    1. CentOS 7.3.1611 x64 + Apache 2.4.6 built 2016/11/14
    2. CentOS 6.8 (final) + Apache 2.2.15 built 2017/01/12
  2. kjdev/apache-mod-brotli
過程說明(linux 環境都文字,就不抓圖了)
  1. 無論 CentOS 7 或者 6 ,請先 yum 安裝 git, automake, libtool
  2. 使用 git 來 clone kjdev/apache-mod-brotli(這部份該網址有說明怎麼操作即安裝)
    git clone --depth=1 --recursive https://github.com/kjdev/apache-mod-brotli.git

    但要注意一下,上述指令執行會在你當前目錄環境下建立 clone 的資料,所以有規畫一點,可以先跳到你當放 git clone 的資料夾後再下指令,譬如我會跳到 opt 再執行指令
  3. 接著進行元件的 complie 與 make configure
    cd apache-mod-brotli
    ./autogen.sh
    ./configure
    make
    有些人可能會在執行 autogen.sh 時遇到錯誤…就以我而且,我自己在 CentOS 7 時遇到告知
    ./autogen.sh: line 22: aclocal:命令找不到
    這代表你系統沒有安裝 automake
    而遇到
    ./autogen.sh: line 25: libtoolize:命令找不到
    這代表你系統沒有安裝 libtool
    而理論上 ./autogen.sh 之後正常的結果應該是
    [root@test apache-mod-brotli]# ./autogen.sh
    Cleanup
    Running aclocal
    aclocal: warning: couldn't open directory 'm4': 沒有此一檔案或目錄
    Running libtoolize
    libtoolize: putting auxiliary files in `.'.
    libtoolize: copying file `./ltmain.sh'
    libtoolize: putting macros in AC_CONFIG_MACRO_DIR, `m4'.
    libtoolize: copying file `m4/libtool.m4'
    libtoolize: copying file `m4/ltoptions.m4'
    libtoolize: copying file `m4/ltsugar.m4'
    libtoolize: copying file `m4/ltversion.m4'
    libtoolize: copying file `m4/lt~obsolete.m4'
    Running autoheader
    Running automake
    configure.ac:13: installing './ar-lib'
    configure.ac:14: installing './config.guess'
    configure.ac:14: installing './config.sub'
    configure.ac:10: installing './install-sh'
    configure.ac:10: installing './missing'
    Makefile.am: installing './depcomp'
    Running autoconf
    不過,有一個例外,我在 CentOS 6.8 使用時發現,即便安裝好了 automake, libtool ,在執行 autogen.sh 仍會失敗,會出現以下訊息
    [root@test apache-mod-brotli]# ./autogen.sh
    Cleanup
    Running aclocal
    configure.ac:13: warning: macro `AM_PROG_AR' not found in library
    Running libtoolize
    libtoolize: putting auxiliary files in `.'.
    libtoolize: copying file `./ltmain.sh'
    libtoolize: putting macros in AC_CONFIG_MACRO_DIR, `m4'.
    libtoolize: copying file `m4/libtool.m4'
    libtoolize: copying file `m4/ltoptions.m4'
    libtoolize: copying file `m4/ltsugar.m4'
    libtoolize: copying file `m4/ltversion.m4'
    libtoolize: copying file `m4/lt~obsolete.m4'
    Running autoheader
    Running automake
    configure.ac:14: installing `./config.guess'
    configure.ac:14: installing `./config.sub'
    configure.ac:10: installing `./install-sh'
    configure.ac:10: installing `./missing'
    Makefile.am: installing `./depcomp'
    Running autoconf
    configure.ac:13: error: possibly undefined macro: AM_PROG_AR
    If this token and others are legitimate, please use m4_pattern_allow.
    See the Autoconf documentation.
    過程中我查了一下,似乎是因為 CentOS 6.8 使用的 libtool 2.2.6 並不包含 macro AM_PROG_AR 這個庫,而在 CentOS 7 的 libtool 2.4.2 是有的…所以最後有提醒你可以使用 m4_pattern_allow 避過這個問題,但好玩的是, m4_pattern_allow 要怎麼使用?當然,也是去翻了很多國外網頁,最後發現兩種方式可以,一個是屬於程式中告知的正規方式…你可以打開 configure.ac 這個檔,找到 AM_PROG_AR 的位置,應該是在第 13 行,此行改為 m4_pattern_allow([AM_PROG_AR]) 就行了;而另一個方式,就是在同樣 13 行的位置,在 AM_PROG_AR 最前頭加上個"#",變成「#AM_PROG_AR」,其實就是把這一行變成註解啦,跟刪掉這一行是一樣的…
  4. 再來就是把做好的 mod_brotli.so 改屬性後放到 apache 的 module 中啦
    install -p -m 755 -D .libs/mod_brotli.so /etc/httpd/modules/mod_brotli.so
  5. 最後,就是把 kjdev/apache-mod-brotli 中建議的 conf 讓 apache 載入
    # Load module
    LoadModule brotli_module modules/mod_brotli.so
    <ifmodule brotli_module>
    # Output filter
    #AddOutputFilterByType BROTLI text/html text/plain text/css text/xml
    # Compress HTML, CSS, JavaScript, Text, XML and fonts (此段我自己加的)
    AddOutputFilterByType BROTLI application/javascript
    AddOutputFilterByType BROTLI application/rss+xml
    AddOutputFilterByType BROTLI application/vnd.ms-fontobject
    AddOutputFilterByType BROTLI application/x-font
    AddOutputFilterByType BROTLI application/x-font-opentype
    AddOutputFilterByType BROTLI application/x-font-otf
    AddOutputFilterByType BROTLI application/x-font-truetype
    AddOutputFilterByType BROTLI application/x-font-ttf
    AddOutputFilterByType BROTLI application/x-javascript
    AddOutputFilterByType BROTLI application/xhtml+xml
    AddOutputFilterByType BROTLI application/xml
    AddOutputFilterByType BROTLI font/opentype
    AddOutputFilterByType BROTLI font/otf
    AddOutputFilterByType BROTLI font/ttf
    AddOutputFilterByType BROTLI image/svg+xml
    AddOutputFilterByType BROTLI image/x-icon
    AddOutputFilterByType BROTLI text/css
    AddOutputFilterByType BROTLI text/html
    AddOutputFilterByType BROTLI text/javascript
    AddOutputFilterByType BROTLI text/plain
    AddOutputFilterByType BROTLI text/xml
    # SetOutputFilter BROTLI
    # SetEnvIfNoCase Request_URI \.txt$ no-br

    # Compression
    ## BrotliCompressionLevel: 0-11 (default: 11)
    BrotliCompressionLevel 10

    ## BrotliWindowSize: 10-24 (default: 22)
    BrotliWindowSize 22

    # Specifies how to change the ETag header when the response is compressed
    ## BrotliAlterEtag: AddSuffix, NoChange, Remove (default: AddSuffix)
    BrotliAlterEtag AddSuffix

    # Filter note
    BrotliFilterNote Input brotli_in
    BrotliFilterNote Output brotli_out
    BrotliFilterNote Ratio brotli_ratio

    #LogFormat '"%r" %{brotli_out}n/%{brotli_in}n (%{brotli_ratio}n)' brotli
    #CustomLog logs/access_log brotli
    </IfModule>
    我有自己加了一些要壓縮的 type ,還有我最後把 log 給 disable 掉了
跟 IIS 啟用 Brotli 一樣也五步,記得要測試前,需要重新啟動 apache ,也別忘了只能找 https 的測試

沒有留言: