本文將梳理Apache web網站服務器的相關知識,以及在CentOS6.7環境中簡單配置Apache web網站的相關用法!
一. Apache web程序安裝
利用光盤,製作本地RPM鏡像源,利用yum安裝httpd程序包。
(1)掛載本地光盤
[root@bogon ~]# mount /dev/sr0 /media/cdrom mount: block device /dev/sr0iswrite-protected, mounting read-only [root@bogon ~]#
(2)查看httpd程序包
[root@bogon~]#yum list | grep httpd* httpd.x86_64 2.2.15-53.el6.centos base httpd-devel.i686 2.2.15-53.el6.centos base httpd-devel.x86_64 2.2.15-53.el6.centos base httpd-manual.noarch 2.2.15-53.el6.centos base httpd-tools.x86_64 2.2.15-53.el6.centos base [root@bogon ~]#
說明:各RPM軟件包的用途如下:
httpd: 是Apache服務器的程序軟件包,包含服務器的執行程序、配置文件、啓動腳本等必備的文件
httpd-devel: 是Apache HTTP服務器的開發工具包
httpd-mannual: 是Apache 服務器的幫助手冊文檔,以網頁的形式提供了Apache服務器的完整說明文檔。
httpd-tools : 是 Apache HTTP服務器的工具包。
(3)yum安裝Apachehttpd程序包,利用yum安裝,可以充分解決程序包之間的依賴關係,簡單易用
[root@bogon ~]# yum -y install httpd Loaded plugins: fastestmirror, security Setting up Install Process Loading mirror speeds from cached hostfile * base: mirrors.neusoft.edu.cn * extras: mirrors.neusoft.edu.cn *updates:mirrors.neusoft.edu.cn<br>Resolving Dependencies --> Running transaction check ---> Packagehttpd.x86_640:2.2.15-53.el6.centos will be installed ...... Installed: httpd.x86_640:2.2.15-53.el6.centos Dependency Installed: apr.x86_64 0:1.3.9-5.el6_2 apr-util.x86_640:1.3.9-3.el6_0.1 apr-util-ldap.x86_64 0:1.3.9-3.el6_0.1 httpd-tools.x86_640:2.2.15-53.el6.centos Complete! [root@bogon ~]#
(4)查看httpd包安裝完成後,生成哪些文件
[root@bogon ~]# rpm -ql httpd /etc/httpd /etc/httpd/conf /etc/httpd/conf.d /etc/httpd/conf.d/README /etc/httpd/conf.d/welcome.conf /etc/httpd/conf/httpd.conf /etc/httpd/conf/magic /etc/httpd/logs /etc/httpd/modules /etc/httpd/run /etc/logrotate.d/httpd /etc/rc.d/init.d/htcacheclean /etc/rc.d/init.d/httpd /etc/sysconfig/htcacheclean /etc/sysconfig/httpd /usr/lib64/httpd /usr/lib64/httpd/modules /usr/lib64/httpd/modules/mod_actions.so ...... /usr/lib64/httpd/modules/mod_vhost_alias.so /usr/sbin/apachectl /usr/sbin/htcacheclean /usr/sbin/httpd ...... /usr/sbin/suexec<br>/usr/share/doc/httpd-2.2.15 ...... /usr/share/doc/httpd-2.2.15/VERSIONING /usr/share/man/man8/apachectl.8.gz ...... /usr/share/man/man8/suexec.8.gz /var/cache/mod_proxy /var/lib/dav /var/log/httpd /var/run/httpd /var/www /var/www/cgi-bin /var/www/error /var/www/error/HTTP_BAD_GATEWAY.html.var ...... /var/www/error/noindex.html /var/www/html var/www/icons var/www/icons/README /var/www/icons/README.html /var/www/icons/a.gif ...... /var/www/icons/world1.gif /var/www/icons/world1.png /var/www/icons/world2.gif /var/www/icons/world2.png [root@bogon ~]#
說明:
/etc/httpd ------->配置文件的目錄 /etc/rc.d/init.d/ ------->httpd服務啓動腳本目錄 /usr/lib64/httpd/modules ------->httpd服務所用模塊的目錄 /var/www ------->web服務器網站所有文件的根目錄 /var/www/error ------->web服務器的錯誤相關處理的文件 /var/www/html ------->web服務器靜態網頁的根目錄 /var/www/icons ------->web服務器網頁所調用的圖片的目錄
二. httpd的主要配置文件/etc/httpd/conf/httpd.conf的簡要說明:
/etc/httpd/conf/httpd.conf文件是http服務的主配置文件,其中包含的配置項直接決定着web服務器的各項運行參數及服務器性能,其內容主要包括以下三個部分:
Section 1: Global Environment ------> 全局環境配置部分 Section 2: 'Main' server configuration ------>主服務配置部分 Section 3: Virtual Hosts ------> 虛擬主機配置部分
httpd.conf 文件由註釋行和設置行兩部分組成。
註釋行以"#"開始,包含了對配置項進行說明和解釋的內容,可以通過閱讀註釋行的內容,快速獲得相關配置項的幫助信息。
設置行不以"#"開始,是配置文件中真正有效的設置內容。
常用設置項:
(1)ServerRoot 指令
設置Apache軟件的配置文件的主目錄。
語法格式:ServerRoot directory-path
默認:"/etc/httpd"
(2)Listen 指令:
設置服務器監聽的IP以及端口號,默認監聽服務器本機所有的IP地址的TCP 80端口。
語法格式:Listen [IP-address:]portnumber[protocol]
默認:Listen 80
當然你可以配置多個選項,來監控更多的端口:
示例: Listen 80
Listen 443
(3)LoadModule 指令
用來設置加載模塊的指令,Apache的功能大部分由模塊提供,並且支持動態裝載和卸載模塊(DSO), 模塊文件一般位於SystemRoot根目錄下的modules的目錄裏面
語法格式:LoadModule module filename
示例:
LoadModuleauth_basic_modulemodules/mod_auth_basic.so LoadModule auth_digest_modulemodules/mod_auth_digest.so LoadModuleauthn_file_modulemodules/mod_authn_file.so
(4)Include 指令
用來設置從哪個目錄中加載除主配置文件外的其他配置文件
語法格式:Include file-path|directory-path
默認:Includeconf.d/*.conf 相對SystemRoot根目錄。絕對路徑是:/etc/httpd/conf.d/*.conf
示例:
Include /usr/local/apache2/conf/ssl.conf Include/usr/local/apache2/conf/vhosts/*.conf
(5)User 指令和 Group 指令
用來設置啓動httpd進程的用戶及組名稱
(6)ServerAdmin 指令
通常來說當網站出現故障時,需要爲客戶提供一個可以幫助解決問題的郵件地址。例如管理網站的管理員地址。這條指令就是這種作用!
語法格式:ServerAdmin email-address|URL
示例:ServerAdmin [email protected]
(7)ServerName 指令
用來標識服務器本身的主機名和端口,這條指令常用於配合主服務配置和虛擬主機配置。
語法格式:ServerName [scheme://]fully-qualified-domain-name[:port]
示例:ServerName www.ipsecx.com
(8)DocumentRoot指令
用來設置客戶端訪問網站的根路徑,如果修改,請關閉Selinux服務。
語法格式:DocumentRoot directory-path
默認:DocumentRoot "/var/www/html"
(9)DirectoryIndex指令
用來設置客戶端請求網站時,要查找的網頁列表
語法格式:DirectoryIndex local-url[local-url] ...
默認:DirectoryIndex index.html index.html.var
(10)AllowOverride指令
httpd允許在網頁文檔的各目錄下使用.htaccess文件實現單目錄資源的訪問控制;表示哪此指令可以存放於.htaccess文件中。建議
語法格式:AllowOverride All|None|directive-type[directive-type] ...
開啓此項,會大量佔用服務器資源,建議AllowOverride的值設置爲 None
(11)Options指令
對指定的目錄設置選項。
常用Options:
Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews None All
None : 全部不允許
Indexes:允許索引,即當客戶端無法找到請求的首頁時,會列出當前目錄的所有文件索引。
FollowSymLinks:允許跟蹤符號鏈接;
SymLinksifOwnerMatch:如果匹配文件屬主,允許跟蹤符號鏈接
ExecCGI:允許執行CGI腳本;
...
語法格式:Options [+|-]option [[+|-]option] ...
示例:
<Directory /> Options FollowSymLinks AllowOverride None </Directory>
(12)order 指令
用來控制默認訪問狀態以及Allow與Deny的次序,如果使用Orderdeny,allow,則先會檢查拒絕,在檢查允許,當拒絕與允許有衝突時,允許優先,默認規則是允許。如果使用Order allow,deny則先檢查允許,在檢查拒絕,當允許與拒絕有衝突時,拒絕優先,默認規則是拒絕。
語法格式:Order ordering
默認:Order Deny,Allow
注意:在http-2.2的版本中,對用戶的訪問控制,默認是全部允許;在httpd-2.4的版本中,默認是全部禁止
(13)ErrorLog 指令
語法格式:ErrorLog file-path|syslog[:facility]
用來設置錯誤日誌的存放位置,默認:ErrorLog logs/error_log
(14)ErrorLogFormat指令:
設置錯誤日誌的格式,可以參考Apache的幫助文檔
(15)CustomLog指令:
設置客戶端的訪問日誌文件名以及日誌格式,默認爲 logs/access_log
語法格式:CustomLog file|pipeformat|nickname[env=[!]environment-variable]
示例:
# CustomLog with format nickname LogFormat "%h %l %u %t \"%r\" %>s %b" common CustomLog logs/access_log common
(16)LogFormat指令
該指令描述用戶日誌文件格式,可以直接使用Apache預先設置的格式字串,並且在配置的時候,我們會爲LogFormat指令設置的日誌格式創建別名,可以通過CustomLog指令調用該日誌格式別名!
語法格式:LogFormat format|nickname[nickname]
默認:LogFormat "%h%l %u %t \"%r\" %>s %b"
示例:LogFormat "%v%h %l %u %t \"%r\" %>s %b" vhost_common
(17)Alias指令
用來設置目錄別名,舉例來說,當你在瀏覽器通過某個路徑來訪問某個圖片時,實際上,web服務器內部已經將你輸入的路徑,切換到其他路徑了。
語法格式:Alias URL-path file-path|directory-path
示例:
Alias /images/ "/webdata/pictures/"
http://www.ipsecx.com/images/logo.jpg <-- /SrvData/pictures/logo.jpg
(18)NameVirtualHost指令
當你需要配置基於域名的虛擬主機,需要用到此項
語法格式:NameVirtualHost addr[:port]
好吧,關於Apache的主配置文件httpd.conf,簡單就說這麼多!
下面提供一些示例,供大家參考。
示例1:設置基於用戶的控制web站點訪問資源
basic認證的配置示例:
(1) 定義安全域
<Directory "/PATH/TO/SOME_DIR"> Options None AllowOverride None AuthType Basic AuthName "SOME_STRING_HERE" AutuUserFile "/PATH/TO/HT_PASSWD_FILE" Require user user1 user2 ... </Directory> # Require valid-user 所有用戶
(2) 創建賬號文件 htpasswd [options] /PATH/TO/HT_PASSWD_FILE USERNAME
-c:創建文件;
-m:md5加密密碼;
-s: SHA加密密碼;
-D:刪除指定用戶
注意:第一次創建賬號文件,命令選項必須有-c選項,需要向此賬號文件裏面添加賬號時,不在需要-c選項,否則就會覆蓋原來的賬號文件
示例2:基於域名的虛擬主機簡單配置:
(1)先在httpd.conf文件裏面修改下面項:以服務器IP爲172.16.30.100爲例
NameVirtualHost 172.16.30.100:80 監聽服務器的TCP 80 端口
(2)配置你的虛擬主機的內容,建議在/etc/httpd/conf.d/目錄下創建一一對應的配置文件
虛擬主機1
<VirtualHost 172.16.100.71:80> ----->設置監聽虛擬主機的IP和端口 ServerName www.ipsecx.com ----->設置主機域名地址 DocumentRoot /data/vhosts/ipsecx ----->設置網站資源的根目錄 </VirtualHost>
虛擬主機2
<VirtualHost 172.16.30.100:80> ServerName www.heihei.com DocumentRoot /data/vhosts/heihei </VirtualHost>
由於作者水平有限,本文錯漏缺點在所難免,希望讀者批評指正。謝謝!