CentOS 7部署DNS主从复制及Apache域名虚拟主机

一、安装主DNS服务器

1、配置主DNS

[ ~]# yum -y install bind bind-chroot bind-utils    <!--安装DNS依赖程序-->
[ ~]# cp /etc/named.conf /etc/named.conf.bak   <!--备份主配置文件-->
[ ~]# echo "" > /etc/named.conf       <!--清空主配置文件内容->
[ ~]# vim /etc/named.conf         <!--编辑主配置文件-->
options{              <!--全局配置文件-->
    listen-on   port    53  { any; };      <!--53号端口监听所有IP地址-->
    directory "/var/named";           <!--区域配置文件位置-->
};
zone    "benet.com" IN  {          <!--正向解析区域名称-->
    type    master;                       <!--主DNS服务器-->
    file    "benet.com.zone";          <!--正向解析区域配置文件名字-->
    allow-transfer { 192.168.100.20; };       <!--备用DNSIP地址-->
};
zone    "accp.com"  IN  {
    type    master;
    file    "accp.com.zone";
    allow-transfer  { 192.168.100.20; };
};
[ ~]# named-checkconf -z /etc/named.conf  <!--检查主配置文件是否错误-->

2、配置benet区域的正向解析区域

[ ~]# vim /var/named/benet.com.zone  <!--编制benet的正向解析配置文件-->
$TTL    86400         <!--解析资源记录有效时间24小时-->
@   SOA benet.com.  root.benet.com(       <!--域名为benet.com-->
    2020020910         <!--更新序列号-->
    1H             <!--刷新时间1小时-->
    15M              <!--重试时间15分钟-->
    1W           <!--失效时间1周-->
    1D             <!--无效解析记录保存时间1天-->
)
@   NS  centos01.benet.com. <!--主权威服务器是DNS完全合格域名-->
    NS  centos02.benet.com.              <!--备-->
centos01 A  192.168.100.10        <!--记录正向解析条目-->
centos02 A  192.168.100.20        <!--记录正向解析条目-->
ftp  A  192.168.100.10           <!--ftp服务器IP地址-->
www A  192.168.100.10          <!--网站服务器IP地址-->
[ ~]# named-checkzone benet.com /var/named/benet.com.zone  <!--检查正向解析区域配置文件是否正常-->
zone benet.com/IN: loaded serial 2020020910
OK
[ ~]# chmod 755 /var/named/benet.com.zone        
     <!--benet正向解析区域配置文件添加权限-->
[ ~]# chown named:named /var/named/benet.com.zone         
              <!--修改属主属组-->

3、配置accp区域的正向解析区域

[ ~]#cp /var/named/benet.com.zone  /var/named/accp.com.zone     
<!--复制benet正向解析区域配置文件到accp正向解析区域配置文件-->

[ ~]# vim /var/named/accp.com.zone
          <!--编辑accp正向解析区域配置文件-->
$TTL    86400 
@   SOA accp.com.   root.accp.com(
    2020020910
    1H
    15M
    1W
    1D
)
@   NS  centos01.accp.com.         
    NS  centos02.accp.com.         
centos01 A  192.168.100.10       
ftp  A  192.168.100.10            
www A  192.168.100.10          
[ ~]# named-checkzone accp.com /var/named/accp.com.zone  
       <!--检查accp正向解析区域配置文件是否正常-->     
zone accp.com/IN: loaded serial 2020020910
OK

4、启动DNS服务

[ ~]# systemctl start named           <!--启动服务--> 
[ ~]# systemctl enable named  <!--设置服务开机自动启动-->

5、网卡添加DNS

[ ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens32       <!--编辑网卡配置文件-->     
DNS1=192.168.100.20           <!--添加主DNS(主从互指)-->     
DNS2=192.168.100.10             <!--备用DNS-->     
[ ~]# systemctl restart network <!--重新启动网卡服务-->     
[ ~]# systemctl restart named <!--重新启动DNS服务-->

二、安装从DNS服务器

1、配置从DNS

[ ~]# yum -y install bind bind-chroot bind-utils
[ ~]# cp /etc/named.conf /etc/named.conf.bak
[ ~]# echo "" > /etc/named.conf
[ ~]# vim /etc/named.conf
options {
    listen-on   port    53  { any; };
    directory   "/var/named";
};
zone    "benet.com" IN  {
    type    slave;
    file    "slaves/benet.com.zone";
    masters { 192.168.100.10; };
};
zone    "accp.com"  IN  {
    type    slave;
    file    "slaves/accp.com.zone";
    masters { 192.168.100.10; };
};

2、启动DNS服务

[ ~]# systemctl start named
[ ~]# systemctl enable named

3、检测从是否自动复制主的配置文件

[ ~]# cd /var/named/
[ named]# ls
chroot  data  dynamic  named.ca  named.empty  named.localhost  named.loopback  slaves
[ named]# cd slaves/
[ slaves]# ls
accp.com.zone  benet.com.zone

4、网卡添加DNS

[ ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens32
DNS1=192.168.100.10
DNS2=192.168.100.20
[ ~]# systemctl restart network
[ ~]# systemctl restart named

5、测试DNS解析

1)主DNS测试解析

[ ~]# nslookup ftp.benet.com
Server:     192.168.100.20
Address:    192.168.100.20#53

Name:   ftp.benet.com
Address: 192.168.100.10

2)客户端测试解析

CentOS 7部署DNS主从复制及Apache域名虚拟主机

3)备用DNS测试解析

[ ~]# nslookup ftp.accp.com
Server:     192.168.100.10
Address:    192.168.100.10#53

Name:   ftp.accp.com
Address: 192.168.100.10

三、配置基于域名的Apache服务器

1、安装配置Apache

[ ~]# tar zxvf /mnt/httpd-2.2.17.tar.gz -C /usr/src/ 
<!--解压缩apache包-->
[ ~]# cd /usr/src/httpd-2.2.17/
[ httpd-2.2.17]# ./configure 
--prefix=/usr/local/httpd
--enable-so --enable-rewrite 
-enable-charset-lite 
--enable-cgi
         <!--配置Apache-->
[ httpd-2.2.17]# make && make install <!--编译及安装Apache-->
[ ~]# ln -s /usr/local/httpd/bin/* /usr/local/bin/ <!--优化Apache执行命令-->
[ ~]# cp /usr/local/httpd/bin/apachectl /etc/init.d/httpd
           <!--生成Apache服务-->
[ ~]# chmod +x /etc/init.d/httpd  <!--添加执行权限-->
[ ~]# vim /etc/init.d/httpd  <!--修改Apache服务控制文件-->
#chkconfig:35 80 20 
[ ~]# chkconfig --add httpd <!--添加系统服务-->
[ ~]# chkconfig --level 35 httpd on<!--设置开机自动启动-->
[ ~]# systemctl start httpd  <!--启动Apache服务-->

2、配置虚拟主机

[ ~]# mkdir /var/www      <!--创建网站根目录-->
[ ~]# mkdir /var/www/benetcom <!--创建虚拟站点根目录-->
[ ~]# mkdir /var/www/accpcom<!--创建虚拟站点根目录-->
[ ~]# echo "www.benet.com" >  /var/www/benetcom/index.html         
<!--设置网站主页-->
[ ~]# echo "www.accp.com" >   /var/www/accpcom/index.html         
<!--设置网站主页-->
[ ~]# vim /usr/local/httpd/conf/httpd.conf   
         <!--编制apache主配置文件-->
98 ServerName 192.168.100.10:80 <!--apache服务器IP地址-->
389 Include conf/extra/httpd-vhosts.conf <!--支持虚拟主机-->
[ ~]# vim /usr/local/httpd/conf/extra/httpd-vhosts.conf        <!--编辑虚拟主机配置文件-->
NameVirtualHost 192.168.100.10:80       <!--虚拟主机IP地址-->

<Directory "/var/www/">
        Order   allow,deny
        allow   from    all
 </Directory>

<VirtualHost www.benet.com:80>      <!--虚拟主机域名-->
    DocumentRoot "/var/www/benetcom/"   <!--网站根目录-->
    ServerName www.benet.com             <!--网站域名-->
    CustomLog "logs/www.benet.com_access_log" common  
                   <!--访问成功日志-->
</VirtualHost>
<VirtualHost www.accp.com:80>
    DocumentRoot "/var/www/accpcom/"
    ServerName www.accp.com
    CustomLog "logs/www.accp.com_access_log" common
</VirtualHost>
[ ~]# systemctl restart httpd <!--重新启动httpd服务-->
[ ~]# httpd -t <!--检查Apache服务配置文件是否正常-->
Syntax OK

3、客户端访问测试

CentOS 7部署DNS主从复制及Apache域名虚拟主机

CentOS 7部署DNS主从复制及Apache域名虚拟主机

—————— 本文至此结束,感谢阅读 ——————

相关推荐