hellopasswd
httpd的默认虚拟主机
- 一台服务器可以访问多个网站,每个网站都是一个虚拟机
- 概念:域名(主机名)、DNS、解析域名、hosts
- 任何一个域名解析到这台机器,都可以访问的虚拟主机就是默认虚拟主机
- vi /usr/local/apache2.4/conf/httpd.conf #搜索httpd/vhost,去掉#
- vi /usr/local/apache2.4/conf/extra/httpd-vhosts.conf #改为如下 <VirtuaHost *:80> ServerAdmin DocumentRoot "/data/wwwroot/abc.com" ServerName abc.com ServerAlias www.example.com Errorlog "log/abc.com-access_log" CustomLog "logs/abc.com-access_log" common </VirtualHost> <VirtuaHost *:80> DocumentRoot "/data/wwwroot/www.111.com" ServerName www.111.com </VirtualHost>
- /usr/local/apache2.4/bin/apachectl -t
- /usr/local/apache2.4/bin/apachectl graceful
网站能够直接访问,其实是DocumentRoot "/usr/local/apache2.4/htdocs"指定了一个文件
[root@localhost ~]# vi /usr/local/apache2.4/conf/httpd.conf\DocumentRoot 211 # below. 212 # 213 214 # 215 # DocumentRoot: The directory out of which you will serve your 216 # documents. By default, all requests are taken from this directory, but 217 # symbolic links and aliases may be used to point to other locations. 218 # 219 DocumentRoot "/usr/local/apache2.4/htdocs" 220221 # 222 # Possible values for the Options directive are "None", "All", 223 # or any combination of: 224 # Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews 225 # 226 # Note that "MultiViews" must be named *explicitly* --- "Options All"
而域名为
[root@localhost ~]# vi /usr/local/apache2.4/conf/httpd.conf/ServerName 188 # 189 # ServerName gives the name and port that the server uses to identify itself. 190 # This can often be determined automatically, but we recommend you specify 191 # it explicitly to prevent problems during startup. 192 # 193 # If your host doesn't have a registered DNS name, enter its IP address here. 194 # 195 ServerName www.example.com:80 196 197 # 198 # Deny access to the entirety of your server's filesystem. You must 199 # explicitly permit access to web content directories in other 200 #blocks below. 201 # 202 203 AllowOverride none
Windows下的hosts
C:\Users\Administrator>cd C:\Windows\System32\drivers\etcC:\Windows\System32\drivers\etc>start hosts# Copyright (c) 1993-2009 Microsoft Corp.## This is a sample HOSTS file used by Microsoft TCP/IP for Windows.## This file contains the mappings of IP addresses to host names. Each# entry should be kept on an individual line. The IP address should# be placed in the first column followed by the corresponding host name.# The IP address and the host name should be separated by at least one# space.## Additionally, comments (such as these) may be inserted on individual# lines or following the machine name denoted by a '#' symbol.## For example:## 102.54.94.97 rhino.acme.com # source server# 38.25.63.10 x.acme.com # x client host# localhost name resolution is handled within DNS itself.# 127.0.0.1 localhost# ::1 localhost127.0.0.1 localhost
在hosts中添加一行192.168.9.134 www.abc.com www.123.com
C:\Users\Administrator>ping www.abc.com正在 Ping www.abc.com [192.168.9.134] 具有 32 字节的数据:来自 192.168.9.134 的回复: 字节=32 时间<1ms TTL=64来自 192.168.9.134 的回复: 字节=32 时间<1ms TTL=64来自 192.168.9.134 的回复: 字节=32 时间<1ms TTL=64来自 192.168.9.134 的回复: 字节=32 时间<1ms TTL=64192.168.9.134 的 Ping 统计信息: 数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),往返行程的估计时间(以毫秒为单位): 最短 = 0ms,最长 = 0ms,平均 = 0msC:\Users\Administrator>ping www.123.com正在 Ping www.abc.com [192.168.9.134] 具有 32 字节的数据:来自 192.168.9.134 的回复: 字节=32 时间<1ms TTL=64来自 192.168.9.134 的回复: 字节=32 时间<1ms TTL=64来自 192.168.9.134 的回复: 字节=32 时间<1ms TTL=64来自 192.168.9.134 的回复: 字节=32 时间<1ms TTL=64192.168.9.134 的 Ping 统计信息: 数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),往返行程的估计时间(以毫秒为单位): 最短 = 0ms,最长 = 0ms,平均 = 0ms
浏览器访问www.abc.com或www.123.com出现It work!则表示成功
apache默认虚拟主机,当访问ServerName和DocumentRoot 默认虚拟主机
[root@localhost ~]# vi /usr/local/apache2.4/conf/httpd.conf/extra 470 #Include conf/extra/httpd-languages.conf 471 472 # User home directories 473 #Include conf/extra/httpd-userdir.conf 474 475 # Real-time info on requests and configuration 476 #Include conf/extra/httpd-info.conf 477 478 # Virtual hosts 479 #Include conf/extra/httpd-vhosts.conf 480 481 # Local access to the Apache HTTP Server Manual 482 #Include conf/extra/httpd-manual.conf 483 484 # Distributed authoring and versioning (WebDAV) 485 #Include conf/extra/httpd-dav.conf
将479行的注释去掉为Include conf/extra/httpd-vhosts.conf
打开二级配置文件,虚拟主机配置文件
[root@localhost ~]# vi /usr/local/apache2.4/conf/extra/httpd-vhosts.conf 2324 ServerAdmin webmaster@dummy-host.example.com 25 DocumentRoot "/usr/local/apache2.4/docs/dummy-host.example.com" 26 ServerName dummy-host.example.com 27 ServerAlias www.dummy-host.example.com 28 ErrorLog "logs/dummy-host.example.com-error_log" 29 CustomLog "logs/dummy-host.example.com-access_log" common 30 31 3233 ServerAdmin webmaster@dummy-host2.example.com 34 DocumentRoot "/usr/local/apache2.4/docs/dummy-host2.example.com" 35 ServerName dummy-host2.example.com 36 ErrorLog "logs/dummy-host2.example.com-error_log" 37 CustomLog "logs/dummy-host2.example.com-access_log" common 38
一个主机代表一个网站 ServerAdmin有无都可以 DocumentRoot代表网站根目录 ServerName代表域名 ServerAlias代表别名 ErrorLog错误日志 CustomLog访问日志
修改为以下,若修改后之前的www.example.com就会访问不了
2324 DocumentRoot "/data/wwwroot/abc.com" 25 ServerName abc.com 26 ServerAlias www.abc.com www.123.com 27 ErrorLog "logs/abc.com-error_log" 28 CustomLog "logs/abc.com-access_log" common 29 30 3132 DocumentRoot "/data/wwwroot/111.com" 33 ServerName 111.com 34 ServerAlias www.example.com 35 ErrorLog "logs/111.com-error_log" 36 CustomLog "logs/111.com-access_log" common 37
但可以添加一个别名为www.example.com,但他此时对应的目录为/data/wwwroot/111.com
在站点目录下创建
[root@localhost ~]# mkdir /data/wwwroot/[root@localhost ~]# mkdir /data/wwwroot/abc.com[root@localhost ~]# mkdir /data/wwwroot/111.com[root@localhost ~]# vi /data/wwwroot/abc.com/index.php [root@localhost ~]# vi /data/wwwroot/111.com/index.php [root@localhost ~]# /usr/local/apache2.4/bin/apachectl -tSyntax OK[root@localhost ~]# /usr/local/apache2.4/bin/apachectl graceful
[root@localhost ~]# ping www.abc.comPING abc.com (199.181.132.250) 56(84) bytes of data.^C64 bytes from 199.181.132.250: icmp_seq=1 ttl=128 time=248 ms--- abc.com ping statistics ---1 packets transmitted, 1 received, 0% packet loss, time 0msrtt min/avg/max/mdev = 248.780/248.780/248.780/0.000 ms
若没有绑定hosts,则会访问到外网,此时可以使用curl -x
[root@localhost ~]# curl -x 192.168.9.134:80 abc.comhello!abc.com[root@localhost ~]# curl -x 192.168.9.134:80 www.abc.comhello!abc.com[root@localhost ~]# curl -x 192.168.9.134:80 www.abcde.comhello!abc.com
无论任何域名都指向abc.com,而abc.com是虚拟主机配置的文件中的虚拟主机,而且还是默认虚拟主机
[root@localhost ~]# curl -x 192.168.9.134:80 www.example.comhello!111.comcurl -x 192.168.9.134:80 www.111.comhello!abc.com[root@localhost ~]# curl -x 192.168.9.134:80 111.comhello!111.com
www.example.com指向111.com,而www.111.com没有指定别名www.111.com,则会指向www.abc.com
虚拟主机配置文件生效,则可以定义多个虚拟主机
[root@localhost ~]# vi /usr/local/apache2.4/conf/httpd.conf 471 472 # User home directories 473 #Include conf/extra/httpd-userdir.conf 474 475 # Real-time info on requests and configuration 476 #Include conf/extra/httpd-info.conf 477 478 # Virtual hosts 479 Include conf/extra/httpd-vhosts.conf 480 481 # Local access to the Apache HTTP Server Manual 482 #Include conf/extra/httpd-manual.conf 483 484 # Distributed authoring and versioning (WebDAV) 485 #Include conf/extra/httpd-dav.conf 486
而最为特殊的是默认虚拟主机,任何域名解析到这个主机上则会自动解析虚拟主机
修改与 171220