httpd配置文件詳解及實例
html
做者:尹正傑前端
版權聲明:原創做品,謝絕轉載!不然將追究法律責任。node
1 [root@yinzhengjie ~]# cat /etc/redhat-release 2 CentOS release 6.6 (Final) 3 [root@yinzhengjie ~]# 4 [root@yinzhengjie ~]# rpm -q httpd 5 httpd-2.2.15-39.el6.centos.x86_64 6 [root@yinzhengjie ~]# 7 [root@yinzhengjie ~]# rpm -ql httpd | head 8 /etc/httpd ------>運行目錄; 9 /etc/httpd/conf ------>主配置目錄; 10 /etc/httpd/conf.d/*.conf ------->擴展配置目錄; 11 /etc/logrotate.d/httpd ------>日誌滾動目錄; 12 /usr/sbin/httpd ------>可執行命令目錄; 13 /var/www/html/ ------>文檔根目錄,全部須要請求的文件都放在該目錄下; 14 /var/www/cgi-bin/ ------>CGI目錄; 15 /etc/httpd/logs ----->日誌目錄,其實他是一個連接; 16 /etc/httpd/modules ------>存放各類不一樣模塊的目錄,其實它也是一個連接; 17 /etc/httpd/run ------>保存運行的pid; 18 /etc/sysconfig/httpd ------->腳本的配置文件; 19 /etc/init.d/httpd ------->服務啓動腳本; 20 .......... 21 [root@yinzhengjie ~]# 22 [root@yinzhengjie ~]# cd /etc/httpd/ 23 [root@yinzhengjie httpd]# ll 24 total 8 25 drwxr-xr-x. 2 root root 4096 Sep 18 07:34 conf 26 drwxr-xr-x. 2 root root 4096 Sep 18 07:34 conf.d 27 lrwxrwxrwx. 1 root root 19 Sep 18 07:34 logs -> ../../var/log/httpd 28 lrwxrwxrwx. 1 root root 29 Sep 18 07:34 modules -> ../../usr/lib64/httpd/modules 29 lrwxrwxrwx. 1 root root 19 Sep 18 07:34 run -> ../../var/run/httpd 30 [root@yinzhengjie httpd]#
[root@yinzhengjie ~]# service httpd configtestpython
1 [root@yinzhengjie ~]# httpd -l 2 Compiled in modules: 3 core.c --------->核心模塊 4 prefork.c --------->支持的MPM,早httpd2.2版本編譯時,有且只能有指定一種MPM。 5 http_core.c --------->http的核心模塊 6 mod_so.c --------->支持DSO的機制 7 [root@yinzhengjie ~]#
1 [root@yinzhengjie ~]# httpd.worker -l 2 Compiled in modules: 3 core.c 4 worker.c --------->你會發現他們只是MPM的類型不一樣。若是想要啓動worker模式能夠用該命令。 5 http_core.c 6 mod_so.c 7 [root@yinzhengjie ~]#
1 [root@yinzhengjie ~]# more /etc/sysconfig/httpd | grep HTTPD=
2 #HTTPD=/usr/sbin/httpd.worker ------->服務默認是講worker模式是關閉的。而是用的httpd 3 [root@yinzhengjie ~]# 4 [root@yinzhengjie ~]# ps aux | grep httpd | grep -v grep
5 root 2404 0.0 0.3 183936 3828 ? Ss 18:44 0:00 /usr/sbin/httpd 6 apache 2407 0.0 0.3 184072 3168 ? S 18:44 0:00 /usr/sbin/httpd 7 apache 2408 0.0 0.3 184072 3080 ? S 18:44 0:00 /usr/sbin/httpd 8 apache 2409 0.0 0.3 184072 3080 ? S 18:44 0:00 /usr/sbin/httpd 9 apache 2410 0.0 0.3 184072 3096 ? S 18:44 0:00 /usr/sbin/httpd 10 apache 2411 0.0 0.3 184072 3084 ? S 18:44 0:00 /usr/sbin/httpd 11 apache 2412 0.0 0.3 184072 3156 ? S 18:44 0:00 /usr/sbin/httpd 12 apache 2413 0.0 0.3 184072 3156 ? S 18:44 0:00 /usr/sbin/httpd 13 apache 2414 0.0 0.2 183936 2468 ? S 18:44 0:00 /usr/sbin/httpd 14 [root@yinzhengjie ~]# 15 [root@yinzhengjie ~]# more /etc/sysconfig/httpd | grep HTTPD=
16 HTTPD=/usr/sbin/httpd.worker --------------->固然,咱們能夠手動吧worker功能給打開。 17 [root@yinzhengjie ~]# service httpd restart 18 Stopping httpd: [ OK ] 19 Starting httpd: httpd.worker: Could not reliably determine the server's fully qualified domain name, using 192.168.1.200 for ServerName ------------->這行不用管,緣由是你的主機名和IP對應的不一致,該服務會自動進行反解的,只要服務能夠正常啓動便可。
20 [ OK ] 21 [root@yinzhengjie ~]# 22 [root@yinzhengjie ~]# ps aux | grep httpd | grep -v grep
23 root 2848 0.0 0.4 184140 4036 ? Ss 19:44 0:00 /usr/sbin/httpd.worker 24 apache 2852 0.6 0.5 593936 5368 ? Sl 19:44 0:00 /usr/sbin/httpd.worker 25 apache 2853 0.0 0.5 528400 5368 ? Sl 19:44 0:00 /usr/sbin/httpd.worker 26 apache 2854 0.4 0.5 528400 5372 ? Sl 19:44 0:00 /usr/sbin/httpd.worker 27 [root@yinzhengjie ~]#
Match | Allow,Deny result | Deny,Allow result |
Match Allow only | Request allowed | Request allowed |
Match Deny only | Request denied | Request denied |
No match | Default to second directive: Denied | Default to second directive: Allowed |
Match both Allow & Deny | Final match controls: Denied | Final match controls: Allowed |
1 [root@yinzhengjie ~]# more /etc/httpd/conf/httpd.conf | grep -v ^# | grep -v ^$ | grep ScriptAlias 2 ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
3 [root@yinzhengjie ~]# 4 [root@yinzhengjie ~]# more /var/www/cgi-bin/yinzhengjie 5 #!/bin/bash 6 #@author :yinzhengjie 7 #blog:http://www.cnblogs.com/yinzhengjie
8 #EMAIL:y1053419035@qq.com 9
10 cat <<EOF 11 Content-Type:text/html 12
13 <pre>
14 <h1>The hostname is `hostname`.</h1>
15 The time is `date +%F` 16 <h1>My name is yinzhengjie!</h1>
17 </pre>
18
19 EOF 20 [root@yinzhengjie ~]# 21 [root@yinzhengjie ~]# chmod +x /var/www/cgi-bin/yinzhengjie 22 [root@yinzhengjie ~]# 23 [root@yinzhengjie ~]# ll /var/www/cgi-bin/yinzhengjie 24 -rwxr-xr-x. 1 root root 247 Oct 21 02:51 /var/www/cgi-bin/yinzhengjie 25 [root@yinzhengjie ~]# 26 [root@yinzhengjie ~]# /etc/init.d/iptables stop 27 iptables: Setting chains to policy ACCEPT: filter [ OK ] 28 iptables: Flushing firewall rules: [ OK ] 29 iptables: Unloading modules: [ OK ] 30 [root@yinzhengjie ~]# 31 [root@yinzhengjie ~]# service httpd reload 32 Reloading httpd: 33 [root@yinzhengjie ~]# 34 [root@yinzhengjie ~]# ip a | grep brd |grep inet | awk '{print $2}' | awk -F "/" '{print $1}'
35 192.168.1.115
36 [root@yinzhengjie ~]#
1 <Directory "/yinzhengjie/www/htdocs/caiwu">
2 Options None ------>表示全部屬性都不啓用 3 AllowOverride AuthConfig ------->表示基於用戶的認證,而再也不徹底基於IP的認證了 4 AuthType Basic ------>指定認真類型爲基本認證 5 AuthName "Please enter your username and password" ------>該參數給用戶一個提示的標題 6 AuthBasicProvider file -------->指定認真模式爲基於文件的,認證方式,其實該行能夠不寫,默認就是基於文件認真的,並且下面一行以及充分說明認證時基於文件的 7 AuthUserfile /etc/httpd/conf/.ApachePassword -------->指定存放用戶的配置文件 8 #AuthGroupFile /etc/httpd/conf/.ApacheGroup ------->指定存放組名的配置文件 9 Require user yinzhengjie -------->表示只容許yingzhengjie這個用戶訪問,若是你想要「 /etc/httpd/conf/.ApachePassword」這個配置文件的全部用戶都有能夠訪問,就能夠改爲「Require valid-user」。 10 #Require valid-user ----------->容許用戶配置文件的全部用戶均可以訪問 11 #Require group GroupName ------>容許訪問的組名 12 </Directory>
13 g>.建立用戶和密碼 14 [root@yinzhengjie ~]# htpasswd -c -m /etc/httpd/conf/.ApachePassword yinzhengjie #第一次要加「-c」選項,若是第二次建立用戶就不須要啦! 15 New password: 16 Re-type new password: 17 Adding password for user yinzhengjie 18 [root@yinzhengjie ~]# 19 [root@yinzhengjie ~]# htpasswd -m /etc/httpd/conf/.ApachePassword yzj #這是第二次建立用戶,若是加「-m」參數以後,就會將以前的配置給清空掉。 20 New password: 21 Re-type new password: 22 Adding password for user yzj 23 [root@yinzhengjie ~]# 24 [root@yinzhengjie ~]# more /etc/httpd/conf/.ApachePassword #查看咱們建立的用戶名和密碼 25 yinzhengjie:$apr1$t/P8rFq4$PAbZS1icMTxnCvIG8lAS3/
26 yzj:$apr1$WKxSWG2B$HuYha4pS6z7.SHyv9zNxv0 27 [root@yinzhengjie ~]# 28 [root@yinzhengjie ~]# ll /yinzhengjie/www/htdocs/caiwu/index.html 29 -rw-r--r--. 1 root root 6792 Oct 21 04:50 /yinzhengjie/www/htdocs/caiwu/index.html 30 [root@yinzhengjie ~]# 31 htpasswd命令:(更多關於htpasswd命令的使用能夠參考man幫助。) 32 -c:建立文件,建立第一個用戶時使用; 33 -m:「密碼基於MD5編碼存儲;
1 [root@yinzhengjie ~]# ifconfig eth0:0 192.168.1.116/24 #在web服務器中添加多個IP以便測試 2 [root@yinzhengjie ~]# ifconfig | grep addr|grep Bcast | awk '{print $2}'|awk -F ":" '{print $2}'
3 192.168.1.115
4 192.168.1.116
5 [root@yinzhengjie ~]# 6 [root@yinzhengjie ~]# more /etc/httpd/conf/httpd.conf | grep DocumentRoot | grep ^#DocumentRoot 7 #DocumentRoot "/yinzhengjie/www/htdocs/" ----->須要先關閉中心主機才能配置虛擬主機 8 [root@yinzhengjie ~]# 9 [root@yinzhengjie ~]# tail -11 /etc/httpd/conf/httpd.conf ------>配置虛擬主機格式以下 10 #ADD by yinzhengjie 11 <VirtualHost 192.168.1.115:80>
12 ServerName www.yinzhengjie.com 13 DocumentRoot "/yinzhengjie/www/yinzhengjie.com/htdocs"
14 </VirtualHost>
15
16 <VirtualHost 192.168.1.116:80> ------->你會發現虛擬主機的IP不一致; 17 ServerName yinzhengjie.org.cn 18 DocumentRoot "/yinzhengjie/www/yinzhengjie.org.cn/htdocs"
19 </VirtualHost>
20
21 [root@yinzhengjie ~]# 22 [root@yinzhengjie ~]# mkdir -p /yinzhengjie/www/yinzhengjie.com/htdocs 23 [root@yinzhengjie ~]# mkdir -p /yinzhengjie/www/yinzhengjie.org.cn/htdocs 24 [root@yinzhengjie ~]# more /yinzhengjie/www/yinzhengjie.org.cn/htdocs/index.html ---->設置主機的IP 25 <h1>www.yinzhengjie.org.cn</h1>
26 [root@yinzhengjie ~]# 27 [root@yinzhengjie ~]# 28 [root@yinzhengjie ~]# more /yinzhengjie/www/yinzhengjie.com/htdocs/index.html 29 <h1>www.yinzhengjie.com</h1>
30 [root@yinzhengjie ~]# 31 [root@yinzhengjie ~]# httpd -t ------>測試配置文件的語法格式是否正確 32 httpd: apr_sockaddr_info_get() failed for yinzhengjie 33 httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName
34 Syntax OK ------>諾,這是沒問題的,說是語法OK。 35 [root@yinzhengjie ~]# 36 [root@yinzhengjie ~]# service httpd reload ------->從新加載服務便可。 37 Reloading httpd: 38 [root@yinzhengjie ~]#
1 [root@yinzhengjie ~]# tail -17 /etc/httpd/conf/httpd.conf 2 #ADD by yinzhengjie 3 <VirtualHost 192.168.1.115:80>
4 ServerName www.yinzhengjie.com 5 DocumentRoot "/yinzhengjie/www/yinzhengjie.com/htdocs"
6 </VirtualHost>
7
8 <VirtualHost 192.168.1.115:8888> ----->咱們發現其和上面的虛擬主機不一樣之處在於端口,一次必定要監聽8080端口喲。 9 ServerName www.yinzhengjie.gov.cn 10 DocumentRoot "/yinzhengjie/www/yinzhengjie.gov.cn/htdocs"
11 </VirtualHost>
12
13
14 <VirtualHost 192.168.1.116:80>
15 ServerName www.yinzhengjie.org.cn 16 DocumentRoot "/yinzhengjie/www/yinzhengjie.org.cn/htdocs"
17 </VirtualHost>
18
19 [root@yinzhengjie ~]# 20 [root@yinzhengjie ~]# more /yinzhengjie/www/yinzhengjie.gov.cn/htdocs/index.html 21 <h1>www.yinzhengjie.gov.cn</h1>
22 [root@yinzhengjie ~]# 23 [root@yinzhengjie ~]# grep Listen /etc/httpd/conf/httpd.conf | grep -v ^# 24 Listen 80
25 Listen 8888 ------>注意,必定要啓用該端口,否則即便上面的虛擬主機配置正確也沒法訪問喲 26 [root@yinzhengjie ~]# 27 [root@yinzhengjie ~]# service httpd configtest ------->驗證語法格式是否正確 28 httpd: apr_sockaddr_info_get() failed for yinzhengjie 29 httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName
30 Syntax OK ------->很顯然,語法都是OK的。 31 [root@yinzhengjie ~]# 32 [root@yinzhengjie ~]# /etc/init.d/httpd restart ------>重啓服務便可 33 Stopping httpd: [ OK ] 34 Starting httpd: [ OK ] 35 [root@yinzhengjie ~]#
1 [root@yinzhengjie ~]# grep NameVirtualHost /etc/httpd/conf/httpd.conf | grep -v ^# 2 NameVirtualHost *:80 ------>在httpd2.2版本,想要實現FQDN方法,該功能須要開啓。httpd2.4則不須要開啓該功能啦! 3 [root@yinzhengjie ~]# 4 [root@yinzhengjie ~]# tail -17 /etc/httpd/conf/httpd.conf 5 #ADD by yinzhengjie 6 <VirtualHost *:80>
7 ServerName www.yinzhengjie.com 8 DocumentRoot "/yinzhengjie/www/yinzhengjie.com/htdocs"
9 </VirtualHost>
10
11 <VirtualHost *:80>
12 ServerName www.yinzhengjie.gov.cn 13 DocumentRoot "/yinzhengjie/www/yinzhengjie.gov.cn/htdocs"
14 </VirtualHost>
15
16
17 <VirtualHost *:80>
18 ServerName www.yinzhengjie.org.cn 19 DocumentRoot "/yinzhengjie/www/yinzhengjie.org.cn/htdocs"
20 </VirtualHost>
21
22 [root@yinzhengjie ~]# 23 [root@yinzhengjie ~]# more /etc/hosts | grep yinzhengjie ----->我打算在本地進行測試,所以須要修改配置文件 24 192.168.1.105 node1.yinzhengjie.com 25 192.168.1.110 node2.yinzhengjie.com 26 192.168.1.115 node3.yinzhengjie.com 27 192.168.1.200 node4.yinzhengjie.com 28 192.168.1.115 www.yinzhengjie.com 29 192.168.1.115 www.yinzhengjie.gov.cn 30 192.168.1.115 www.yinzhengjie.org.cn 31 [root@yinzhengjie ~]# 32 [root@yinzhengjie ~]# yum -y install elinks ----->安裝命令行工具 33 [root@yinzhengjie ~]# elinks -dump www.yinzhengjie.com -----如下是Linux測試結果 34 www.yinzhengjie.com 35 [root@yinzhengjie ~]# 36 [root@yinzhengjie ~]# 37 [root@yinzhengjie ~]# elinks -dump www.yinzhengjie.gov.cn 38 www.yinzhengjie.com 39 [root@yinzhengjie ~]# 40 [root@yinzhengjie ~]# elinks -dump www.yinzhengjie.org.cn 41 www.yinzhengjie.com 42 [root@yinzhengjie ~]#
1 [root@yinzhengjie ~]# tail -20 /etc/httpd/conf/httpd.conf 2 #ADD by yinzhengjie 3 <VirtualHost *:80> 4 ServerName www.yinzhengjie.com 5 DocumentRoot "/yinzhengjie/www/yinzhengjie.com/htdocs" 6 CustomLog "/var/log/httpd/www.yinzhengjie.com.log" combined 7 </VirtualHost> 8 9 <VirtualHost *:80> 10 ServerName www.yinzhengjie.gov.cn 11 DocumentRoot "/yinzhengjie/www/yinzhengjie.gov.cn/htdocs" 12 CustomLog "/var/log/httpd/www.yinzhengjie.gov.cn.log" combined 13 </VirtualHost> 14 15 16 <VirtualHost *:80> 17 ServerName www.yinzhengjie.org.cn 18 DocumentRoot "/yinzhengjie/www/yinzhengjie.org.cn/htdocs" 19 CustomLog "/var/log/httpd/www.yinzhengjie.org.cn.log" combined 20 </VirtualHost> 21 22 [root@yinzhengjie ~]# 23 [root@yinzhengjie ~]# httpd -t 24 Syntax OK 25 [root@yinzhengjie ~]# /etc/init.d/httpd restart 26 Stopping httpd: [ OK ] 27 Starting httpd: [ OK ] 28 [root@yinzhengjie ~]# 29 [root@yinzhengjie ~]# cd /var/log/httpd/ ----->客戶端訪問以後,再去日誌目錄下查看。 30 [root@yinzhengjie httpd]# ll 31 total 52 32 -rw-r--r--. 1 root root 13007 Oct 21 06:33 access_log 33 -rw-r--r--. 1 root root 19566 Oct 21 06:44 error_log 34 -rw-r--r--. 1 root root 194 Oct 21 06:44 www.yinzhengjie.com.log 35 -rw-r--r--. 1 root root 194 Oct 21 06:44 www.yinzhengjie.gov.cn.log 36 -rw-r--r--. 1 root root 194 Oct 21 06:44 www.yinzhengjie.org.cn.log 37 [root@yinzhengjie httpd]# 38 [root@yinzhengjie httpd]# cat www.yinzhengjie.com.log 39 192.168.1.161 - - [21/Oct/2017:06:44:35 -0700] "GET / HTTP/1.1" 304 - "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36" 40 [root@yinzhengjie httpd]# 41 [root@yinzhengjie httpd]# cat www.yinzhengjie.gov.cn.log 42 192.168.1.161 - - [21/Oct/2017:06:44:32 -0700] "GET / HTTP/1.1" 304 - "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36" 43 [root@yinzhengjie httpd]# 44 [root@yinzhengjie httpd]# cat www.yinzhengjie.org.cn.log 45 192.168.1.161 - - [21/Oct/2017:06:44:31 -0700] "GET / HTTP/1.1" 304 - "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36" 46 [root@yinzhengjie httpd]# 47 [root@yinzhengjie httpd]#