Apache 簡介及基本配置

來源:互聯網
上載者:User
一. Apache簡介: 
Apache是世界排名第一的Web伺服器, 根據Netcraft(www.netsraft.co.uk)所作的調查,世界上百分之五十以上的Web伺服器在使用Apache. 
1995年4月, 最早的Apache(0.6.2版)由Apache Group公布發行. Apache Group 是一個完全通過Internet進行運作的非盈利機構, 由它來決定Apache Web伺服器的標準發行版中應該包含哪些內容. 准許任何人修改隱錯, 提供新的特徵和將它移植到新的平台上, 以及其它的工作. 當新的代碼被提交給Apache Group時, 該團體審核它的具體內容, 進行測試, 如果認為滿意, 該代碼就會被整合到Apache的主要發行版中. 
Apache 的特性: 
1) 幾乎可以運行在所有的電腦平台上. 
2) 支援最新的HTTP/1.1協議 
3) 簡單而且強有力的基於檔案的配置(HTTPD.CONF). 
4) 支援通用閘道介面(CGI) 
5) 支援虛擬機器主機. 
6) 支援HTTP認證. 
7) 整合PERL. 
8) 整合的Proxy 伺服器 
9) 可以通過WEB瀏覽器監視伺服器的狀態, 可以自訂日誌. 
10) 支援伺服器端包含命令(SSI). 
11) 支援安全SOCKET層(SSL). 
12) 具有使用者會話過程的跟蹤能力. 
13) 支援FASTCGI 
14) 支援JAVA SERVLETS. 
... 
"缺點"(MAYBE IT IS, MAYBE NOT): 
APACHE沒有為管理員提供圖形使用者介面(GUI), 但最近的APACHE版本已經有了GUI的支援. 

以下是APACHE的許可證 
/* ==================================================================== 
* The Apache Software License, Version 1.1 

* Copyright (c) 2000 The Apache Software Foundation. All rights 
* reserved. 

* Redistribution and use in source and binary forms, with or without 
* modification, are permitted provided that the following conditions 
* are met: 

* 1. Redistributions of source code must retain the above copyright 
* notice, this list of conditions and the following disclaimer. 

* 2. Redistributions in binary form must reproduce the above copyright 
* notice, this list of conditions and the following disclaimer in 
* the documentation and/or other materials provided with the 
* distribution. 

* 3. The end-user documentation included with the redistribution, 
* if any, must include the following acknowledgment: 
* "This product includes software developed by the 
* Apache Software Foundation (http://www.apache.org/)." 
* Alternately, this acknowledgment may appear in the software itself, 
* if and wherever such third-party acknowledgments normally appear. 

* 4. The names "Apache" and "Apache Software Foundation" must 
* not be used to endorse or promote products derived from this 
* software without prior written permission. For written 
* permission, please contact apache@apache.org. 

* 5. Products derived from this software may not be called "Apache", 
* nor may "Apache" appear in their name, without prior written 
* permission of the Apache Software Foundation. 

* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 
* SUCH DAMAGE. 
* ==================================================================== 

* This software consists of voluntary contributions made by many 
* individuals on behalf of the Apache Software Foundation. For more 
* information on the Apache Software Foundation, please see 
* <http://www.apache.org/>. 

* Portions of this software are based upon public domain software 
* originally written at the National Center for Supercomputing Applications, 
* University of Illinois, Urbana-Champaign. 
*/ 

可以看出, APACHE不要求任何人把對APACHE的修改公開(不受GPL條款約束). 

二. Apache的基本配置及說明: 

1.獲得Apache: 

獲得apache原始碼最方便的方法是到http://www.apache.org,選擇最新的版本。然後, 
tar xvzf apache_x.x.xx.tar.gz 

2. 編譯Apache 
在src目錄下 
./configure 可用 ./configure--help|less 得到協助, 
make, make install, install 

3. apache的啟動和關閉 
sbin/apachectl start 
sbin/apachectl stop 
sbin/apachectl restart (重起) 

4. 一些模組介紹: 
下列的組模組控制進入和驗證 
Acess 進入控制 
auth 檢查Unix-style口令和組檔案的基本的驗證。 
auth-anon 使用匿名的FTP-style usernames和口令。 
auth-dbm 使用Berkeley db檔案儲存體usernames,口令,和組資訊。如果你有很多usernames並關心效能, 這是很有用的。 
digest 使用HTTP文摘認證,這比基本認證更安全。 
expire 模組讓你定製Expire到期時間 

5. Apache1.3的預設目錄布局是: 
./bin 
伺服器二進位,apachectl原本 
./conf 
Apache設定檔 
./include/apache 
Apache標題檔案 
./libexec 
共用的對象檔案 
./man 
man 頁 
./sbin 
伺服器二進位,apachectl原本,log rotator 
./cgi-bin 
預設CGI原本/二進位的目錄 
./share/htdocs 
預設檔案根 
./htdocs/manual 
Apache手冊 
./share/icon 
包含各種表徵圖GIFs的目錄 
./var/apache/log 
log檔案的目錄 
./var/apache/proxy 
proxy緩衝目錄 
./var/apache/run 
httpd.pid(包含主要的httpd過程標識符的檔案)在此目錄 

6. 主要檔案如下: 
httpd.conf, 在/usr/local/apache/conf或/etc/httpd/conf下 
apachectl, 在/usr/local/apache/bin或/etc/httpd/bin下 
access_log, 紀錄使用者訪問資訊, 在../logs裡 
error_log, 紀錄使用者訪問錯誤資訊, 也在../logs裡 

7. httpd.conf 基本設定 
Port, Apache 在哪一個 TCP 連接埠進行偵聽,HTTP 的預設連接埠為 80。如果你使用其它連接埠,你不得不在 URLs 中指定連接埠來串連你的伺服器, 

User,Group 由於安全上的原因,在 Linux 上,可以通過把 User 和 Group 都設定成 nobody 

ServerRoot 參數應該是 Apache 安裝的基礎。除非你移動了周圍的資料,否則其值與你所給定的值一樣。 

ServerName, 如果你的機器有多個名稱或者 IP 地址,設定 Apache 表徵伺服器的名稱。 

DocumentRoot 是 Web 樹的根目錄, 

UserDir 在啟用 ~username 處理請求時, 此指令告訴 Apache 哪個目錄追加到使用者的主目錄(home)。UserDir 被設定為 public_html。使用者將只有基本的網頁服務功能, 

<Directory>是一個容器指令。</Directive>格式的結束指令。 

<Directive>及相關的<Files>和<FilesMatch>指令的目的,是為了讓你在每個目錄或者每個檔案的基礎上,控制Apache行為的方式。指令放在<Directory>中,也適用於子目錄。 

FollowSymLinks選項,告訴 Apache 它可以從主目錄內部到主目錄外部跟蹤符號連結。在主目錄內部,FollowSymLinks 和 Indexes 都處於開啟狀態(on)。萬一預設的索引檔案(通常為 index.html)不存在時,最後允許目錄列表顯示。 

Directory 中,用 Options 指令你可以控制什麼選項是被允許的。 

Options 指令: 
1) ExecCGI CGI 指令碼程式能夠運行於該分類樹。 
2) FollowSymLinks 在該目錄中,伺服器將跟蹤符號連結。注意:即使伺服器跟蹤符號連結,它也不會改變用來匹配不同地區的路徑名。注意:如果在地區內設定,該選項會被忽略。 
3) Includes 伺服器端包容被允許。 
4) IncludesNOEXEC 伺服器端引用被允許,但 #exec 命令和 CGI 指令碼程式的 #include 被禁止。 
5) Indexes 如果要求映射到目錄的 URL 和在該目錄裡沒有 DirectoryIndex(例如,index.html),那麼伺服器將返回格式化的目錄列表。 
6) MultiViews 處理多重瀏覽內容被允許。 
7) SymLinksIfOwnerMatch 通過與使用者 id 一樣的連結,伺服器只跟蹤目標檔案或者目錄所擁有的符號連結。 
8) All 除 MultiViews 外的一切東西。要是沒有 Options 指令,預設為all. 

AccessFileName 命令來設定(預設設定為 .htaccess)。在任何目錄中,Apache 配置指令可被放入一個叫做 .htaccess 的檔案。指令也可應用於目錄裡的子目錄。在訪問檔案中, 

AllowOverride 控制哪個指令將被使用。如果 AllowOverride 被設定為 none。Apache 甚至不會檢查訪問檔案。 

AllowOverride 參數: 
1) AuthConfig, 授權指令哪一個允許使用; 
2) FileInfo,用來控制文件類型和處理方法的指令; 
3)Indexes,使用其中的一個指令來控制目錄列表、表徵圖等的顯示; 
4) Limit,允許使用指令來控制主機訪問; 
5) Options,使用 Options 指令中的一個。 

“allow”和“deny”指令就是用來控制訪問的。第三個指令“order”,控制這些指令的使用順序。如果你把順序設定成 allow,deny,而沒有指定哪個主機是被拒絕的話,則所有主機都被假定為禁止訪問。在本例子中,allow 指令被選中,deny 指令被普遍應用。在 .htaccess 檔案或者 <Directory> 指令內,更多典型的配置可能是: 
order allow,deny 
allow from all 
deny aaa.com 
所有主機被允許,除了那些來自 aaa.com 域的主機。 

如果相反, 
order deny,allow 
deny from all 
allow from 11.23.44 
deny 首先被檢查。只有來自 11.23.44 IP 網路的主機才被允許訪問。它也可能指定網路遮罩,參數 11.23.44.0/255.255.255.0 相當於上面的例子。 

三. 特殊配置: 
1. 支援使用者個人首頁: 
在../conf/httpd.conf中加入 
UserDir /home/*/public_html 
設定使用者可以使用CGI等: 
<Directory /home/*/public_html/cgi-bin> 
options ExecCGI 
SetHandler cgi-bin #如果在全域設定了AddHandler cgi-bin .cgi ..., 
#這兒就不用設了 
</Directory> 

2. 虛擬機器主機設定: 

1)基於IP的虛擬機器主機 
若該機器的IP地址為192.168.0.2,虛擬IP為192.168.0.3 
首先, 在一台機器的網卡上綁定多個IP地址去服務多個虛擬機器主機。 
#/sbin/ifconfig eth0:0 192.168.0.2 netmask 255.255.255.0 broadcast 192.168.11.255 up 
#/sbin/route add -host 192.168.0.2 eth0:0 

設定../conf/httpd.conf檔案: 
看DocumentRoot是什麼, 如果為/home/httpd 
<VirtualHost 192.168.0.3> 
DocumentRoot /home/httpd/aaa 
</VirtualHost> 

建立目錄 
/home/httpd/aaa, 

2)基於名字的虛擬機器主機: 
編輯../conf/httpd.conf 

NameVirtualHost 192.168.0.2 
<VirtualHost 192.168.11.2> 
DocumentRoot /home/httpd/aaa 
ServerName aaa.yourdomain.com 
</VirtualHost> 

要有NameVirtualHost在最前面,下面依次為各個虛擬機器主機, 
還需要把aaa.yourdomain.com的DNS都指向IP地址192.168.0.2 

3. Apache使用者認證 
基本的Apache使用者認證 
假定, 對目錄/home/aaa下的檔案需要做到使用者認證,在httpd.conf 
中加入下面的行 
<Directory "/home/aaa"> 
allowoverride authconfig 
order allow,deny 
allow from all 
</Directory> 
用在目錄/home/aaa下放檔案.htaccess,內容如下: 
authname "Please give me your ID number and Password" 
authtype basic 
authuserfile /etc/.passwd 
require valid-user 

用htpasswd 組建檔案/etc/.passwd, 
htpasswd -c /etc/.passwd username 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.