虚拟Web主机
在同一台服务器中运行多个Web站点,其中每一个站点并不独立
占用一台真正的计算机
httpd支持的虚拟主机类型
1.基于域名的虚拟主机
2.基于IP地址的虚拟主机
3.基于端口的虚拟主机
范例演示
基于域名的虚拟主机
第一步:环境部署
1.安装DNS的软件包bind和httpd的服务,用以测试
[[email protected] ~]# yum install bind httpd -y
...
2.为虚拟主机提供域名解析
[[email protected] ~]# vim /etc/named.conf //进入主配置文件
options {
listen-on port 53 { any; }; //将原有的127.0.0.1替换成any
...
allow-query { any; }; //将localhost替换成any
[[email protected] ~]# vim /etc/named.rfc1912.zones //进入区域配置文件
zone "accp.com" IN { //第一个正向域名
type master;
file "accp.com.zone"; //指向区域数据配置文件accp.com.zone
allow-update { none; };
};
zone "kgc.com" IN { //第二个正向域名
type master;
file "kgc.com.zone"; //指向区域数据配置文件kgc.com.zone
allow-update { none; };
};
[[email protected] ~]# cd /var/named/ //进入/var/named/目录
[[email protected] named]# cp -p named.localhost accp.com.zone //复制模板文件named.localhost为accp.com.zone的区域数据配置文件
[[email protected] 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.235.129 //添加解析A记录,并指向本主机IP地址
[[email protected] named]# cp -p accp.com.zone kgc.com.zone //复制accp.com.zone为kgc.com.zone的区域数据配置文件,无需进行修改
[[email protected] named]# systemctl start named //启动域名解析服务
[[email protected] named]# systemctl stop firewalld.service //关闭防火墙
[[email protected] named]# setenforce 0 //关闭增强型安全功能
第二步:使用客户机进行测试DNS服务
1.修改客户机的DNS服务器地址为虚拟主机的地址
2.打开客户机的cmd命令提示符,使用nslookup命令加上域名即可获取解析到的地址
第三步:设定虚拟主机相关配置文件
1.配置虚拟主机的配置文件
[[email protected] named]# cd /etc/httpd/conf //进入配置文件的conf目录
[[email protected] conf]# mkdir extra //创建扩展目录
[[email protected] conf]# cd extra/ //进入目录
[[email protected] extra]# vim vhost.conf //编辑虚拟主机配置文件,可自行定义
<VirtualHost *:80> //标签 *所有ip 80端口
DocumentRoot "/var/www/html/accp/" //站点目录
ServerName www.accp.com //站点域名
ErrorLog "logs/www. accp.com.error_log" //站点错误日志文件,logs为/var/log/httpd的软链接(相对路径)
CustomLog "logs/www.accp.com.access_1og" common //站点访问日志
<Directory "/var/www/html/"> //控制目录权限
Require all granted //允许所有访问
</Directory>
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/var/ www/ html/kgc/"
ServerName www. kgc. com
ErrorLog "logs/www.kgc.com.error_1og"
CustomLog "logs/www.kgc.com.access_log” common
<Directory "/var/www/html/">
Require all granted
</Directory>
</VirtualHost>
2.配置网页
[[email protected] extra]# cd /var/www/html/ //进入站点目录
[[email protected] html]# mkdir accp kgc
[[email protected] html]# cd accp/
[[email protected] accp]# vim index.html
<h1>this is accp web</h1>
[[email protected] accp]# cd ../kgc/
[[email protected] kgc]# vim index.html
<h1>this is kgc web</h1>
3.进入httpd.conf配置文件,声明包含先前创建的虚拟主机子配置文件
[[email protected] kgc]# cd /etc/httpd/conf
[[email protected] conf]# vim httpd.conf
...
Include conf/extra/vhost.conf //在末行接入此条目,用以声明包含虚拟主机子配置文件
[[email protected] extra]# systemctl start httpd //启动httpd服务
第四步:使用客户机访问网页
二、基于端口的虚拟主机
1.编辑虚拟主机配置文件
[[email protected] extra]# vim vhost.conf
... //省略之前设定好得部分配置
<VirtualHost *:8080> //添加accp域名的8080端口
DocumentRoot "/var/www/html/accp02"
ServerName www.accp.com
ErrorLog "logs/www.accp02.com.error_log"
CustomLog "logs/www.accp02.com.access_log" common
<Directory "/var/www/html/">
Require all granted
</Directory>
</VirtualHost>
2.创建新建的8080端口的网页站点
[[email protected] extra]# cd /var/www/html/
[[email protected] html]# mkdir accp02
[[email protected] html]# cd accp02/
[[email protected] accp02]# vim index.html
<h1>this is accp02 test web</h1>
3.修改httpd.conf配置文件的监听地址
vim /etc/httpd/conf/httpd.conf //进入配置文件
...
Listen 192.168.235.129:80
Listen 192.168.235.129:8080 //找到Listen条目并添加8080端口监听项
[[email protected] accp02]# systemctl restart httpd //重启服务
4.使用客户机网页
三、基于IP的虚拟主机
1.首先添加第二块网卡,然后他会自动获取到一个IP地址
2.配置虚拟主机的配置文件
[[email protected] accp02]# cd /etc/httpd/conf/extra/
[[email protected] extra]# ls
vhost.conf
[[email protected] extra]# vim vhost.conf
1 <VirtualHost 192.168.235.129:80>
2 DocumentRoot "/var/www/html/accp/"
3 ServerName www.accp.com
4 ErrorLog "logs/www.accp.com.error_log"
5 CustomLog "logs/www.accp.com.access_1og" common
6 <Directory "/var/www/html/">
7 Require all granted
8 </Directory>
9 </VirtualHost>
10
11 <VirtualHost 192.168.235.142:80>
12 DocumentRoot "/var/www/html/accp02"
13 ServerName www.accp.com
14 ErrorLog "logs/www.accp02.com.error_log"
15 CustomLog "logs/www.accp02.com.access_log" common
16 <Directory "/var/www/html/">
17 Require all granted
18 </Directory>
19 </VirtualHost>
3.修改httpd.conf配置文件的监听地址
[[email protected] extra]# vim /etc/httpd/conf/httpd.conf
...
Listen 192.168.235.129:80
Listen 192.168.235.142:80 //添加另一个IP的监听
[[email protected] extra]# systemctl restart httpd //重启服务
4.使用客户机网页
原文地址:https://blog.51cto.com/14449521/2444901
时间: 2024-10-08 21:35:39