【CetOS 7LAMP架构6】,Apache默认虚拟主机#

shallow丿ovephp


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 root@abc.com 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"指定了一个文件web

[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"
    220 <Directory "/usr/local/apache2.4/htdocs">
    221     #
    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"

而域名为apache

[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 # <Directory> blocks below.
    201 #
    202 <Directory />
    203     AllowOverride none

Windows下的hosts浏览器

C:\Users\Administrator>cd C:\Windows\System32\drivers\etc

C:\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             localhost

127.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=64

192.168.9.134 的 Ping 统计信息:
    数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),
往返行程的估计时间(以毫秒为单位):
    最短 = 0ms,最长 = 0ms,平均 = 0ms

C:\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=64

192.168.9.134 的 Ping 统计信息:
    数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),
往返行程的估计时间(以毫秒为单位):
    最短 = 0ms,最长 = 0ms,平均 = 0ms

浏览器访问www.abc.com或www.123.com出现It work!则表示成功app

apache默认虚拟主机,当访问ServerName和DocumentRoot 默认虚拟主机curl

[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.confide

打开二级配置文件,虚拟主机配置文件网站

[root@localhost ~]# vi /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
     23 <VirtualHost *:80>
     24     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 </VirtualHost>
     31 
     32 <VirtualHost *:80>
     33     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 </VirtualHost>

一个主机表明一个网站 ServerAdmin有无均可以 DocumentRoot表明网站根目录 ServerName表明域名 ServerAlias表明别名 ErrorLog错误日志 CustomLog访问日志this

修改成如下,若修改后以前的www.example.com就会访问不了

23 <VirtualHost *:80>
     24     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 </VirtualHost>
     30 
     31 <VirtualHost *:80>
     32     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 </VirtualHost>

但能够添加一个别名为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
	<?php
		echo "hello!abc.com";
	?>
[root@localhost ~]# vi /data/wwwroot/111.com/index.php
	<?php
		echo "hello!111.com";
	?>
[root@localhost ~]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[root@localhost ~]# /usr/local/apache2.4/bin/apachectl graceful
[root@localhost ~]# ping www.abc.com
PING 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 0ms
rtt 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.com
hello!abc.com
[root@localhost ~]# curl -x 192.168.9.134:80 www.abc.com
hello!abc.com
[root@localhost ~]# curl -x 192.168.9.134:80 www.abcde.com
hello!abc.com

不管任何域名都指向abc.com,而abc.com是虚拟主机配置的文件中的虚拟主机,并且仍是默认虚拟主机

[root@localhost ~]# curl -x 192.168.9.134:80 www.example.com
hello!111.com
curl -x 192.168.9.134:80 www.111.com
hello!abc.com
[root@localhost ~]# curl -x 192.168.9.134:80 111.com
hello!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

而最为特殊的是默认虚拟主机,任何域名解析到这个主机上则会自动解析虚拟主机

相关文章
相关标签/搜索