一、概述
虚拟主机就是在一台服务器上部署多个站点,并且基于三种不同方式访问,这样可以充分利用服务器资源
访问方式:
1.基于不同IP相同端口
—缺点:浪费公网IP
2.基于相同IP不同端口
—缺点:客户不会在访问时加上端口号进行访问,访问复杂并且容易出错
3.基于相同IP相同端口不同域名(使用最广)
—优点:在域名层面上进行判断访问内容,并且不易出错,访问简单
二、实验部分
1.基于不同IP相同端口
1)在server端添加两块网卡
2)复制网络配置文件,并重命名为新网卡名称
[root@localhost network-scripts] cp ifcfg-ens33 ifcfg-ens34
3)获取UUID并输入到文件中
[root@localhost network-scripts] uuidgen ens37
'''
a611c514-89da-4823-9cac-7c77e3a6222b
[root@localhost network-scripts] vim ifcfg-ens37
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens37
UUID=a611c514-89da-4823-9cac-7c77e3a6222b
DEVICE=ens37
ONBOOT=yes
IPADDR=10.0.0.11
NETMASK=255.255.255.0
'''
4)重启网络服务
[root@localhost network-scripts] systemctl restart network
5)查看IP地址
[root@localhost network-scripts] ifconfig ens33; ifconfig ens37
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.0.0.1 netmask 255.255.255.0 broadcast 10.0.0.255
inet6 fe80::2f9:8a0f:55b5:4ec7 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:63:97:33 txqueuelen 1000 (Ethernet)
RX packets 58524 bytes 51670191 (49.2 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 34579 bytes 6456091 (6.1 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ens37: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.0.0.11 netmask 255.255.255.0 broadcast 10.0.0.255
inet6 fe80::596e:4181:483:a9eb prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:63:97:3d txqueuelen 1000 (Ethernet)
RX packets 19 bytes 3033 (2.9 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 118 bytes 18591 (18.1 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
6)打开httpd.conf文件的vhost选项(开启虚拟主机功能)
[root@localhost ~] vim /usr/local/httpd/conf/httpd.conf
......
# Virtual hosts
Include conf/extra/httpd-vhosts.conf
......
#去点该选项前面的#号,开启vhosts选项
7)修改httpd-vhost文件中的内容
[root@localhost ~] vim /usr/local/httpd/conf/extra/httpd-vhosts.conf
'''
<Directory "/myweb"> #定义网站的根目录
require all granted #允许所有人访问
</Directory>
<VirtualHost 10.0.0.1:80> #监听地址、端口
ServerAdmin [email protected] #服务器管理员的邮箱
DocumentRoot "/myweb/taobao" #网站根目录
ServerName dummy-host.example.com #网站域名
ServerAlias www.dummy-host.example.com #网站的别名
ErrorLog "logs/taobao.example.com-error_log" #出现报错的日志存放位置
CustomLog "logs/taobao.example.com-access_log" common #普通连接的日志存放位置
</VirtualHost>
<VirtualHost 10.0.0.11:80> #监听地址、端口
ServerAdmin [email protected] #服务器管理员的邮箱
DocumentRoot "/myweb/jingdong" #网站根目录
ServerName dummy-host2.example.com #网站域名
ErrorLog "logs/jingdong.example.com-error_log" #网站报错日志存放位置
CustomLog "logs/jingdong.example.com-access_log" common #普通连接的存放位置
</VirtualHost>
'''
8)创建网站的根目录并重启服务
[root@localhost ~] mkdir -p /myweb/jingdong /myweb/taobao
[root@localhost ~] cd /myweb/
[root@localhost myweb] ls
jingdong taobao
[root@localhost myweb] echo "<h1>www.jingdong.com</h1>" > /myweb/jingdong/index.html
[root@localhost myweb] cat /myweb/jingdong/index.html
<h1>www.jingdong.com</h1>
[root@localhost myweb] echo "<h1>www.taobao.com</h1>" > /myweb/taobao/index.html
[root@localhost myweb] cat /myweb/taobao/index.html
<h1>www.taobao.com</h1>
[root@localhost myweb] systemctl restart httpd
9)使用另一台同一网段的客户端wget访问两个网址
#使用wget下载网站的主页文件,如果下载成功,则是该网站可以访问
[root@localhost ~] wget http://10.0.0.1
'''
--2020-04-21 18:57:56-- http://10.0.0.1/
正在连接 10.0.0.1:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:24 [text/html]
正在保存至: “index.html”
100%[======================================================================>] 24 --.-K/s 用时 0s
2020-04-21 18:57:56 (8.81 MB/s) - 已保存 “index.html” [24/24])
'''
[root@localhost ~] wget http://10.0.0.11
'''
--2020-04-21 18:58:01-- http://10.0.0.11/
正在连接 10.0.0.11:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:26 [text/html]
正在保存至: “index.html.1”
100%[======================================================================>] 26 --.-K/s 用时 0s
2020-04-21 18:58:01 (9.39 MB/s) - 已保存 “index.html.1” [26/26])
'''
10)查看访问成功普通连接的日志
#我的客户端IP是10.0.0.2所以访问前面的客户端地址就是10.0.0.2
[root@localhost myweb] cat /usr/local/httpd/logs/jingdong.example.com-access_log
10.0.0.2 - - [21/Apr/2020:18:58:01 +0800] "GET / HTTP/1.1" 200 26
[root@localhost myweb] cat /usr/local/httpd/logs/taobao.example.com-access_log
10.0.0.2 - - [21/Apr/2020:18:57:56 +0800] "GET / HTTP/1.1" 200 24
2.相同IP不同端口号
1)修改vhosts文件,并重启服务httpd
#在vhosts.conf文件添加listen需要监听的端口
#并修改virtualhost监听的地址为同一ip和后面监听的端口号为不同端口号
[root@localhost myweb] vim /usr/local/httpd/conf/extra/httpd-vhosts.conf
'''
<Directory "/myweb">
require all granted
</Directory>
listen 8080
listen 1800
<VirtualHost 10.0.0.1:8080>
ServerAdmin [email protected]
DocumentRoot "/myweb/taobao"
ServerName dummy-host.example.com
ServerAlias www.dummy-host.example.com
ErrorLog "logs/toabao.example.com-error_log"
CustomLog "logs/toabao.example.com-access_log" common
</VirtualHost>
<VirtualHost 10.0.0.1:1800>
ServerAdmin [email protected]
DocumentRoot "/myweb/jingdong"
ServerName dummy-host2.example.com
ErrorLog "logs/jingdong.example.com-error_log"
CustomLog "logs/jingdong.example.com-access_log" common
</VirtualHost>
'''
[root@localhost myweb] systemctl restart httpd
2)使用netstat查看httpd服务的监听端口
[root@localhost myweb] netstat -anpt | grep httpd
tcp6 0 0 :::1800 :::* LISTEN 60903/httpd
tcp6 0 0 :::80 :::* LISTEN 60903/httpd
tcp6 0 0 :::8080 :::* LISTEN 60903/httpd
3)使用客户端访问测试
[root@localhost ~] wget http://10.0.0.1:8080
'''
--2020-04-21 19:09:21-- http://10.0.0.1:8080/
正在连接 10.0.0.1:8080... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:24 [text/html]
正在保存至: “index.html.2”
100%[======================================================================>] 24 --.-K/s 用时 0s
2020-04-21 19:09:21 (9.05 MB/s) - 已保存 “index.html.2” [24/24])
'''
[root@localhost ~] wget http://10.0.0.1:1800
'''
--2020-04-21 19:09:25-- http://10.0.0.1:1800/
正在连接 10.0.0.1:1800... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:26 [text/html]
正在保存至: “index.html.3”
100%[======================================================================>] 26 --.-K/s 用时 0s
2020-04-21 19:09:25 (9.72 MB/s) - 已保存 “index.html.3” [26/26])
'''
4)查看访问成功日志,同理,会添加一条新的记录
[root@localhost myweb]# cat /usr/local/httpd/logs/toabao.example.com-access_log
10.0.0.2 - - [21/Apr/2020:18:57:56 +0800] "GET / HTTP/1.1" 200 24
10.0.0.2 - - [21/Apr/2020:19:09:21 +0800] "GET / HTTP/1.1" 200 24
[root@localhost myweb]# cat /usr/local/httpd/logs/jingdong.example.com-access_log
10.0.0.2 - - [21/Apr/2020:18:58:01 +0800] "GET / HTTP/1.1" 200 26
10.0.0.2 - - [21/Apr/2020:19:09:25 +0800] "GET / HTTP/1.1" 200 26
3.基于相同IP相同端口不同域名(使用最多)
搭建DNS服务
1)搭建DNS(bind)服务
[root@localhost ~] yum -y install bind
[root@localhost ~] vim /etc/named.conf
#named.conf是DNS的主配置文件
#修改该文件的这两项内容即可
'''
......
listen-on port 53 { 10.0.0.1; }; #监听10.0.0.1地址和53端口
allow-query { any; }; #any表示允许所有人访问
......
'''
[root@localhost ~] vim /etc/named.rfc1912.zones
#该文件是区域指定文件
#在文件末尾添加这两项内容
'''
......
zone "taobao.com" IN { #taobao.com的域名
type master; #类型为主DNS
file "taobao.com.zone"; #指定区域文件
allow-update { none; }; #允许所有更新
};
zone "jingdong.com" IN { #jingdong.com的域名
type master; #类型为主DNS
file "jingdong.com.zone"; #指定区域文件
allow-update { none; }; #允许所有更新
};
'''
[root@localhost ~] cd /var/named/
#进入named区域文件目录
[root@localhost named] cp -p named.localhost taobao.com.zone
[root@localhost named] cp -p named.localhost jingdong.com.zone
#-p是保存文件原有权限进行拷贝,该区域文件要与上面的区域指定文件中指定的文件名对应
[root@localhost named] vim taobao.com.zone
'''
$TTL 1D
@ IN SOA www.taobao.com. rname.invalid. (
0 ; serial
1D ; refresh
1H ; retry
1W ; expire
3H ) ; minimum
NS www.taobao.com.
www A 10.0.0.1
'''
[root@localhost named] vim jingdong.com.zone
'''
$TTL 1D
@ IN SOA www.jingdong.com. rname.invalid. (
0 ; serial
1D ; refresh
1H ; retry
1W ; expire
3H ) ; minimum
NS www.jingdong.com.
www A 10.0.0.1
'''
[root@localhost named] systemctl restart named
#重启named服务,开启DNS
客户端配置:
[root@localhost ~] vim /etc/sysconfig/network-scripts/ifcfg-ens33
#修改客户机的网卡配置文件,添加DNS服务器的IP地址
'''
......
DNS1=10.0.0.1
'''
[root@localhost ~] systemctl restart network
#重启网络服务
配置httpd的配置文件
1)修改vhosts文件并重启
[root@localhost ~] vim /usr/local/httpd/conf/extra/httpd-vhosts.conf
'''
<Directory "/myweb">
require all granted
</Directory>
<VirtualHost 10.0.0.1:8080>
ServerAdmin [email protected]
DocumentRoot "/myweb/taobao"
ServerName www.taobao.com
ErrorLog "logs/toabao.example.com-error_log"
CustomLog "logs/toabao.example.com-access_log" common
</VirtualHost>
<VirtualHost 10.0.0.1:1800>
ServerAdmin [email protected]
DocumentRoot "/myweb/jingdong"
ServerName www.jingdong.com
ErrorLog "logs/jingdong.example.com-error_log"
CustomLog "logs/jingdong.example.com-access_log" common
</VirtualHost>
'''
[root@localhost ~] systemctl restart httpd
2)在客户端使用nslookup解析一下域名
[root@localhost ~] nslookup
'''
> www.jingdong.com
Server: 10.0.0.1
Address: 10.0.0.1#53
Name: www.jingdong.com
Address: 10.0.0.1
> www.taobao.com
Server: 10.0.0.1
Address: 10.0.0.1#53
Name: www.taobao.com
Address: 10.0.0.1
'''
3)再在客户端使用wget下载主页文件
[root@localhost ~] wget www.taobao.com
'''
--2020-04-21 19:35:15-- http://www.taobao.com/
正在解析主机 www.taobao.com (www.taobao.com)... 10.0.0.1
正在连接 www.taobao.com (www.taobao.com)|10.0.0.1|:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:45 [text/html]
正在保存至: “index.html”
100%[======================================================================>] 45 --.-K/s 用时 0s
2020-04-21 19:35:15 (14.6 MB/s) - 已保存 “index.html” [45/45])
'''
[root@localhost ~] wget www.jingdong.com
'''
--2020-04-21 19:35:18-- http://www.jingdong.com/
正在解析主机 www.jingdong.com (www.jingdong.com)... 10.0.0.1
正在连接 www.jingdong.com (www.jingdong.com)|10.0.0.1|:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:45 [text/html]
正在保存至: “index.html.1”
100%[======================================================================>] 45 --.-K/s 用时 0s
2020-04-21 19:35:18 (17.1 MB/s) - 已保存 “index.html.1” [45/45])
'''
#成功!!!!!!!!!!