在同一台服务器中运行多个Web站点,其中每一个站点并不独立占用一台真正的计算机机
基于域名的虚拟主机
基于IP地址的虚拟主机
基于端口的虚拟主机
CentOS 7.6
Win 10
#安装httpd跟bind软件包
[root@localhost ~]# yum -y install httpd bind
#修改主配置文件
[root@localhost ~]# vim /etc/named.conf
......
listen-on port 53 { any; };
......
allow-query { any; };
......
#修改区域配置文件
[root@localhost ~]# vim /etc/named.rfc1912.zones
......
zone "test01.com" IN {
type master;
file "test01.com.zone";
allow-update { none; };
};
zone "test02.com" IN {
type master;
file "test02.com.zone";
allow-update { none; };
};
......
#进入目录进行后续操作
[root@localhost ~]# cd /var/named/
#复制区域配置模板
[root@localhost named]# cp -p named.localhost test01.com.zone
#修改区域配置模板
[root@localhost named]# vim test01.com.zone
......
www IN A 20.0.0.126 ===>追加到末尾
#复制一份刚才修改好的区域配置文件
[root@localhost named]# cp -p test01.com.zone test02.com.zone
#进入该目录进行后续操作
[root@localhost ~]# cd /etc/httpd/conf
#修改虚拟主机配置文件
[root@localhost conf]# vim vhost.conf
DocumentRoot "/var/www/html/test01"
ServerName www.test01.com
ErrorLog "logs/www.test01.com.error_log"
CustomLog "logs/www.test01.com.access_log" common
Require all granted
DocumentRoot "/var/www/html/test02"
ServerName www.test02.com
ErrorLog "logs/www.test02.com.error_log"
CustomLog "logs/www.test02.com.access_log" common
Require all granted
#进入站点目录
[root@localhost conf]# cd /var/www/html
#创建对应的站点目录
[root@localhost html]# mkdir test01 test02
#配置index.html测试使用
[root@localhost html]# vim test01/index.html
this is test01 web
[root@localhost html]# vim test02/index.html
this is test02 web
#修改主配置文件
[root@localhost html]# vim /etc/httpd/conf/httpd.conf
......
Include conf/vhost.conf ===>配置文件末尾追加这一条指向虚拟主机
#开启服务,关闭防火墙,关闭核心防护
[root@localhost html]# systemctl start httpd
[root@localhost html]# systemctl restart named
[root@localhost html]# systemctl stop firewalld
[root@localhost html]# setenforce 0
在Win10浏览器上输入"www.test01.com"
在Win10浏览器上输入"www.test02.com"
CentOS 7.6
Win 10
在上面基于域名的基础上
#修改虚拟主机配置文件
#修改虚拟主机配置文件
[root@localhost html]# vim /etc/httpd/conf/vhost.conf
DocumentRoot "/var/www/html/test01"
ServerName www.test01.com
ErrorLog "logs/www.test01.com.error_log"
CustomLog "logs/www.test01.com.access_log" common
Require all granted
DocumentRoot "/var/www/html/test02"
ServerName www.test01.com
ErrorLog "logs/www.test02.com.error_log"
CustomLog "logs/www.test02.com.access_log" common
Require all granted
#修改主配置文件
[root@localhost html]# vim /etc/httpd/conf/httpd.conf
......
Listen 20.0.0.126:80
Listen 20.0.0.126:8080
......
#开启服务,关闭防火墙,关闭核心防护
[root@localhost html]# systemctl start httpd
[root@localhost html]# systemctl restart named
[root@localhost html]# systemctl stop firewalld
[root@localhost html]# setenforce 0
在Win10浏览器上面输入"www.test01.com:8080"
CentOS 7.6
Win 10
#修改虚拟主机配置文件
[root@localhost conf]# vim /etc/httpd/conf/httpd.conf
DocumentRoot "/var/www/html/test01"
ErrorLog "logs/www.test01.com.error_log"
CustomLog "logs/www.test01.com.access_log" common
Require all granted
DocumentRoot "/var/www/html/test02"
ErrorLog "logs/www.test02.com.error_log"
CustomLog "logs/www.test02.com.access_log" common
Require all granted
#修改主配置文件
[root@localhost conf]# vim /etc/httpd/conf/httpd.conf
......
Listen 20.0.0.26:80
Listen 20.0.0.126:80
......
#开启服务,关闭防火墙,关闭核心防护
[root@localhost html]# systemctl start httpd
[root@localhost html]# systemctl restart named
[root@localhost html]# systemctl stop firewalld
[root@localhost html]# setenforce 0