Last-modified: 2011-12-26 (月) 01:24:42
Apache/mod-pagespeedを試用してみたい

概要

webサイトの自動最適化モジュール、mod-pagespeedを試用してみます。
ソースからインストールしたApacheがターゲットです。
ただ、mod-pagespeedをコンパイルからするのは面倒なようなので、rpm版パッケージを分解してとりあえず動くようにしてみました。
Apacheのインストールに関しては、以下のページを参照ください。
ソースから導入

手順

  1. パッケージをダウンロードします。
    Everything is expanded.Everything is shortened.
      1
    
     
    
    wget https://dl-ssl.google.com/dl/linux/direct/mod-pagespeed-beta_current_i386.rpm
  2. パッケージを分解します。
    Everything is expanded.Everything is shortened.
      1
      2
      3
    
     
     
     
    
    mkdir mod-pagespeed
    cd mod-pagespeed
    rpm2cpio ../mod-pagespeed-beta_current_i386.rpm | cpio -id
  3. 手動で必要なファイルをコピーします。
    Everything is expanded.Everything is shortened.
      1
      2
      3
    
     
     
     
    
    cp etc/cron.daily/mod-pagespeed /etc/cron.daily/mod-pagespeed
    cp etc/httpd/conf.d/pagespeed.conf <Apacheルート>/conf/extra/
    cp usr/lib/httpd/modules/mod_pagespeed.so <Apacheルート>/modules/
  4. <Apacheルート>/conf/httpd.confを開きます。
    Everything is expanded.Everything is shortened.
      1
      2
    
    -
    !
    
    # PageSpeed用の設定を読み込むための設定を追加します
    Include conf/extra/pagespeed.conf
  5. <Apacheルート>/conf/extra/pagespeed.confを開きます。
    最適化の設定は検証時に使った一時的な設定です。
    Everything is expanded.Everything is shortened.
      1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
     29
     30
     31
     32
     33
     34
     35
     36
     37
     38
     39
     40
     41
     42
     43
     44
     45
     46
     47
     48
     49
     50
     51
     52
     53
     54
     55
     56
     57
     58
     59
     60
     61
     62
     63
     64
     65
     66
     67
     68
     69
     70
     71
     72
     73
     74
     75
     76
     77
     78
     79
     80
     81
     82
     83
     84
     85
     86
     87
     88
     89
     90
     91
     92
     93
     94
     95
     96
     97
     98
     99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    
    -
    |
    !
     
    -
    !
    -
    !
     
     
    -
    |
    !
     
    -
    |
    !
     
    -
    |
    |
    |
    |
    !
     
     
    -
    |
    |
    |
    |
    |
    |
    |
    !
     
    -
    |
    |
    |
    |
    |
    |
    |
    !
    -
    |
    |
    |
    |
    |
    !
     
    -
    |
    |
    |
    |
    |
    |
    |
    |
    |
    |
    |
    |
    |
    !
    -
    |
    |
    |
    |
    |
    |
    |
    |
    |
    !
    -
    |
    |
    !
    -
    |
    |
    |
    |
    |
    |
    !
     
    -
    |
    |
    |
    |
    |
    |
    !
     
     
    -
    |
    |
    |
    |
    |
    |
    |
    !
    -
    !
     
    -
    |
    |
    |
    |
    |
    |
    |
    !
     
     
    
    # /usr/lib/httpd/がApacheルートだという前提で絶対パスで設定されています
    # /usr/lib/httpd/までを削除し、相対パスで記述します
    LoadModule pagespeed_module modules/mod_pagespeed.so
     
    # Only attempt to load mod_deflate if it hasn't been loaded already.
    <IfModule !mod_deflate.c>
        # こちらも同様に相対パスにします
        LoadModule deflate_module modules/mod_deflate.so
    </IfModule>
    <IfModule pagespeed_module>
        # Turn on mod_pagespeed. To completely disable mod_pagespeed, you
        # can set this to "off".
        ModPagespeed on
     
        # Direct Apache to send all HTML output to the mod_pagespeed
        # output handler.
        AddOutputFilterByType MOD_PAGESPEED_OUTPUT_FILTER text/html
     
        # The ModPagespeedFileCachePath and
        # ModPagespeedGeneratedFilePrefix directories must exist and be
        # writable by the apache user (as specified by the User
        # directive).
        # 私は、パッケージ通りのディレクトリ構成が気に食わなかったので、パスを変更しました
        ModPagespeedFileCachePath            "/opt/www/pagespeed/cache/"
        ModPagespeedGeneratedFilePrefix      "/opt/www/pagespeed/files/"
     
        # Override the mod_pagespeed 'rewrite level'. The default level
        # "CoreFilters" uses a set of rewrite filters that are generally
        # safe for most web pages. Most sites should not need to change
        # this value and can instead fine-tune the configuration using the
        # ModPagespeedDisableFilters and ModPagespeedEnableFilters
        # directives, below. Valid values for ModPagespeedRewriteLevel are
        # PassThrough and CoreFilters.
        #
        ModPagespeedRewriteLevel CoreFilters
     
        # Explicitly disables specific filters. This is useful in
        # conjuction with ModPagespeedRewriteLevel. For instance, if one
        # of the filters in the CoreFilters needs to be disabled for a
        # site, that filter can be added to
        # ModPagespeedDisableFilters. This directive contains a
        # comma-separated list of filter names, and can be repeated.
        #
        # ModPagespeedDisableFilters rewrite_javascript
     
        # Explicitly enables specific filters. This is useful in
        # conjuction with ModPagespeedRewriteLevel. For instance, filters
        # not included in the CoreFilters may be enabled using this
        # directive. This directive contains a comma-separated list of
        # filter names, and can be repeated.
        #
        ModPagespeedEnableFilters collapse_whitespace,elide_attributes,remove_comments
     
        # ModPagespeedDomain
        # authorizes rewriting of JS, CSS, and Image files found in this
        # domain. By default only resources with the same origin as the
        # HTML file are rewritten. For example:
        #
        #   ModPagespeedDomain cdn.myhost.com
        #
        # This will allow resources found on http://cdn.myhost.com to be
        # rewritten in addition to those in the same domain as the HTML.
        #
        # Wildcards (* and ?) are allowed in the domain specification. Be
        # careful when using them as if you rewrite domains that do not
        # send you traffic, then the site receiving the traffic will not
        # know how to serve the rewritten content.
     
        # Other defaults (cache sizes and thresholds):
        # ModPagespeedFileCacheSizeKb          102400
        # ModPagespeedFileCacheCleanIntervalMs 3600000
        # ModPagespeedLRUCacheKbPerProcess     1024
        # ModPagespeedLRUCacheByteLimit        16384
        # ModPagespeedCssInlineMaxBytes        2048
        # ModPagespeedImgInlineMaxBytes        2048
        # ModPagespeedJsInlineMaxBytes         2048
        # ModPagespeedCssOutlineMinBytes       3000
        # ModPagespeedJsOutlineMinBytes        3000
     
        # Bound the number of images that can be rewritten at any one time; this
        # avoids overloading the CPU.  Set this to 0 to remove the bound.
        # ModPagespeedImgMaxRewritesAtOnce        8
     
        # When Apache is set up as a browser proxy, mod_pagespeed can record
        # web-sites as they are requested, so that an image of the web is built up
        # in the directory of the proxy administrator's choosing.  When ReadOnly is
        # on, only files already present in the SlurpDirectory are served by the
        # proxy.
        # ModPagespeedSlurpDirectory ...
        # ModPagespeedSlurpReadOnly on
     
     
        # Enables server-side instrumentation and statistics.  If this rewriter is
        # enabled, then each rewritten HTML page will have instrumentation javacript
        # added that sends latency beacons to /mod_pagespeed_beacon.  These
        # statistics can be accessed at /mod_pagespeed_statistics.  You must also
        # enable the mod_pagespeed_statistics and mod_pagespeed_beacon handlers
        # below.
        #
        ModPagespeedEnableFilters add_instrumentation
     
     
        # This handles the client-side instrumentation callbacks which are injected
        # by the add_instrumentation filter.
        # You can use a different location by adding the ModPagespeedBeaconUrl
        # directive; see the documentation on add_instrumentation.
        #
        # <Location /mod_pagespeed_beacon>
        #       SetHandler mod_pagespeed_beacon
        # </Location>
     
        # This page lets you view statistics about the mod_pagespeed module.
        <Location /mod_pagespeed_statistics>
            Order allow,deny
            # You may insert other "Allow from" lines to add hosts you want to
            # allow to look at generated statistics.  Another possibility is
            # to comment out the "Order" and "Allow" options from the config
            # file, to allow any client that can reach your server to examine
            # statistics.  This might be appropriate in an experimental setup or
            # if the Apache server is protected by a reverse proxy that will
            # filter URLs in some fashion.
            # mod_pagespeedのステータスを表示するページですが、localhostだけだと不便なので、LAN内からのアクセスも許可するようにしました
            Allow from <LANのIPの一部(例. 192.168.50)> localhost
            SetHandler mod_pagespeed_statistics
        </Location>
  6. 私と同じようにModPagespeedFileCachePathなどを変更したら、それに合わせてディレクトリを作成します。
    Everything is expanded.Everything is shortened.
      1
      2
      3
      4
      5
      6
    
     
     
     
     
     
     
    
    mkdir -p <ModPagespeedFileCachePathで設定したパス>
    mkdir -p <ModPagespeedGeneratedFilePrefixで設定したパス>
    chgown -R <Apache実効ユーザー> <ModPagespeedFileCachePathで設定したパス>
    chgown -R <Apache実効ユーザー> <ModPagespeedGeneratedFilePrefixで設定したパス>
    chggrp -R <Apache実効グループ> <ModPagespeedFileCachePathで設定したパス>
    chggrp -R <Apache実効グループ> <ModPagespeedGeneratedFilePrefixで設定したパス>
  7. Apacheを再起動します。
    Everything is expanded.Everything is shortened.
      1
    
     
    
    /etc/init.d/httpd restart
  8. サイトへアクセスして、firefoxのPageSpeedやYSlowなどを確認したり、<URL>/mod_pagespeed_statisticsへアクセスして稼動していることを確認します。

検証時の環境