虛拟Web主机
- 在同一台物理服务器中运行多个Web站点,其中每一个站点并不独立占用一台真正的计算机
httpd支持的虚拟主机类型
- 基于域名的虚拟主机
- 基于IP地址的虚拟主机
- 基于端口的虚拟主机
搭建实验
基于域名的虚拟主机
[[email protected] ~]# yum install bind httpd -y //在服务器上安装DNS与HTTP服务
[[email protected] ~]# cd /etc/ //进入etc目录
[[email protected] etc]# vim named.conf //进入编辑DNS服务主配置文件
...//省略部分内容...
options {
listen-on port 53 { any; }; //将监听IP地址更改为any,监听所有地址
listen-on-v6 port 53 { ::1; };
directory "/var/named";
dump-file "/var/named/data/cache_dump.db";
statistics-file "/var/named/data/named_stats.txt";
memstatistics-file "/var/named/data/named_mem_stats.txt";
recursing-file "/var/named/data/named.recursing";
secroots-file "/var/named/data/named.secroots";
allow-query { any; }; //主机名更改为any,允许所有主机通过解析
...//省略部分内容...
:wq //保存退出
[[email protected] etc]# vim named.rfc1912.zones //进入编辑区域配置文件
...//省略部分内容...
zone "kgc.com" IN { //更改域名
type master;
file "kgc.com.zone"; //更改数据文件名称
allow-update { none; };
};
zone "aaa.com" IN { //更改域名
type master;
file "aaa.com.zone"; //更改数据文件名称
allow-update { none; };
};
...//省略部分内容...
[[email protected] etc]# cd /var/named/ //进入区域数据文件存放目录
[[email protected] named]# cp -p named.localhost kgc.com.zone //复制区域数据文件模板
[[email protected] named]# vim kgc.com.zone //进入编辑模板
$TTL 1D
@ IN SOA @ rname.invalid. (
0 ; serial
1D ; refresh
1H ; retry
1W ; expire
3H ) ; minimum
NS @
A 127.0.0.1
www IN A 192.168.144.133 //删掉最后一行,更改为此行
:wq //保存退出
[[email protected] named]# cp -p kgc.com.zone aaa.com.zone //复制刚更改的数据文件,命名为aaa.com.zone,不需要更改内容
[[email protected] named]# systemctl start named //启动DNS服务
[[email protected] named]# systemctl stop firewalld.service //关闭防火墙
[[email protected] named]# setenforce 0 //关闭增强性安全功能
[[email protected] html]# cd /etc/httpd/conf/ //进入http服务配置文件目录
[[email protected] conf]# mkdir extra //创建文件夹
[[email protected] conf]# ls //查看目录
extra httpd.conf magic
[[email protected] extra]# vim vhost.conf //编辑子配置文件
<VirtualHost *:80>
DocumentRoot "/var/www/html/aaa/"
ServerName www.aaa.com
ErrorLog "logs/www.aaa.com.error_log"
CustomLog "logs/www.aaa.com.access_log" common
<Directory "/var/www/html">
Require all granted //编辑虚拟主机配置条目
</Directory>
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/var/www/html/kgc/"
ServerName www.kgc.com
ErrorLog "logs/www.kgc.com.error_log"
CustomLog "logs/www.kgc.com.access_log" common
<Directory "/var/www/html">
Require all granted
</Directory>
</VirtualHost>
~
:wq //保存退出
[[email protected] extra]# cd /var/www/html/ //进入http服务网页站点
[[email protected] html]# mkdir aaa kgc //创建目录文件
[[email protected] html]# cd aaa/ //进入目录
[[email protected] aaa]# vim index.html //编辑默认主页
<h1>this is aaa web</h1> //编写内容
~
:wq //保存退出
[[email protected] aaa]# ls //查看目录
index.html
[[email protected] aaa]# cd ../kgc/ //返回上一层并进入kgc目录
[[email protected] kgc]# vim index.html //编辑默认网页
<h1>this is kgc web</h1> //编辑内容
~
:wq //保存退出
[[email protected] kgc]# ls //查看目录
index.html
[[email protected] kgc]# vim /etc/httpd/conf/httpd.conf //进入编辑http服务主配置文件
...//省略部分内容...
# prevent Apache from glomming onto all bound IP addresses.
#
Listen 192.168.144.137:80 //打开ipv4监听地址,并更改为本机IP地址
#Listen 80 //关闭ipv6的监听地址
...//省略部分内容...
# Load config files in the "/etc/httpd/conf.d" directory, if any.
IncludeOptional conf.d/*.conf
Include conf/extra/vhost.conf //在最后一行添加包含子配置文件目录条目
:wq //保存退出
[[email protected] kgc]# systemctl start httpd //开启http服务
[[email protected] kgc]# netstat -ntap | grep 80 //查看80端口是否开启
tcp6 0 0 :::80 :::* LISTEN 2450/httpd
打开一台win10客户机,更改DNS服务器地址,打开网页,测试基于不同域名构建虚拟主机是否成功
基于端口建立虚拟主机
此处实验就直接在上面的实验中更改配置,不重新操作
[[email protected] named]# vim /etc/httpd/conf/extra/vhost.conf
...//省略部分内容...
<VirtualHost *:80>
DocumentRoot "/var/www/html/kgc/"
ServerName www.kgc.com
ErrorLog "logs/www.kgc.com.error_log"
CustomLog "logs/www.kgc.com.access_log" common
<Directory "/var/www/html">
Require all granted
</Directory>
</VirtualHost>
<VirtualHost *:8080>
DocumentRoot "/var/www/html/kgc02/"
ServerName www.kgc.com
ErrorLog "logs/www.kgc02.com.error_log"
CustomLog "logs/www.kgc02.com.access_log" common
<Directory "/var/www/html"> //复制上面的配置文件条目,并更改监听端口为8080,并将站点目录、日志文件名更改为kgc02
Require all granted
</Directory>
</VirtualHost>
:wq //保存退出
[[email protected] named]# cd /var/www/html/
[[email protected] html]# mkdir kgc02
[[email protected] html]# cd kgc02/
[[email protected] kgc02]# vim index.html
<h1>this is kgc02 web</h1>
~
:wq
[[email protected] kgc02]# vim /etc/httpd/conf/httpd.conf
...//省略部分内容...
# prevent Apache from glomming onto all bound IP addresses.
#
Listen 192.168.144.137:80
Listen 192.168.144.137:8080
#Listen 80
...//省略部分内容...
:wq
[[email protected] kgc02]# systemctl restart httpd
在win10客户端验证基于端口的虚拟主机配置
基于IP地址建立虚拟主机
在这里先给Linux服务器虚拟机添加一张网卡,获取另一个IP地址
在虚拟机中获取IP地址
[[email protected] ~]# ifconfig //查看网卡信息
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.144.137 netmask 255.255.255.0 broadcast 192.168.144.255
inet6 fe80::a85a:c203:e2e:3f3c prefixlen 64 scopeid 0x20<link>
inet6 fe80::ad78:663f:1f02:22e4 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:72:65:cb txqueuelen 1000 (Ethernet)
RX packets 14117 bytes 10290025 (9.8 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 6337 bytes 767788 (749.7 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ens36: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.144.143 netmask 255.255.255.0 broadcast 192.168.144.255 //成功获取IP地址
inet6 fe80::d65e:47b1:916d:de6c prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:72:65:d5 txqueuelen 1000 (Ethernet)
RX packets 115 bytes 20495 (20.0 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 79 bytes 17837 (17.4 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
...//省略部分内容...
[[email protected] ~]# vim /etc/httpd/conf/extra/vhost.conf //进入编辑http服务子配置文件
<VirtualHost 192.168.144.137:80> //更改为固定IP地址
DocumentRoot "/var/www/html/aaa/"
ServerName www.aaa.com
ErrorLog "logs/www.aaa.com.error_log"
CustomLog "logs/www.aaa.com.access_log" common
<Directory "/var/www/html">
Require all granted
</Directory>
</VirtualHost>
<VirtualHost 192.168.144.143:80> //复制上面配置条,更改IP地址
DocumentRoot "/var/www/html/aaa02/" //更改站点文件
ServerName www.naaa.com //更改域名
ErrorLog "logs/www.aaa02.com.error_log" //更改错误日志文件名
CustomLog "logs/www.aaa02.com.access_log" common //更改日志文件名
<Directory "/var/www/html">
Require all granted
</Directory>
</VirtualHost>
:wq //保存退出
[[email protected] ~]# vim /etc/named.rfc1912.zones //编辑DNS区域配置文件
...//省略部分内容...
zone "aaa.com" IN {
type master;
file "aaa.com.zone";
allow-update { none; };
};
zone "naaa.com" IN {
type master; //添加新的区域配置文件
file "naaa.com.zone";
allow-update { none; };
};
...//省略部分内容...
:wq //保存退出
[[email protected] ~]# cd /var/named/ //进入区域数据文件存放目录
[[email protected] named]# cp -p aaa.com.zone naaa.com.zone //复制区域数据文件
[[email protected] named]# vim naaa.com.zone //进入编辑新的区域数据文件
$TTL 1D
@ IN SOA @ rname.invalid. (
0 ; serial
1D ; refresh
1H ; retry
1W ; expire
3H ) ; minimum
NS @
A 127.0.0.1
www IN A 192.168.144.143 //更改IP地址
~
:wq //保存退出
[[email protected] named]# cd /var/www/html/ //进入http服务站点
[[email protected] html]# mkdir aaa02 //创建新的站点目录
[[email protected] html]# cd aaa02/ //进入目录
[[email protected] aaa02]# vim index.html //编辑默认主页文件
<h1>this is 143 aaa02 web</h1> //编辑网页内容
:wq //保存退出
[[email protected] aaa02]# vim /etc/httpd/conf/httpd.conf //编辑http服务主配置文件
...//省略部分内容...
# prevent Apache from glomming onto all bound IP addresses.
#
Listen 192.168.144.137:80
Listen 192.168.144.143:80 //编辑监听新的IP地址
#Listen 80
...//省略部分内容...
:wq //保存退出
[[email protected] aaa02]# systemctl restart httpd //重启http服务
[[email protected] aaa02]# systemctl restart named //重启DNS服务
在win10客户机中验证基于IP地址建立的虚拟主机
配置成功
原文地址:https://blog.51cto.com/14473285/2446637
时间: 2024-11-08 06:19:15