CentOS 7.6 基于web的虚拟主机配置详解

一、实验环境

  • web服务器:192.168.245.140(CentOS 7.6)
  • 测试客户机:192.168.245.10(win10)

二、实验准备

  • 服务器上:yum安装httpd和bind
  • 客户机上:指定DNS服务器为192.168.245.140

在这里插入图片描述

三、基于不同域名的虚拟主机

域名一:www.yjs.com
域名二:www.cloud.com

第一步:修改DNS主配置文件的监听端口和允许查询的地址为所有

[root@sheng ~]# vim /etc/named.conf 

 12 options {
 13         listen-on port 53 { any; };  <---修改为any
 21         allow-query     { any; };  <---修改为any
 22 

第二步:修改DNS区域配置文件,添加两个不同域名的区域配置,指定各自的区域数据文件

[root@sheng ~]# vim /etc/named.rfc1912.zones 
zone "yjs.com" IN {
        type master;
        file "yjs.com.zone";
        allow-update { none; };
};

zone "cloud.com" IN {
        type master;
        file "cloud.com.zone";
        allow-update { none; };
};

第三步:把区域数据文件的模板拷贝一份

[root@sheng ~]# cd /var/named/

[root@sheng named]# ls
data  dynamic  named.ca  named.empty  named.localhost  named.loopback  slaves

[root@sheng named]# cp -p named.localhost yjs.com.zone

[root@sheng named]# vim yjs.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.245.140   <----因为2个域名指向同一个ip,所以区域数据文件都是这个ip

[root@sheng named]# cp -p yjs.com.zone cloud.com.zone

第四步:启动DNS服务

[root@sheng named]# systemctl start named

第五步:客户机上验证DNS服务正常解析
在这里插入图片描述
第六步:创建虚拟主机配置文件,在/etc/httpd/conf的目录下创建一个extra目录,存放虚拟主机配置文件

[root@sheng conf]# mkdir extra

[root@sheng conf]# cd extra/

[root@sheng extra]# vim vhosts.conf   <----虚拟主机配置文件

<VirtualHost *:80>     
DocumentRoot "/var/www/html/yjs"   <---指定主页目录
ServerName www.yjs.com   <---指定域名
ErrorLog "logs/www.yjs.com.error_log"   <---指定错误日志
CustomLog "logs/www.yjs.com.access_log" common   <---指定访问日志

<Directory "/var/www/html">
        Require all granted    <---指定授权所有用户都可以访问网页
</Directory>
</VirtualHost>

<VirtualHost *:80>
DocumentRoot "/var/www/html/cloud"
ServerName www.cloud.com
ErrorLog "logs/www.cloud.com.error_log"
CustomLog "logs/www.cloud.com.access_log" common
<Directory "/var/www/html">
        Require all granted
</Directory>
</VirtualHost>

第七步:修改apache主配置文件,指向虚拟主机文件

[root@sheng extra]# vim /etc/httpd/conf/httpd.conf

\# Load config files in the "/etc/httpd/conf.d" directory, if any.
IncludeOptional conf.d/*.conf
Include conf/extra/vhosts.conf  <----最后一行添加

第八步:创建2个域名访问的不同主页内容

[root@sheng extra]# cd /var/www/html/
[root@sheng html]# 
[root@sheng html]# mkdir yjs cloud

[root@sheng extra]# vim /var/www/html/yjs/index.html 

<h1>this is yjs</h1>    <----网页内容

[root@sheng extra]# vim /var/www/html/cloud/index.html 

<h1>this is cloud</h1>   <----网页内容

第九步:启动apache服务

[root@sheng extra]# systemctl start httpd

在这里插入图片描述
第十步:客户机上验证访问网页
在这里插入图片描述

四、基于不同端口的虚拟主机

不同端口实际上就是指使用相同的ip相同的域名不同的端口访问网页,得到的是不同的网页内容

第一步:修改虚拟主机配置文件,添加另一个端口的配置块

[root@sheng extra]# vim vhosts.conf 
<VirtualHost *:80>     
DocumentRoot "/var/www/html/yjs"   
ServerName www.yjs.com  
ErrorLog "logs/www.yjs.com.error_log"  
CustomLog "logs/www.yjs.com.access_log" common   

<Directory "/var/www/html">
        Require all granted    
</Directory>
</VirtualHost>

<VirtualHost *:8080>   <---指定另外一个端口为8080
DocumentRoot "/var/www/html/yjs02"   <---指定主页目录为yjs02
ServerName www.yjs.com   <---域名不变
ErrorLog "logs/www.yjs02.com.error_log"   <---指定错误日志
CustomLog "logs/www.yjs02.com.access_log" common  <---指定访问日志
<Directory "/var/www/html">
        Require all granted   <---指定授权所有用户都可以访问网页
</Directory>
</VirtualHost>

第二步:创建网页站点目录yjs02,并为8080端口访问的网页编写一个内容

[root@sheng html]# mkdir yjs02

[root@sheng yjs02]# vim index.html
<h1>this is yjs02</h1>

第三步:重启DNS和apache服务

[root@sheng yjs02]#systemctl restart httpd
[root@sheng yjs02]#systemctl restart named

第四步:客户机上验证访问8080端口
在这里插入图片描述

五、基于不同ip地址的虚拟主机

顾名思义,就是用同一台服务器上的不同ip地址来做虚拟主机供别人访问,不同ip访问不同的网页,虽然实际场景中使用这种方法的很少

第一步:修改虚拟主机配置文件,增加另外一个ip的区块,这里服务器又添加了一块网卡,ip地址为192.168.245.135

[root@sheng extra]# vim vhosts.conf 

<VirtualHost 192.168.245.140:80>
DocumentRoot "/var/www/html/cloud"
ErrorLog "logs/www.cloud.com.error_log"
CustomLog "logs/www.cloud.com.access_log" common
<Directory "/var/www/html">
        Require all granted
</Directory>
</VirtualHost>

<VirtualHost 192.168.245.135:80>  <----指定另外一个ip地址
DocumentRoot "/var/www/html/yjs"
ErrorLog "logs/www.yjs.com.error_log"
CustomLog "logs/www.yjs.com.access_log" common
<Directory "/var/www/html">
        Require all granted
</Directory>
</VirtualHost>

第二步:这里值得注意的是,如果是多个ip地址,需要修改apache的配置文件,增加监听地址

[root@sheng extra]# vim /etc/httpd/conf/httpd.conf 
Listen 192.168.245.140:80
Listen 192.168.245.135:80

第三步:重启DNS服务和apache

[root@sheng extra]# systemctl restart named
[root@sheng extra]# systemctl restart httpd

第四步:验证客户机访问网页
在这里插入图片描述
在这里插入图片描述

六、基于不同ip不同域名的虚拟主机

就是不同的域名映射到同一台服务器的不同ip地址,也有不同的网页提供访问

第一步:修改虚拟主机配置文件,在之前的基础上添加域名即可

[root@sheng extra]# vim vhosts.conf 

<VirtualHost 192.168.245.140:80>
DocumentRoot "/var/www/html/cloud"
ServerName www.cloud.com
ErrorLog "logs/www.cloud.com.error_log"
CustomLog "logs/www.cloud.com.access_log" common
<Directory "/var/www/html">
        Require all granted
</Directory>
</VirtualHost>

<VirtualHost 192.168.245.135:80>
DocumentRoot "/var/www/html/yjs"
ServerName www.yjs.com
ErrorLog "logs/www.yjs.com.error_log"
CustomLog "logs/www.yjs.com.access_log" common
<Directory "/var/www/html">
        Require all granted
</Directory>
</VirtualHost>

第二步:因为不同域名映射到不同ip,所以在区域数据文件要指定不同的ip地址

[root@sheng extra]# vim /var/named/yjs.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.245.135

第三步:重启DNS服务和apache

[root@sheng extra]# systemctl restart named
[root@sheng extra]# systemctl restart httpd

第四步:客户机上验证访问网页
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值