Httpd服务入门知识-Httpd服务常见配置案例之虚拟主机

          Httpd服务入门知识-Httpd服务常见配置案例之虚拟主机

                                                作者:尹正杰

版权声明:原创作品,谢绝转载!否则将追究法律责任。

 

 

 

一.虚拟主机实现方案

1>.Apache httpd 有三种实现虚拟主机的方案

  基于ip:
    为每个虚拟主机准备至少一个ip地址   基于port:
    为每个虚拟主机使用至少一个独立的port   基于FQDN:
    为每个虚拟主机使用至少一个FQDN

2>.创建测试网页文件

[[email protected] ~]# mkdir /var/www/html/{a,b,c}site
[[email protected] ~]# 
[[email protected] ~]# echo "

www.a.com

" > /var/www/html/asite/index.html [[email protected] ~]# [[email protected] ~]# echo "

www.b.org

" > /var/www/html/bsite/index.html [[email protected] ~]# [[email protected] ~]# echo "

www.c.net

" > /var/www/html/csite/index.html [[email protected] ~]# [[email protected] ~]# cat /var/www/html/asite/index.html

www.a.com

[[email protected] ~]# [[email protected] ~]# cat /var/www/html/bsite/index.html

www.b.org

[[email protected] ~]# [[email protected] ~]# cat /var/www/html/csite/index.html

www.c.net

[[email protected] ~]# [[email protected] ~]#

 

二.基于不同的IP地址实现虚拟主机配置实战案例

1>.给一块网卡临时配置多个IP地址测试使用

[[email protected] ~]# ifconfig 
eth0: flags=4163  mtu 1500
        inet 10.0.2.15  netmask 255.255.255.0  broadcast 10.0.2.255
        ether 08:00:27:e0:bb:66  txqueuelen 1000  (Ethernet)
        RX packets 11  bytes 2107 (2.0 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 14  bytes 1563 (1.5 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth1: flags=4163  mtu 1500
        inet 172.30.1.101  netmask 255.255.255.0  broadcast 172.30.1.255
        ether 08:00:27:c1:c7:46  txqueuelen 1000  (Ethernet)
        RX packets 20385  bytes 1745732 (1.6 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 13262  bytes 2189091 (2.0 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 82  bytes 15620 (15.2 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 82  bytes 15620 (15.2 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[[email protected] ~]# 
[[email protected] ~]# ip addr a 172.30.1.200 dev eth1
[[email protected] ~]# 
[[email protected] ~]# ip addr a 172.30.1.100 dev eth1
[[email protected] ~]# 
[[email protected] ~]# ip a
1: lo:  mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
2: eth0:  mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 08:00:27:e0:bb:66 brd ff:ff:ff:ff:ff:ff
    inet 10.0.2.15/24 brd 10.0.2.255 scope global noprefixroute dynamic eth0
       valid_lft 79421sec preferred_lft 79421sec
3: eth1:  mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 08:00:27:c1:c7:46 brd ff:ff:ff:ff:ff:ff
    inet 172.30.1.101/24 brd 172.30.1.255 scope global noprefixroute eth1
       valid_lft forever preferred_lft forever
    inet 172.30.1.200/32 scope global eth1
       valid_lft forever preferred_lft forever
    inet 172.30.1.100/32 scope global eth1
       valid_lft forever preferred_lft forever
[[email protected] ~]#  

Httpd服务入门知识-Httpd服务常见配置案例之虚拟主机_第1张图片

2>.编辑httpd的配置文件

[[email protected] ~]# egrep -v "^ *#|^$" /etc/httpd/conf/httpd.conf  
ServerRoot "/etc/httpd"
Listen 80
Include conf.modules.d/*.conf
User apache
Group apache
ServerAdmin root@localhost

    AllowOverride none
    Require all denied

DocumentRoot "/var/www/html"

    AllowOverride None
    Require all granted


    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted


    DirectoryIndex index.html


    Require all denied

ErrorLog "logs/error_log"
LogLevel warn

    LogFormat "%h %l %u %{%Y-%m-%d %H:%M:%S}t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" testlog
    LogFormat "%h %l %u %t \"%r\" %>s %b" common
    
      LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
    
    CustomLog "logs/access_log" testlog


    ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"


    AllowOverride None
    Options None
    Require all granted


    TypesConfig /etc/mime.types
    AddType application/x-compress .Z
    AddType application/x-gzip .gz .tgz
    AddType text/html .shtml
    AddOutputFilter INCLUDES .shtml

AddDefaultCharset UTF-8

    MIMEMagicFile conf/magic

EnableSendfile on
IncludeOptional conf.d/*.conf
[[email protected] ~]# 
[[email protected] ~]# 
[[email protected] ~]# egrep -v "^ *#|^$" /etc/httpd/conf/httpd.conf      #查看主配置文件内容
[[email protected] ~]# egrep -v "^ *#|^$" /etc/httpd/conf/httpd.conf  | grep ServerRoot
ServerRoot "/etc/httpd"
[[email protected] ~]# 
[[email protected] ~]# egrep -v "^ *#|^$" /etc/httpd/conf/httpd.conf  | grep IncludeOptional
IncludeOptional conf.d/*.conf
[[email protected] ~]# 
[[email protected] ~]# cat /etc/httpd/conf.d/virtualHost.conf 

    DocumentRoot "/var/www/html/asite"
    
        Require all granted
    
    CustomLog "/var/log/httpd/access_asite_log" testlog        #注意这个testlog变量需要在主配置文件中定义哟~




    DocumentRoot "/var/www/html/bsite"
    
        Require all granted
    
    CustomLog "/var/log/httpd/access_bsite_log" testlog




    DocumentRoot "/var/www/html/csite"
    
        Require all granted
    
    CustomLog "/var/log/httpd/access_csite_log" testlog

[[email protected] ~]# 
[[email protected] ~]# httpd -t
Syntax OK
[[email protected] ~]# 
[[email protected] ~]# systemctl reload httpd
[[email protected] ~]# 
[[email protected] ~]# ss -ntl
State       Recv-Q Send-Q             Local Address:Port                            Peer Address:Port              
LISTEN      0      128                            *:80                                         *:*                  
LISTEN      0      128                            *:22                                         *:*                  
LISTEN      0      128                           :::22                                        :::*                  
[[email protected] ~]# 
[[email protected] ~]# 

3>.验证客户端是否可以正常访问

[[email protected] ~]# tail /var/log/httpd/access_asite_log 
172.30.1.100 - - 2019-12-09 16:00:26 "GET / HTTP/1.1" 200 19 "-" "curl/7.29.0"
172.30.1.254 - - 2019-12-09 16:01:24 "GET / HTTP/1.1" 200 19 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebK
it/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36"172.30.1.254 - - 2019-12-09 16:01:24 "GET /favicon.ico HTTP/1.1" 404 209 "http://172.30.1.100/" "Mozilla/5.0 (Windows
 NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36"[[email protected] ~]# 
[[email protected] ~]# tail /var/log/httpd/access_asite_log
[[email protected] ~]# tail /var/log/httpd/access_bsite_log 
172.30.1.101 - - 2019-12-09 16:00:29 "GET / HTTP/1.1" 200 19 "-" "curl/7.29.0"
172.30.1.254 - - 2019-12-09 16:01:32 "GET / HTTP/1.1" 200 19 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebK
it/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36"172.30.1.254 - - 2019-12-09 16:02:25 "-" 408 - "-" "-"
[[email protected] ~]# 
[[email protected] ~]# tail /var/log/httpd/access_bsite_log
[[email protected] ~]# tail /var/log/httpd/access_csite_log 
172.30.1.200 - - 2019-12-09 16:00:32 "GET / HTTP/1.1" 200 19 "-" "curl/7.29.0"
172.30.1.254 - - 2019-12-09 16:01:39 "GET / HTTP/1.1" 200 19 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebK
it/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36"172.30.1.254 - - 2019-12-09 16:01:39 "GET /favicon.ico HTTP/1.1" 404 209 "http://172.30.1.200/" "Mozilla/5.0 (Windows
 NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36"172.30.1.254 - - 2019-12-09 16:02:31 "-" 408 - "-" "-"
[[email protected] ~]# 
[[email protected] ~]# tail /var/log/httpd/access_csite_log

Httpd服务入门知识-Httpd服务常见配置案例之虚拟主机_第2张图片

 

三.基于相同IP地址的不同端口实现虚拟主机配置实战案例

1>.编辑httpd的配置文件

[[email protected] ~]# egrep -v "^ *#|^$" /etc/httpd/conf/httpd.conf  
ServerRoot "/etc/httpd"
Listen 80
Include conf.modules.d/*.conf
User apache
Group apache
ServerAdmin root@localhost

    AllowOverride none
    Require all denied

DocumentRoot "/var/www/html"

    AllowOverride None
    Require all granted


    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted


    DirectoryIndex index.html


    Require all denied

ErrorLog "logs/error_log"
LogLevel warn

    LogFormat "%h %l %u %{%Y-%m-%d %H:%M:%S}t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" testlog
    LogFormat "%h %l %u %t \"%r\" %>s %b" common
    
      LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
    
    CustomLog "logs/access_log" testlog


    ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"


    AllowOverride None
    Options None
    Require all granted


    TypesConfig /etc/mime.types
    AddType application/x-compress .Z
    AddType application/x-gzip .gz .tgz
    AddType text/html .shtml
    AddOutputFilter INCLUDES .shtml

AddDefaultCharset UTF-8

    MIMEMagicFile conf/magic

EnableSendfile on
IncludeOptional conf.d/*.conf
[[email protected] ~]# 
[[email protected] ~]# 
[[email protected] ~]# egrep -v "^ *#|^$" /etc/httpd/conf/httpd.conf      #查看主配置文件内容
[[email protected] ~]# egrep -v "^ *#|^$" /etc/httpd/conf/httpd.conf  | grep ServerRoot
.ServerRoot "/etc/httpd"
[[email protected] ~]# 
[[email protected] ~]# egrep -v "^ *#|^$" /etc/httpd/conf/httpd.conf  | grep IncludeOptional
IncludeOptional conf.d/*.conf
[[email protected] ~]# 
[[email protected] ~]# cat /etc/httpd/conf.d/virtualHost.conf 
Listen 8080          #注意,千万别忘记监听不同的端口哟~
Listen 8081
Listen 8082


    DocumentRoot "/var/www/html/asite"
    
        Require all granted
    
    CustomLog "/var/log/httpd/access_asite_log" testlog




    DocumentRoot "/var/www/html/bsite"
    
        Require all granted
    
    CustomLog "/var/log/httpd/access_bsite_log" testlog




    DocumentRoot "/var/www/html/csite"
    
        Require all granted
    
    CustomLog "/var/log/httpd/access_csite_log" testlog

[[email protected] ~]#
[[email protected] ~]# httpd -t
Syntax OK
[[email protected] ~]# 
[[email protected] ~]# systemctl reload httpd
[[email protected] ~]#
[[email protected] ~]# ss -ntl
State       Recv-Q Send-Q             Local Address:Port                            Peer Address:Port              
LISTEN      0      128                            *:8080                                       *:*                  
LISTEN      0      128                            *:80                                         *:*                  
LISTEN      0      128                            *:8081                                       *:*                  
LISTEN      0      128                            *:8082                                       *:*                  
LISTEN      0      128                            *:22                                         *:*                  
LISTEN      0      128                           :::22                                        :::*                  
[[email protected] ~]# 
[[email protected] ~]# 

2>.验证客户端是否可以正常访问

Httpd服务入门知识-Httpd服务常见配置案例之虚拟主机_第3张图片

 

四.基于FQDN实现虚拟主机配置实战案例

1>.配置hosts文件解析

[[email protected] ~]# hostname
node101.yinzhengjie.org.cn
[[email protected] ~]# 
[[email protected] ~]# hostname -i
172.30.1.101
[[email protected] ~]# 
[[email protected] ~]# grep 172.30.1.101 /etc/hosts          #添加咱们要配置的虚拟主机FQDN解析,我这里是实验环境添加本地解析即可,生产环境需要添加DNS解析哟~
172.30.1.101 node101.yinzhengjie.org.cn www.a.com www.b.org www.c.net
[[email protected] ~]#
[[email protected] ~]# ping www.a.com
PING node101.yinzhengjie.org.cn (172.30.1.101) 56(84) bytes of data.
64 bytes from node101.yinzhengjie.org.cn (172.30.1.101): icmp_seq=1 ttl=64 time=0.011 ms
^C
--- node101.yinzhengjie.org.cn ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.011/0.011/0.011/0.000 ms
[[email protected] ~]# 
[[email protected] ~]# ping www.a.com
[[email protected] ~]# ping www.b.org
PING node101.yinzhengjie.org.cn (172.30.1.101) 56(84) bytes of data.
64 bytes from node101.yinzhengjie.org.cn (172.30.1.101): icmp_seq=1 ttl=64 time=0.010 ms
^C
--- node101.yinzhengjie.org.cn ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.010/0.010/0.010/0.000 ms
[[email protected] ~]# 
[[email protected] ~]# ping www.b.org
[[email protected] ~]# ping www.c.net
PING node101.yinzhengjie.org.cn (172.30.1.101) 56(84) bytes of data.
64 bytes from node101.yinzhengjie.org.cn (172.30.1.101): icmp_seq=1 ttl=64 time=0.015 ms
^C
--- node101.yinzhengjie.org.cn ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.015/0.015/0.015/0.000 ms
[[email protected] ~]#
[[email protected] ~]# ping www.c.net

2>.修改httpd的配置文件

[[email protected] ~]# egrep -v "^ *#|^$" /etc/httpd/conf/httpd.conf  
ServerRoot "/etc/httpd"
Listen 80
Include conf.modules.d/*.conf
User apache
Group apache
ServerAdmin root@localhost

    AllowOverride none
    Require all denied

DocumentRoot "/var/www/html"

    AllowOverride None
    Require all granted


    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted


    DirectoryIndex index.html


    Require all denied

ErrorLog "logs/error_log"
LogLevel warn

    LogFormat "%h %l %u %{%Y-%m-%d %H:%M:%S}t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" testlog
    LogFormat "%h %l %u %t \"%r\" %>s %b" common
    
      LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
    
    CustomLog "logs/access_log" testlog


    ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"


    AllowOverride None
    Options None
    Require all granted


    TypesConfig /etc/mime.types
    AddType application/x-compress .Z
    AddType application/x-gzip .gz .tgz
    AddType text/html .shtml
    AddOutputFilter INCLUDES .shtml

AddDefaultCharset UTF-8

    MIMEMagicFile conf/magic

EnableSendfile on
IncludeOptional conf.d/*.conf
[[email protected] ~]# 
[[email protected] ~]# 
[[email protected] ~]# egrep -v "^ *#|^$" /etc/httpd/conf/httpd.conf      #查看主配置文件内容
[[email protected] ~]# egrep -v "^ *#|^$" /etc/httpd/conf/httpd.conf  | grep ServerRoot
ServerRoot "/etc/httpd"
[[email protected] ~]# 
[[email protected] ~]# egrep -v "^ *#|^$" /etc/httpd/conf/httpd.conf  | grep IncludeOptional
IncludeOptional conf.d/*.conf
[[email protected] ~]# 
[[email protected] ~]# cat /etc/httpd/conf.d/virtualHost.conf 

    DocumentRoot "/var/www/html/asite"
    ServerName "www.a.com"        #别忘记在这里写上相应的虚拟主机的名称哟~以下配置类似修改即可。
    
        Require all granted
    
    CustomLog "/var/log/httpd/access_asite_log" testlog




    DocumentRoot "/var/www/html/bsite"
    ServerName "www.b.org"
    
        Require all granted
    
    CustomLog "/var/log/httpd/access_bsite_log" testlog




    DocumentRoot "/var/www/html/csite"
    ServerName "www.c.net"
    
        Require all granted
    
    CustomLog "/var/log/httpd/access_csite_log" testlog

[[email protected] ~]# 
[[email protected] ~]# httpd -t
Syntax OK
[[email protected] ~]# 
[[email protected] ~]# systemctl reload httpd
[[email protected] ~]# 
[[email protected] ~]# ss -ntl
State       Recv-Q Send-Q             Local Address:Port                            Peer Address:Port              
LISTEN      0      128                            *:80                                         *:*                  
LISTEN      0      128                            *:22                                         *:*                  
LISTEN      0      128                           :::22                                        :::*                  
[[email protected] ~]# 

3>.如下图所示,编辑windows系统的本地解析文件("C:\Windows\System32\drivers\etc\hosts")

Httpd服务入门知识-Httpd服务常见配置案例之虚拟主机_第4张图片

4>.验证客户端是否可以正常访问

Httpd服务入门知识-Httpd服务常见配置案例之虚拟主机_第5张图片

 

五.总结

  上面我们已经演示了基于IP,端口和FQDN来实现虚拟主机的实战案例,接下来我们分析一下这3种方案的区别。

    基于IP:
      需要使用多个不同的IP地址实现虚拟主机,无疑是会浪费多余的公网IP地址。
  
    基于端口:
      只需要一个公网IP地址即可,并将多个端口的绑定到同一个IP地址上,相对于基于IP实现的虚拟主机要更加节省IP地址,但会占多个端口号实,从而浪费多余的套接字文件。

    FQDN:
      基于FQDN只需要一个IP地址和一段端口即可,所有的虚拟主机只绑定在同一个端口即可,不同的虚拟主机根据客户端的请求报文中的"HOST"参数来判断是要访问哪个虚拟主机。如果客户端不指定"HOST"的属性,而是直接输入的是IP地址,则默认使用虚拟主机配置文件中的第一个虚拟主机来进行响应哟~
      基于FQDN实现虚拟主机要比基于IP实现虚拟主机更加节省公网IP地址。
      基于FQDN实现虚拟主机要比基于端口实现虚拟主机更加节省公网IP地址的端口数量。

  综上所述,我们生产环境中实现虚拟主机大多数运维人员都会选用基于FQDN的方案实现。

你可能感兴趣的:(Httpd服务入门知识-Httpd服务常见配置案例之虚拟主机)