啓用Apache的deflate模塊,能夠開啓壓縮功能,減少網站傳輸時的帶寬。javascript
apache須要編譯安裝方式,以/usr/local/apache爲apache的安裝目錄,以源碼包/usr/local/src/httpd-2.4.33爲例php
首先查看/usr/local/apache/bin/apachectl -M ###查看裏面的模塊是否有deflate模塊,也能夠過濾一下/usr/local/apache/bin/apachectl -M | grep deflatecss
如有則到httpd.conf下面開啓,若是沒有顯示,或者用grep過濾後顯示Syntax OK,則說明模塊沒有安裝。html
以動態共享模塊方式(DSO)編譯安裝deflate模塊java
/usr/local/apache/bin/apxs -c -i -a /usr/local/src/httpd-2.4.33/modules/filters/mod_deflate.c #其中前面部分是安裝好的apache軟件裏面的apxs程序,後面是apache的源碼安裝包裏面的deflate壓縮模塊
apache
apxs程序編譯選項:bash
-c 表示執行編譯模塊操做app
-i 表示執行安裝模塊操做
ide
-a 表示自動添加一個LoadModule行到httpd.conf文件中,以激活該模塊svg
注意:若是提示-bash: /apache/bin/apxs: /replace/with/path/to/perl/interpreter: bad interpreter: No such file or directory。請安裝perl,perl-devel
yum install perl perl-devel
安裝完成後修改apxs腳本: vi /usr/local/apache/bin/apxs,將第一行「#!/replace/with/path/to/perl/interpreter -w」修改成#!/usr/bin/perl -w,wq保存退出,而後再次編譯該模塊
安裝完成後,vi /usr/local/apache/conf/httpd.conf中查看該模塊是否安裝及激活
LoadModule deflate_module modules/mod_deflate.so
注意:若是啓動apache提示httpd: Syntax error on line 105 of /apache/conf/httpd.conf: Cannot load modules/mod_deflate.so into server: /apache/modules/mod_deflate.so: undefined symbol: inflate
則說明deflate模塊沒有找到zlib的庫,能夠先安裝zlib,zlib-devel
yum install zlib zlib-devel
而後手動在httpd.conf下 加入如下命令
LoadFile /usr/lib64/libz.so ##libz.so爲deflate壓縮模塊所須要的庫文件
<ifmodule mod_deflate.c>
DeflateCompressionLevel 9
SetOutputFilter DEFLATE
DeflateFilterNote Input instream
DeflateFilterNote Output outstream
DeflateFilterNote Ratio ratio
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/xhtml+xml application/xml application/rss+xml application/atom_xml application/x-javascript application/x-httpd-php image/svg+xml image/gif image/png image/jpe image/swf image/jpeg image/bmp application/javascript
</ifmodule>
DeflateCompressionLevel 9 ###壓縮等級(1-9),等級越大效率越高,但同時消耗的CPU也高,根據具體狀況調優,也能夠設置爲6來平衡CPU及壓縮質量
AddOutputFilterByType ##僅壓縮限制特定的MIME類型文件