httpd基于域名的虚拟主机

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了httpd基于域名的虚拟主机相关的知识,希望对你有一定的参考价值。


搭建基于域名的网站虚拟主机。


web虚拟主机服务器centos6.5192.168.200.202搭建基于域名的虚拟主机
dns、ftp服务器centos6.5192.168.200.254提供DNS解析,ftp下载


1:在dns服务器上设置dns解析:


修改dns的配置文件,设置区域文件名称和所在位置。

[[email protected] ~]# cd /var/named/chroot/etc/
[[email protected] etc]# vi named.conf 
[[email protected] ~]# cat /var/named/chroot/etc/named.conf 
options {
listen-on port 53{ any; };
forwarders { 202.106.0.20;114.114.114.114;8.8.8.8; };
directory "/var/named";
};
zone "leslie.cn." IN {
type master;
file "leslie.cn.zone";
};
zone "leslie.com." IN {
type master;
file "leslie.com.zone";
};


配置区域文件,添加记录。两个虚拟主机要建两个区域文件,

分别是www.leslie.com.zone和www.leslie.cn.zone

[[email protected] etc]# cd ../var/named/
[[email protected] named]# vi leslie.cn.zone 
[[email protected] ~]# cat /var/named/chroot/var/named/leslie.cn.zone 
$TTL 86400
@INSOAleslie.cn.root.leslie.cn. (
20150630
1H
2M
3W
1D
)
@INNSns.leslie.cn.
mailINMX 10mail.leslie.cn.
@INA192.168.200.254
mailINA192.168.200.254
nsINA192.168.200.254
ftpINA192.168.200.254
ntpINCNAMEns
wwwINA192.168.200.202
[[email protected] named]# vi leslie.com.zone 
[[email protected] ~]# cat /var/named/chroot/var/named/leslie.com.zone 
$TTL 86400
@INSOAleslie.com.root.leslie.com. (
20150630
1H
2M
3W
1D
)
@INNSns.leslie.com.
mailINMX 10mail.leslie.com.
@INA192.168.200.254
mailINA192.168.200.254
nsINA192.168.200.254
ftpINA192.168.200.254
ntpINCNAMEns
wwwINA192.168.200.202


设置完成后重启named服务:

[[email protected] named]# /etc/init.d/named restart
停止 named:.                                              [确定]
启动 named:                                               [确定]


解析测试:

[[email protected] named]# nslookup
> server 192.168.200.254
Default server: 192.168.200.254
Address: 192.168.200.254#53
> www.leslie.com 
Server:192.168.200.254
Address:192.168.200.254#53
Name:www.leslie.com
Address: 192.168.200.202
> www.leslie.cn
Server:192.168.200.254
Address:192.168.200.254#53


安装httpd服务:

    使用yum安装: 

[[email protected] ~]# yum -y install httpd &>>/dev/null  ##红色部分是不看输出内容,不显示信息

    编辑配置文件/etc/httpd/conf/httpd.conf

    277 ServerName www.leslie.com:80 
    990 NameVirtualHost 192.168.200.202:80
    991 <VirtualHost 192.168.200.202:80>
    992     ServerAdmin [email protected]
    993     DocumentRoot "/var/www/html/www.leslie.com"
    994     ServerName www.leslie.com
    995     ErrorLog logs/www.leslie.com.com-error_log
    996     CustomLog logs/www.leslie.com.com-access_log common
    997 </VirtualHost>
    998 <VirtualHost 192.168.200.202:80>
    999     ServerAdmin [email protected]
   1000     DocumentRoot "/var/www/html/www.leslie.cn"
   1001     ServerName www.leslie.cn
   1002     ErrorLog logs/www.leslie.cn-error_log
   1003     CustomLog logs/www.leslie.cn-access_log common
   1004 </VirtualHost>

新建网页根目录 和配置文件中DocumentRoot 中的一样:

[[email protected] conf]# cd /var/www/html/
[[email protected] html]# mkdir www.leslie.com
[[email protected] html]# mkdir www.leslie.cn
[[email protected] html]# echo "www.leslie.com" >>www.leslie.com/index.html
[[email protected] html]# echo "www.leslie.cn" >>www.leslie.cn/index.html
[[email protected] html]# cat www.leslie.com/index.html 
www.leslie.com
[[email protected] html]# cat www.leslie.cn/index.html 
www.leslie.cn


重启httpd服务 

[[email protected] ~]# /etc/init.d/httpd restart
停止 httpd:                                               [确定]
正在启动 httpd:                                           [确定]

检测服务:

[[email protected] ~]# netstat -utpln |grep httpd
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      53492/httpd


测试:

[[email protected] ~]# elinks --dump www.leslie.com
   www.leslie.com
[[email protected] ~]# elinks --dump www.leslie.cn
   www.leslie.cn


本文出自 “向往技术的小白” 博客,请务必保留此出处http://lesliecheung.blog.51cto.com/12622169/1956240

以上是关于httpd基于域名的虚拟主机的主要内容,如果未能解决你的问题,请参考以下文章

httpd基于域名虚拟机及主站迁移

Apache配置虚拟主机的三种方法(基于IP端口域名)

基于域名的虚拟主机

linux 搭建基于域名的虚拟主机

2.4 httpd 构建虚拟Web主机的三种方式

httpd服务器之——Apache的相关配置与应用