httpd支持的虚拟主机类型在同一台物理服务器中运行多个web站点,其中每一个站点并不独立占用一台真正的计算机
* 基于域名的虚拟主机
* 基于IP地址的虚拟主机
* 基于端口的虚拟主机
案例一——基于域名
www.accp.com ip地址为本机地址
www.kgc.com ip地址为本机地址
一台Linux主机做服务器
一台win7主机做测试
[root@localhost ~]# yum install httpd bind -y //安装http,dns服务
[root@localhost ~]# vim /etc/named.conf ##主配置文件
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 ##配置区域配置文件
zone "localhost" IN { ##复制两个模板到下面
type master;
file "named.localhost";
allow-update { none; };
};
zone "accp.com" IN { ##修改localhost为accp.com
type master;
file "accp.com.zone"; ##创建区域数据配置文件
allow-update { none; };
};
zone "kgc.com" IN { ##修改localhost为kgc.com
type master;
file "kgc.com.zone"; ##创建区域数据配置文件
allow-update { none; };
};
[root@localhost ~]# cd /var/named/ ##切换到/var/named目录下
[root@localhost named]# cp -p named.localhost accp.com.zone
##复制模板为accp.com.zone
[root@localhost named]# vim accp.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.13.133 ##删除ipv6 添加域名解析地址为本机
[root@localhost named]# cp -p accp.com.zone kgc.com.zone
##编辑kgc的区域数据配置文件,内容不需要更改
[root@localhost named]# systemctl start named ##开启dns服务
[root@localhost named]# systemctl stop firewalld.service ##关闭防火墙
[root@localhost named]# setenforce 0 ##关闭增强功能
[root@localhost named]# cd /etc/httpd/conf ##切换到http配置目录中
[root@localhost named]# mkdir extra ##创建扩展配置文件目录
[root@localhost named]# cd extra
[root@localhost named]# vim vhost.conf ##编辑扩展配置文件
##标签 *所有ip 80端口
Documentroot "/var/www/html/accp/" ##站点目录
ServerName www.accp.com ##域名
Errorlog "logs/www.accp.com.error_log" ##错误日志文件
Customlog "logs/www.accp.com.access_log" common ##访问日志
##控制目录权限
Require all granted ##允许所有人可以访问
##上面的logs是软连接/var/log/httpd ##下面的只要修改accp为kgc即可
Documentroot "/var/www/html/kgc/"
ServerName www.kgc.com
Errorlog "logs/www.kgc.com.error_log"
Customlog "logs/www.kgc.com.access_log" common
Require all granted
[root@localhost extra]# cd /var/www/html/ ##切换到网页站点中
[root@localhost html]# mkdir accp kgc ##创建两个网站的站点目录
[root@localhost html]# cd accp/
[root@localhost accp]# vim index.html ##编辑accp网页内容
this is a accp web
[root@localhost html]# cd kgc/
[root@localhost accp]# vim index.html ##编辑kgc网页内容
this is a kgc web
[root@localhost www]# cd /etc/httpd/conf ##切换到http的配置文件目录
[root@localhost conf]# vim httpd.conf ##编辑主配置文件
Include conf/extra/vhost.conf ##在末行加入扩展配置文件目录
[root@localhost conf]# systemctl start httpd ##启动http服务
[root@lolocalhost extra]# vim vhost.conf
Documentroot "/var/www/html/kgc/"
ServerName www.kgc.com
Errorlog "logs/www.kgc.com.error_log"
Customlog "logs/www.kgc.com.access_log" common
Require all granted
##端口号改成8080
Documentroot "/var/www/html/kgc02/" ##目录更改为02
ServerName www.kgc.com
Errorlog "logs/www.kgc02.com.error_log" ##日志文件更改为02
Customlog "logs/www.kgc02.com.access_log" common
Require all granted
[root@localhost extra]# cd /var/www/html/
[root@localhost html]# mkdir kgc02
[root@localhost html]# vim index.html ##编辑kgc02网页的内容
this is a kgc02 web
[root@localhost html]# vim /etc/httpd/conf/httpd.conf ##修改配置文件
Listen 192.168.13.133:80 ##修改ipv4监听端口
Listen 192.168.13.133:8080
#Listen 80 ##将ipv6端口注释
[root@localhost html]# systemctl restart httpd.service ##重启网络服务
[root@localhost html]# netstat -ntap ##查看监听端口开启状态
tcp 0 0 192.168.13.133:8080 0.0.0.0:* LISTEN 4093/httpd
tcp 0 0 192.168.13.133:80 0.0.0.0:* LISTEN 4093/httpd
在Linux上添加一块网卡做不同IP
[root@lolocalhost extra]# vim vhost.conf
##修改ip地址
Documentroot "/var/www/html/accp/" ##域名此处没有用处删除
Errorlog "logs/www.accp.com.error_log"
Customlog "logs/www.accp.com.access_log" common
Require all granted
##修改ip地址
Documentroot "/var/www/html/accp02/" ##修改目录accp02和日志
Errorlog "logs/www.accp02.com.error_log"
Customlog "logs/www.accp02.com.access_log" common
Require all granted
[root@localhost extra]# cd /var/www/html/
[root@localhost html]# mkdir accp02 ##创建132ip地址的目录站点内容
[root@localhost html]# cd accp02/
[root@localhost accp02]# echo "this is 132 web" > index.html
[root@localhost accp02]# cd ../
[root@localhost html]# cd accp ##修改133ip地址网页内容
[root@localhost accp]# vim index.html
this is 133 web
[root@localhost html]# vim /etc/httpd/conf/httpd.conf ##修改配置文件
Listen 192.168.13.133:80 ##添加两个ip的端口监听
Listen 192.168.13.132:80
#Listen 192.168.13.133:8080 ##注释8080端口的
#Listen 80 ##将ipv6端口注释
[root@localhost conf]# systemctl restart httpd.service ##重启网络服务
[root@lolocalhost extra]# vim vhost.conf
Documentroot "/var/www/html/accp/"
ServerName www.accp.com ##添加域名
Errorlog "logs/www.accp.com.error_log"
Customlog "logs/www.accp.com.access_log" common
Require all granted
Documentroot "/var/www/html/accp02/"
ServerName www.naccp.com ##添加另一个域名
Errorlog "logs/www.accp02.com.error_log"
Customlog "logs/www.accp02.com.access_log" common
Require all granted
[root@localhost /]# vim /etc/named.rfc1912.zones
zone "accp.com" IN {
type master;
file "accp.com.zone";
allow-update { none; };
};
zone "naccp.com" IN {
type master;
file "naccp.com.zone";
allow-update { none; };
};
[root@localhost named]# cd /var/named/
[root@localhost named]# cp -p accp.com.zone naccp.com.zone ##复制
[root@localhost named]# vim naccp.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.13.132 ##此处的naccp要修改132ip地址
[root@localhost named]# systemctl restart named ##重启DNS
[root@localhost named]# systemctl restart httpd.service ##重启网络
构建虚拟主机——基于域名,端口,IP