看板 Knuckles_note
作者 標題 [Apache] 安裝 PageSpeed 模組 改善網頁速度
時間 2014年09月09日 Tue. AM 02:32:28
安裝環境: CentOS 7
官網說明: https://www.modpagespeed.com/doc/
# 安裝 Pagespeed
到此頁下載安裝檔
https://www.modpagespeed.com/doc/download
要先安裝 at
$ sudo yum install at
安裝 mod-pagespeed-stable
$ sudo cd /usr/local/src
$ sudo wget https://dl-ssl.google.com/dl/linux/direct/mod-pagespeed-stable_current_x86_64.rpm
$ sudo rpm -U mod-pagespeed-*.rpm
重載 Apache
(CentOS 6)
$ sudo service httpd reload
(CentOS 7)
$ sudo systemctl reload httpd
要更新版本的話使用
$ sudo yum update mod-pagespeed-stable
然後重載 Apache
要切換 beta 版轉成 stable 版的話
$ sudo yum remove mod-pagespeed-beta
$ sudo yum install mod-pagespeed-stable
# 修改設定
$ sudo vim /etc/httpd/conf.d/pagespeed.conf
# 是否啟用,改成 off 可以關閉 ModPagespeed
ModPagespeed on
# RewriteLevel 預設是 CoreFilters,會使用幾個主要的 Filter
# 也可以改成 PassThrough 預設不使用任何 Filter,後面再自行加上要使用的 Filter
# ModPagespeedRewriteLevel PassThrough
# 設定好 RewriteLevel 後,如果想要關掉幾個特定的 Filter,可以加在這邊
# ModPagespeedDisableFilters rewrite_images
# 設定好 RewriteLevel 後,如果想要打開其他的 Filter,可以加在這邊
# ModPagespeedEnableFilters rewrite_javascript,rewrite_css
ModPagespeed on
# RewriteLevel 預設是 CoreFilters,會使用幾個主要的 Filter
# 也可以改成 PassThrough 預設不使用任何 Filter,後面再自行加上要使用的 Filter
# ModPagespeedRewriteLevel PassThrough
# 設定好 RewriteLevel 後,如果想要關掉幾個特定的 Filter,可以加在這邊
# ModPagespeedDisableFilters rewrite_images
# 設定好 RewriteLevel 後,如果想要打開其他的 Filter,可以加在這邊
# ModPagespeedEnableFilters rewrite_javascript,rewrite_css
CoreFilter 預設會使用的 Filter 為
add_head
combine_css
combine_javascript
convert_meta_tags
extend_cache
fallback_rewrite_css_urls
flatten_css_imports
inline_css
inline_import_to_link
inline_javascript
rewrite_css
rewrite_images
rewrite_javascript
rewrite_style_attributes_with_url
每個 Filter 的說明可參考
https://developers.google.com/speed/pagespeed/module/filters
例如 Filter: trim_urls
參考: https://www.modpagespeed.com/doc/filter-trim-urls
將網址路徑改成相對路徑
例如在 http://disp.cc/b/ 的網頁時
會將 <script src="http://disp.cc/b/xxx.js">
改成 <script src="xxx.js">
# 部份頁面停用 PageSpeed
除了在設定檔使用 ModPagespeed off 將PageSpeed整個關掉外
如果只想要在某些資料夾或檔案關閉的話
可以在網址加上 &ModPagespeed=off
或是用PHP加上 header("ModPagespeed: off");
或是在目錄裡的 .htaccess 加上
<IfModule pagespeed_module>
ModPagespeed off
</IfModule>
ModPagespeed off
</IfModule>
# 清除暫存檔
參考: https://developers.google.com/speed/pagespeed/module/system#flush_cache
暫存檔放在 /var/cache/mod_pagespeed
只要執行
$ touch /var/cache/mod_pagespeed/cache.flush
過個5秒之後就會清除暫存檔了
# 管理頁面
修改設定檔
$ sudo vim /etc/httpd/conf.d/pagespeed.conf
加上允許瀏覽管理頁面的 IP 位址 x.x.x.x
<Location /pagespeed_admin>
Order allow,deny
Allow from localhost
Allow from 127.0.0.1
Allow from x.x.x.x
SetHandler pagespeed_admin
</Location>
<Location /pagespeed_global_admin>
Order allow,deny
Allow from localhost
Allow from 127.0.0.1
Allow from x.x.x.x
SetHandler pagespeed_global_admin
</Location>
/pagespeed_admin 是只有這個 Virtual Host 的資料Order allow,deny
Allow from localhost
Allow from 127.0.0.1
Allow from x.x.x.x
SetHandler pagespeed_admin
</Location>
<Location /pagespeed_global_admin>
Order allow,deny
Allow from localhost
Allow from 127.0.0.1
Allow from x.x.x.x
SetHandler pagespeed_global_admin
</Location>
/pagespeed_global_admin 是所有網站的資料
重啟 Apache 後,用瀏覽器連到 https://網站位址/pagespeed_admin
□ 錯誤解決記錄
使用加密連線 https 時,css 和 js 會無法載入
因為被加上未加密的網址 http://
→ 改用直接路徑 ex: /css/index.css
--
※ 作者: Knuckles 時間: 2014-09-09 02:32:28
※ 編輯: Knuckles 時間: 2023-11-27 14:44:20 (台灣)
※ 看板: KnucklesNote 文章推薦值: 0 目前人氣: 0 累積人氣: 654
回列表(←)
分享