首页 > 代码库 > httpd基于域名的虚拟主机

httpd基于域名的虚拟主机


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


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


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


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

[root@localhost ~]# cd /var/named/chroot/etc/
[root@localhost etc]# vi named.conf 
[root@localhost ~]# 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

[root@localhost etc]# cd ../var/named/
[root@localhost named]# vi leslie.cn.zone 
[root@localhost ~]# 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
[root@localhost named]# vi leslie.com.zone 
[root@localhost ~]# 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服务:

[root@localhost named]# /etc/init.d/named restart
停止 named:.                                              [确定]
启动 named:                                               [确定]


解析测试:

[root@localhost 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安装: 

[root@localhost ~]# 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 admin@leslie.com
    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 admin@leslie.cn
   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 中的一样:

[root@localhost conf]# cd /var/www/html/
[root@localhost html]# mkdir www.leslie.com
[root@localhost html]# mkdir www.leslie.cn
[root@localhost html]# echo "www.leslie.com" >>www.leslie.com/index.html
[root@localhost html]# echo "www.leslie.cn" >>www.leslie.cn/index.html
[root@localhost html]# cat www.leslie.com/index.html 
www.leslie.com
[root@localhost html]# cat www.leslie.cn/index.html 
www.leslie.cn


重启httpd服务 

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

检测服务:

[root@localhost ~]# netstat -utpln |grep httpd
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      53492/httpd


测试:

[root@localhost ~]# elinks --dump www.leslie.com
   www.leslie.com
[root@localhost ~]# elinks --dump www.leslie.cn
   www.leslie.cn


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

httpd基于域名的虚拟主机