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]#