溫馨提示:
由于上一篇己經對http編譯安裝完成,這篇将介紹CGI、https、壓縮功能的啟用。
由于是編譯安裝,在編譯時己經指加載的了大多數的子產品,是以在配置CGI的過程中,隻要編輯/etc/httpd24/httpd.conf就可以了。
1、啟用CGI子產品
<a href="http://s3.51cto.com/wyfs02/M01/22/E1/wKioL1Mqi_CwefO5AABuBbN_NaE582.jpg" target="_blank"></a>
2、啟用别名子產品
<a href="http://s3.51cto.com/wyfs02/M00/22/E0/wKiom1MqjBfQ8W1xAABD90dtS8Y594.jpg" target="_blank"></a>
别名子產品的作用就是将ServerRoot中的cgi-bin目錄指向自定義位置
3、設定cgi-bin的别名目錄
<a href="http://s3.51cto.com/wyfs02/M02/22/E0/wKiom1MqlAWCpQt3AAJIei6wg1w901.jpg" target="_blank"></a>
提示:
什麼是處理器(Handler)
"處理器"是當一個檔案被調用時,Apache所執行操作的内部表現。檔案一般都有基于其檔案類型的隐含處理器。通常,檔案都隻是被伺服器簡單的送出,隻有某些檔案類型會被特别地"處理"。
常用指令
AddHandler:在檔案擴充名與特定的處理器之間建立映射
SetHandler:強制所有比對的檔案被一個指定的處理器處理
利用上一個篇中的服務腳本啟動服務
1
<code>#service httpd24 start</code>
4、書寫測試CGI腳本
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<code>#mkdir -p /web/www/ess/</code>
<code>#cd /web/www/ess/</code>
<code>#vim test.sh</code>
<code># cat /web/www/ess/test.sh</code>
<code>#!/bin/bash</code>
<code>cat</code> <code><< EOF</code>
<code>Content-Type: text</code><code>/html</code>
<code><pre></code>
<code>hello</code>
<code>new Time is : `</code><code>date</code><code>`</code>
<code>current user:\033[0m `</code><code>whoami</code><code>`</code>
<code>current direcory:`</code><code>pwd</code><code>`</code>
<code><</code><code>/pre</code><code>></code>
<code>EOF</code>
<code>#chmod +x test.sh</code>
5、測試CGI功能
<a href="http://s3.51cto.com/wyfs02/M01/22/E2/wKioL1MqmLeCqwtDAAE1Ycx48Mk136.jpg" target="_blank"></a>
竟然出錯了,好吧,看錯誤日志
<a href="http://s3.51cto.com/wyfs02/M02/22/E2/wKioL1MqmWfQyCnZAADMy1QjeHw314.jpg" target="_blank"></a>
注意:
APACHE錯誤日志:Premature end of script headers,或 malformed header from script 'filename': Bad header:XXX,這種情況,還是檢查一下CGI輸出的第一句話是啥。應該是形如:
Content-type:text/html\n\n
修改後格式
<a href="http://s3.51cto.com/wyfs02/M02/22/E1/wKiom1MqnBaRb_bYAACXS2Itx1Y562.jpg" target="_blank"></a>
再次測試
<a href="http://s3.51cto.com/wyfs02/M00/22/E1/wKiom1MqnHaCPm37AACpUn-3OlI626.jpg" target="_blank"></a>
總結:
編寫CGI程式
編寫CGI程式和"正常"程式之間有兩個主要的不同。
首先,在CGI程式的所有輸出前面必須有一個HTTP的MIME類型的頭,對用戶端指明所接收内容的類型,大多數情況下,像這樣:
Content-type: text/html
注:MIME (Multipurpose Internet Mail Extensions) 是描述消息内容類型的網際網路标準
其次,輸出要求是HTML形式的,或者是浏覽器可以顯示的其他某種形式。多數情況下,輸出是HTML形式的,但偶然也會輸出一個gif圖檔或者其他非HTML的内容。
除了這兩點,編寫CGI程式和編寫其他程式大緻相同。
二、https
2、開啟ssl功能
<code>#vim /etc/httpd24/httpd.conf</code>
<code>LoadModule ssl_module modules</code><code>/mod_ssl</code><code>.so</code>
<code># Secure (SSL/TLS) connections</code>
<code>Include </code><code>/etc/httpd24/extra/httpd-ssl</code><code>.conf</code>
3、登出根目錄限制,因為在後面要自定義站點目錄
<code>#<Directory /></code>
<code># AllowOverride none</code>
<code># Require all denied</code>
<code>#</Directory></code>
注:
如果在/etc/httpd24/httpd.conf檔案中沒有啟用LoadModule socache_shmcb_module modules/mod_socache_shmcb.so,在後面配置完/etc/httpd24/extra/httpd-ssl.conf啟動服務會報錯,SSLSessionCache需要這個子產品的支援。
3、修改配置檔案(/etc/httpd24/extra/httpd-ssl.conf)
<code><VirtualHost _default_:443></code>
<code>DocumentRoot </code><code>"/web/www/"</code>
<code>#指定目錄</code>
<code>ServerName www.essun.com:443</code>
<code>#指定服務名字</code>
<code>ErrorLog </code><code>"/usr/local/apache/logs/error_log"</code>
<code>TransferLog </code><code>"/usr/local/apache/logs/access_log"</code>
<code>SSLEngine on</code>
<code>啟用SSL功能</code>
<code>SSLCertificateFile </code><code>"/etc/httpd24/ssl_key/www.crt"</code>
<code>#指定web服務證書位置</code>
<code>SSLCertificateKeyFile </code><code>"/etc/httpd24/ssl_key/www.key"</code>
<code>#指定私鑰位置</code>
<code><</code><code>/VirtualHost</code><code>></code>
4、啟動服務
5、通路測試
在測試時要設定hosts檔案
win7 路徑為:C:\Windows\System32\drivers\etc
IP 主機名
<a href="http://s3.51cto.com/wyfs02/M01/22/E4/wKioL1MqvQLhI-g2AAItGvSDwpI131.jpg" target="_blank"></a>
點繼續。
<a href="http://s3.51cto.com/wyfs02/M02/22/E4/wKioL1MqvVHyJQBmAAEh1RjiDYQ450.jpg" target="_blank"></a>
将CA的證書與web伺服器的證書導出并添加到受信任根證書
<a href="http://s3.51cto.com/wyfs02/M00/22/E4/wKioL1MqvrCyneTpAAEu9Z8lQ1Y524.jpg" target="_blank"></a>
1、将CA的證書導出後,将字尾改為.crt,輕按兩下安裝,指定存儲路徑,添加到受信任的根證書頒發機構。
2、安裝web伺服器證書
3、檢視證書資訊
<a href="http://s3.51cto.com/wyfs02/M02/22/E4/wKioL1MqwKqgLQw6AAE772M-a94219.jpg" target="_blank"></a>
三、虛拟主機
1、基于不同的FQDN通路。
前提:
如果是自定義目錄,請登出/etc/httpd24/httpd.conf中
如果沒有關閉,将會無法通路自定義目錄
設定主配置檔案(/etc/httpd24/httpd.conf),啟用虛拟主機配置檔案
<code># Virtual hosts</code>
<code>Include </code><code>/etc/httpd24/extra/httpd-vhosts</code><code>.conf</code>
在<code>/etc/httpd24/extra/httpd-vhosts</code><code>.conf書寫配置檔案</code>
<code></code>
<code><VirtualHost *:80></code>
<code># ServerAdmin [email protected]</code>
<code> </code><code>DocumentRoot </code><code>"/web/html/ess/"</code>
<code> </code><code>ServerName www.jgpserver.com</code>
<code># ServerAlias www.dummy-host.example.com</code>
<code> </code><code>ErrorLog </code><code>"logs/dummy-host.example.com-error_log"</code>
<code> </code><code>CustomLog </code><code>"logs/dummy-host.example.com-access_log"</code> <code>common</code>
<code># ServerAdmin [email protected]</code>
<code> </code><code>DocumentRoot </code><code>"/web/html/test/"</code>
<code> </code><code>ServerName www.jgpserver.com.cn</code>
<code> </code><code>ErrorLog </code><code>"logs/dummy-host2.example.com-error_log"</code>
<code> </code><code>CustomLog </code><code>"logs/dummy-host2.example.com-access_log"</code> <code>common</code>
建立目錄與預設頁面
16
17
18
19
20
21
22
23
24
25
26
27
<code>#mkdir -p /web/html/{ess,test}</code>
<code>#cd /web/html/ess</code>
<code>#vim index.html</code>
<code><!DOCTYPE html PUBLIC </code><code>"-//W3C//DTD XHTML 1.1//EN"</code> <code>"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"</code><code>></code>
<code><html></code>
<code> </code><code><</code><code>head</code><code>></code>
<code> </code><code><meta http-equiv=</code><code>"Content-Type"</code> <code>content=</code><code>"text/html; charset=gb2312"</code> <code>/></code>
<code> </code><code><title>FQDN<</code><code>/title</code><code>></code>
<code> </code><code><</code><code>/head</code><code>></code>
<code> </code><code><body></code>
<code> </code><code><p></code><code>hostname</code> <code>is :www.jgpserver.com<</code><code>/p</code><code>></code>
<code> </code><code><p>ip is:192.168.1.114<</code><code>/p</code><code>></code>
<code> </code><code><</code><code>/body</code><code>></code>
<code><</code><code>/html</code><code>></code>
<code>#---------------------------------------------</code>
<code>#vim /web/html/test/index.html</code>
<code> </code><code><title>FQDN_cn<</code><code>/title</code><code>></code>
<code> </code><code><p></code><code>hostname</code> <code>is :www.jgpserver.com.cn<</code><code>/p</code><code>></code>
<code></code>重新開機服務,測試
測試www.jgpserver.com
<a href="http://s3.51cto.com/wyfs02/M01/22/E6/wKioL1Mq_uDBsi0kAAB4jdfQqHM334.jpg" target="_blank"></a>
測試www.jgpserver.com.cn
<a href="http://s3.51cto.com/wyfs02/M01/22/E5/wKiom1Mq_zaDd4dvAAB-2HK3tR4539.jpg" target="_blank"></a>
2、基于IP位址的虛拟主機
在Web服務端添加多塊虛拟網卡,以便測試
<a href="http://s3.51cto.com/wyfs02/M00/22/E6/wKioL1MrAsmj15CnAAO2O6EO_rQ860.jpg" target="_blank"></a>
修改配置檔案(/etc/httpd24/extra/httpd-vhosts.conf )
<a href="http://s3.51cto.com/wyfs02/M00/22/E5/wKiom1MrBMaR56mPAAKEm1QgLGM283.jpg" target="_blank"></a>
重新開機服務,測試
<code>#service httpd24 restart</code>
測試ip:192.168.1.234
<a href="http://s3.51cto.com/wyfs02/M01/22/E6/wKioL1MrBOejUSybAABfpr_0beI522.jpg" target="_blank"></a>
測試ip:192.168.1.123
<a href="http://s3.51cto.com/wyfs02/M00/22/E6/wKioL1MrBTXQfq1kAABqpteuX14030.jpg" target="_blank"></a>
3、基于端口的虛拟主機
在主配置檔案(/etc/httpd24/httpd.conf)中添加監聽端口
<a href="http://s3.51cto.com/wyfs02/M01/22/E6/wKioL1MrBjCS9K6PAABFzh246Iw196.jpg" target="_blank"></a>
修改虛拟主機配置檔案(/etc/httpd24/extra/httpd-vhosts.conf )
文法檢查、重新開機服務
<a href="http://s3.51cto.com/wyfs02/M02/22/E6/wKioL1MrB5yApnR6AACYPYBdSH8774.jpg" target="_blank"></a>
檢視端口是否開啟
<a href="http://s3.51cto.com/wyfs02/M01/22/E5/wKiom1MrB-rzCgHsAACFuDqL4a8806.jpg" target="_blank"></a>
測試端口80
<a href="http://s3.51cto.com/wyfs02/M00/22/E5/wKiom1MrC-mAjl96AABafsygNO8559.jpg" target="_blank"></a>
測試端口4800
測試5800
<a href="http://s3.51cto.com/wyfs02/M02/22/E5/wKiom1MrDDKwrD6yAACCcbw5o3s817.jpg" target="_blank"></a>
四、文本壓縮
好處:
經過壓縮後實際上降低了網絡傳輸的位元組數,最明顯的好處就是可以加快網頁加載的速度。網頁加載速度加快的好處不言而喻,除了節省流量,改善使用者的浏覽體驗
Web伺服器處理HTTP壓縮的過程如下:
① Web伺服器接收到浏覽器的HTTP請求後,檢查浏覽器是否支援HTTP壓縮
(Accept-Encoding 資訊);
② 如果浏覽器支援HTTP壓縮,Web伺服器檢查請求檔案的字尾名;
③ 如果請求檔案是HTML、CSS等靜态檔案,Web伺服器到壓縮緩沖目錄中檢查是否已經存在請求檔案的最新壓縮檔案;
④ 如果請求檔案的壓縮檔案不存在,Web伺服器向浏覽器傳回未壓縮的請求檔案,并在壓縮緩沖目錄中存放請求檔案的壓縮檔案;
⑤ 如果請求檔案的最新壓縮檔案已經存在,則直接傳回請求檔案的壓縮檔案;
1、在主配置檔案/etc/httpd24/httpd.conf檔案中啟用 deflate子產品
<a href="http://s3.51cto.com/wyfs02/M00/22/E6/wKioL1MrDlvxTTsaAAB40VAGOyc120.jpg" target="_blank"></a>
MIME (Multipurpose Internet Mail Extensions) 是描述消息内容類型的網際網路标準
2、定義壓縮類型、壓縮等級、對特殊浏覽器不支援壓縮的定義
<code># Level of compression (Highest 9 - Lowest 1)</code>
<code>DeflateCompressionLevel 9</code>
<code># Netscape 4.x has some problems.</code>
<code>BrowserMatch ^Mozilla</code><code>/4</code> <code>gzip</code><code>-only-text</code><code>/html</code>
<code># Netscape 4.06-4.08 have some more problems</code>
<code>BrowserMatch ^Mozilla</code><code>/4</code><code>\.0[678] no-</code><code>gzip</code>
<code># MSIE masquerades as Netscape, but it is fine</code>
<code>BrowserMatch \bMSI[E] !no-</code><code>gzip</code> <code>!</code><code>gzip</code><code>-only-text</code><code>/html</code>
<code><IfModule deflate_module></code>
<code>AddOutputFilterByType DEFLATE text</code><code>/plain</code>
<code>AddOutputFilterByType DEFLATE text</code><code>/html</code>
<code>AddOutputFilterByType DEFLATE text</code><code>/xml</code>
<code>AddOutputFilterByType DEFLATE text</code><code>/css</code>
<code>AddOutputFilterByType DEFLATE text</code><code>/javascript</code>
<code>AddOutputFilterByType DEFLATE application</code><code>/xhtml</code><code>+xml</code>
<code>AddOutputFilterByType DEFLATE application</code><code>/xml</code>
<code>AddOutputFilterByType DEFLATE application</code><code>/rss</code><code>+xml</code>
<code>AddOutputFilterByType DEFLATE application</code><code>/atom_xml</code>
<code>AddOutputFilterByType DEFLATE application</code><code>/x-javascript</code>
<code>AddOutputFilterByType DEFLATE application</code><code>/x-httpd-php</code>
<code><</code><code>/IfModule</code><code>></code>
3、檢視結果
<a href="http://s3.51cto.com/wyfs02/M01/22/E6/wKioL1MrEYSx4bmoAAPFNqDyAeI734.jpg" target="_blank"></a>
五、利用mod_status檢視apache服務相關資訊
1、啟用狀态子產品、info檔案
<code>LoadModule status_module modules</code><code>/mod_status</code><code>.so</code>
<code># Real-time info on requests and configuration</code>
<code>Include </code><code>/etc/httpd24/extra/httpd-info</code><code>.conf</code>
2、修改<code>/etc/httpd24/extra/httpd-info</code><code>.conf檔案</code>
Require [host|ip]對那一個ip或主機響應
3、重新開機服務、測試
<a href="http://s3.51cto.com/wyfs02/M02/22/E6/wKioL1MrFLTg83E_AAK2xXkpd8A586.jpg" target="_blank"></a>
======================================完=============================================
本文轉自 jinlinger 51CTO部落格,原文連結:http://blog.51cto.com/essun/1380687,如需轉載請自行聯系原作者