●在同一台服务器中运行多个Web站点,其中每一个站点并不独立占用一台真正的计算机
虚拟Web主机指的是在同一台服务器中运行多个Web站点,其中的每一个站点实际上并不独自占用整个服务器,因此
被称为“虚拟Web主机”。 通过虚拟Web主机服务可以充分利用服务器的硬件资源,从而大大降低网站构建及运行成本。
●基于域名的虚拟主机 (应用最为广泛)
●基于IP地址的虚拟主机
●基于端口的虚拟主机
特征关系对应分别如下
ip相同、端口相同,域名不同;
ip不同,端口不同;
构建两个虚拟web站点
1.www.jj.com
2.www.kk.com
实际流程如下:
使用一台httpd服务器搭建,ip地址为192.168.68.145.具体构建过程如下:
1.为虚拟主机提供域名解析
安装bind和httpd服务:yum install -y bind httpd
[root@localhost ~]# vim /etc/named.conf
[root@localhost ~]# head -21 /etc/named.conf |tail
options {
listen-on port 53 { 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; };
[root@localhost ~]# vim /etc/named.rfc1912.zones
[root@localhost ~]# vim /etc/named.rfc1912.zones
[root@localhost ~]# head -34 /etc/named.rfc1912.zones | tail
zone "ll.com" IN {
type master;
file "ll.com.zone";
allow-update { none; };
};
zone "cc.com" IN {
type master;
file "cc.com.zone";
allow-update { none; };
[root@localhost ~]# cd /var/named/
[root@localhost named]# ls
data dynamic named.ca named.empty named.localhost named.loopback slaves
[root@localhost named]# cp -p named.localhost ll.com.zone
[root@localhost named]# vim ll.com.zone
[root@localhost named]# cp -p ll.com.zone cc.com.zone
[root@localhost named]# cat ll.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.68.145
[root@localhost named]# cat cc.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.68.145
[root@localhost named]# systemctl start named
[root@localhost named]# systemctl stop firewalld.service
[root@localhost named]# setenforce
usage: setenforce [ Enforcing | Permissive | 1 | 0 ]
[root@localhost named]# setenforce 0
[root@localhost named]# systemctl start httpd
测试是否成功
[root@localhost named]# cd /etc/httpd/
[root@localhost httpd]# cd conf
[root@localhost conf]# ls
httpd.conf magic
[root@localhost conf]# mkdir extra
[root@localhost conf]# ls
extra httpd.conf magic
[root@localhost conf]# cd extra/
[root@localhost extra]# vim vhost.conf
[root@localhost extra]# cat vhost.conf
<VirtualHost *:80>
DocumentRoot "/var/www/html/ll/"
ServerName www.ll.com
ErrorLog "logs/www.ll.com.error_log"
CustomLog "logs/www.ll.com.access_log" common
<Directory "/var/www/html/">
Require all granted
</Directory>
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/var/www/html/cc/"
ServerName www.cc.com
ErrorLog "logs/www.cc.com.error_log"
CustomLog "logs/www.cc.com.access_log" common
<Directory "/var/www/html/">
Require all granted
</Directory>
</VirtualHost>
[root@localhost extra]# vim /etc/httpd/conf/httpd.conf
[root@localhost extra]# tail -2 /etc/httpd/conf/httpd.conf
IncludeOptional conf.d/*.conf
Include conf/extra/vhost.conf
[root@localhost extra]# systemctl restart httpd
根据以上过程可以继续配置来完成基于端口的构建:
[root@localhost ~]# vim /etc/httpd/conf/extra/vhost.conf
[root@localhost ~]# tail /etc/httpd/conf/extra/vhost.conf
<VirtualHost *:8080>
DocumentRoot "/var/www/html/cc02/"
ServerName www.cc.com
ErrorLog "logs/www.cc02.com.error_log"
CustomLog "logs/www.cc02.com.access_log" common
<Directory "/var/www/html/">
Require all granted
</Directory>
</VirtualHost>
[root@localhost ~]# cd /var/www/html/
[root@localhost html]# mkdir cc02
[root@localhost html]# ls
cc cc02 ll
[root@localhost html]# cd cc02/
[root@localhost cc02]# vim index.html
[root@localhost cc02]# cat index.html
<h2>this is cc02 web</h2>
[root@localhost cc02]# vim /etc/httpd/conf/httpd.conf
[root@localhost cc02]# head -43 /etc/httpd/conf/httpd.conf | tail -3
Listen 192.168.68.145:80
Listen 192.168.68.145:8080
#Listen 80
[root@localhost cc02]# systemctl restart httpd
测试验证:输入http://www.cc02.com:8080/即可
[root@localhost cc02]# vim /etc/httpd/conf/extra/vhost.conf
[root@localhost cc02]# cat /etc/httpd/conf/extra/vhost.conf
<VirtualHost 192.168.68.145:80>
DocumentRoot "/var/www/html/ll/"
#ServerName www.ll.com
ErrorLog "logs/www.ll.com.error_log"
CustomLog "logs/www.ll.com.access_log" common
<Directory "/var/www/html/">
Require all granted
</Directory>
</VirtualHost>
<VirtualHost 192.168.68.146:80>
DocumentRoot "/var/www/html/ll02/"
#ServerName www.ll.com
ErrorLog "logs/www.ll02.com.error_log"
CustomLog "logs/www.ll02.com.access_log" common
<Directory "/var/www/html/">
Require all granted
</Directory>
</VirtualHost>
#<VirtualHost *:80>
# DocumentRoot "/var/www/html/cc/"
# ServerName www.cc.com
# ErrorLog "logs/www.cc.com.error_log"
# CustomLog "logs/www.cc.com.access_log" common
# <Directory "/var/www/html/">
# Require all granted
# </Directory>
#</VirtualHost>
#
#<VirtualHost *:8080>
# DocumentRoot "/var/www/html/cc02/"
# ServerName www.cc.com
# ErrorLog "logs/www.cc02.com.error_log"
# CustomLog "logs/www.cc02.com.access_log" common
# <Directory "/var/www/html/">
# Require all granted
# </Directory>
#</VirtualHost>
[root@localhost cc02]# mkdir ll02
[root@localhost cc02]# cd ll02
[root@localhost ll02]# vim index.html
[root@localhost ll02]# cat index.html
<h2>this is ll02 146</h2>
[root@localhost ll02]# vim /etc/httpd/conf/httpd.conf
[root@localhost ll02]# head -43 /etc/httpd/conf/httpd.conf | tail -5
# prevent Apache from glomming onto all bound IP addresses.
#
Listen 192.168.68.145:80
Listen 192.168.68.146:80
#Listen 192.168.68.145:8080
测试验证:输入192.168.68.146
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。