构建虚拟主机
程序员文章站
2022-07-03 14:13:41
...
构建虚拟主机
虚拟主机概述
在同一台服务器中运行多个web站点,其中每一个站点并不独立占用一台真正的计算机
通过虚拟web主机可以充分利用服务器的硬件资源,从而大大降低网站构建及运行成本
使用httpd可以非常方便的构建虚拟主机服务器,只需要运行一个httpd服务就能够同时支撑大量的web站点
httpd支持的虚拟主机类型
基于域名
为每个虚拟主机使用不同的域名,但是其对应的IP地址使相同的
域名不同–IP相同,端口相同
基于域名是最为普遍的虚拟web主机类型
基于IP地址
为每个虚拟主机使用不同的域名,且各自对应的IP地址也不相同
这种方式需要为服务器配备多个网络接口,因此应用不是很广泛
IP不同,端口相同
基于端口
这种方式并不使用域名,IP地址来区分不同的站点内容,而是使用不同的TCP端口号
因此用户在浏览不同的虚拟站点时需要同时制定端口号才能访问
IP相同,端口不同
实验
[aaa@qq.com ~]# cd /etc/sysconfig/network-scripts/
[aaa@qq.com network-scripts]# cp ifcfg-ens33 ifcfg-ens36
[aaa@qq.com network-scripts]# vim ifcfg-ens36 //配置双网卡
[aaa@qq.com network-scripts]# yum -y install bind
[aaa@qq.com network-scripts]# vim /etc/named.conf
[aaa@qq.com network-scripts]# vim /etc/named.rfc1912.zones
zone "hui.com" IN {
type master;
file "hui.com.zone";
allow-update { none; };
};
zone "fang.com" IN {
type master;
file "fang.com.zone";
allow-update { none; };
};
[aaa@qq.com network-scripts]# cd /var/named/
[aaa@qq.com named]# cp named.localhost fang.com.zone
[aaa@qq.com named]# vim fang.com.zone
[aaa@qq.com named]# vim fang.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.20.200
[aaa@qq.com named]# cp fang.com.zone hui.com.zone
基于域名
[aaa@qq.com named]# cd /etc/httpd/conf/
[aaa@qq.com conf]# mkdir extra
[aaa@qq.com conf]# cd extra/
[aaa@qq.com extra]# vim vhost.conf
<VirtualHost *:80>
DocumentRoot "/var/www/html/hui"
ServerName www.hui.com
Errorlog "logs/www.hui.com.error_log"
CustomLog "logs/www.hui.com.access_log" common
<Directory> "/var/www/html">
Require all granted
</Directory>
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/var/www/html/fang"
ServerName www.fang.com
Errorlog "logs/www.fang.com.error_log"
CustomLog "logs/www.fang.com.access_log" common
<Directory> "/var/www/html">
Require all granted
</Directory>
</VirtualHost>
[aaa@qq.com extra]# vim /etc/httpd/conf/httpd.conf
Include conf/extra/vhost.conf
[aaa@qq.com extra]# systemctl stop firewalld
[aaa@qq.com extra]# iptables -F
[aaa@qq.com extra]# setenforce 0
[aaa@qq.com extra]# mkdir /var/www/html/hui
[aaa@qq.com extra]# mkdir /var/www/html/fang
[aaa@qq.com extra]# vim /var/www/html/hui/index.html
<h1>this is hui</h1>
[aaa@qq.com extra]# vim /var/www/html/fang/index.html
<h1>this is fang</h1>
[aaa@qq.com extra]# vim /etc/httpd/conf/httpd.conf
[aaa@qq.com extra]# systemctl restart httpd.service
在win10虚拟机
基于端口
[aaa@qq.com extra]# vim vhost.conf
<VirtualHost *:80>
DocumentRoot "/var/www/html/hui"
ServerName www.hui.com
Errorlog "logs/www.hui.com.error_log"
CustomLog "logs/www.hui.com.access_log" common
<Directory> "/var/www/html">
Require all granted
</Directory>
</VirtualHost>
<VirtualHost *:8080>
DocumentRoot "/var/www/html/hui"
ServerName www.hui.com
Errorlog "logs/www.hui.com.error_log"
CustomLog "logs/www.hui.com.access_log" common
<Directory> "/var/www/html">
Require all granted
</Directory>
</VirtualHost>
[aaa@qq.com extra]# vim /etc/httpd/conf/httpd.conf
[aaa@qq.com extra]# systemctl restart httpd.service
基于IP地址
[aaa@qq.com extra]# vim vhost.conf
[aaa@qq.com extra]# vim /etc/httpd/conf/httpd.conf
[aaa@qq.com extra]# vim /var/www/html/hui01/index.html