标签:根目录 ror 访问 关联 构建 基于 inf mkdir 需要
虚拟 web 主机指的是在同一台服务器中运行多个 web 站点,其中的每个站点实际上并不独立占用整个服务器,因此被称为“虚拟” web主机。通过虚拟 web 主机可以充分利用服务器的硬件资源,从而大大降低网站构建及运行成本。[root@www /]# vim /etc/named.conf
…… //
zone "test1.com" in {
type master;
file "test1.com.zone";
};
zone "test2.com" in {
type master;
file "test2.com.zone";
};
[root@www /]# vim /var/named/test1.com.zone
…… //
in ns www.test1.com.
www in a 192.168.1.10
[root@www /]# vim /var/named/test2.com.zone
…… //
in ns www.test2.com.
www in a 192.168.1.10
为虚拟主机准备网页文档:
[root@www /]# mkdir -p /var/www/html/test1com
[root@www /]# mkdir -p /var/www/html/test2com
[root@www /]# echo "<h1>www.test1.com</h1>" > /var/www/html/test1com/index.html
[root@www /]# echo "<h1>www.test2.com</h1>" > /var/www/html/test2com/index.html
添加虚拟主机配置:
[root@www /]# vim /usr/local/httpd/conf/extra/httpd-vhosts.conf
配置 test1 的虚拟站点区域:
…… // 省略部分内容
<VirtualHost *:80> # 配置监听地址和端口 “*” 代表任意地址
ServerAdmin webmaster@test.com # 设置管理员的邮箱 可删除
DocumentRoot "/var/www/html/test1com" # 指定网站根目录
ServerName www.test1.com # 配置域名
ServerAlias www.dummy-host.example.com # 配置别名
ErrorLog "logs/www.test1.com-error_log" # 记录错误日志
CustomLog "logs/www.test1.com-access_log" common # 记录访问日志
<Directory "/var/www/html"> # 设置目录访问权限
Require all granted # 允许所有
</Directory>
</VirtualHost>
配置 test2 的虚拟站点区域:
<VirtualHost *:80>
ServerAdmin webmaster@test.com
DocumentRoot "/var/www/html/test2com"
ServerName www.test2.com
ServerAlias www.dummy-host.example.com
ErrorLog "logs/www.test2.com-error_log"
CustomLog "logs/www.test2.com-access_log" common
<Directory "/var/www/html">
Require all granted
</Directory>
</VirtualHost>
[root@www /]# vim /usr/local/httpd/conf/httpd.conf # 进入主配置文件
……
Include conf/extra/httpd-vhosts.conf # 找到本行后将开头 # 去掉
[root@www /]# systemctl restart httpd 重启服务使配置生效
客户机访问 web 验证:
二、基于 IP 地址的虚拟主机:
每一种方式之间毫无任何关联,千万别把基于IP地址的虚拟主机和基于域名的搞混了
[root@www /]# vim /usr/local/httpd/conf/extra/httpd-vhosts.conf
……
<VirtualHost 192.168.1.10:80> # 配置监听地址为 192.168.1.10
ServerAdmin webmaster@test.com
DocumentRoot "/var/www/html/test1com"
ServerName www.test1.com
ServerAlias www.dummy-host.example.com
ErrorLog "logs/www.test1.com-error_log"
CustomLog "logs/www.test1.com-access_log" common
<Directory "/var/www/html">
Require all granted
</Directory>
</VirtualHost>
<VirtualHost 192.168.1.20:80> # 配置监听地址为 192.168.1.20
ServerAdmin webmaster@test.com
DocumentRoot "/var/www/html/test2com"
ServerName www.test2.com
ServerAlias www.dummy-host.example.com
ErrorLog "logs/www.test2.com-error_log"
CustomLog "logs/www.test2.com-access_log" common
<Directory "/var/www/html">
Require all granted
</Directory>
</VirtualHost>
三、基于端口的虚拟主机:
[root@www /]# vim /usr/local/httpd/conf/extra/httpd-vhosts.conf
……
<VirtualHost 192.168.1.10:80>
ServerAdmin webmaster@test.com
DocumentRoot "/var/www/html/test1com"
ServerName www.test1.com
ServerAlias www.dummy-host.example.com
ErrorLog "logs/www.test1.com-error_log"
CustomLog "logs/www.test1.com-access_log" common
<Directory "/var/www/html">
Require all granted
</Directory>
</VirtualHost>
<VirtualHost 192.168.1.10:8080> # 修改端口号
ServerAdmin webmaster@test.com
DocumentRoot "/var/www/html/test2com"
ServerName www.test2.com
ServerAlias www.dummy-host.example.com
ErrorLog "logs/www.test2.com-error_log"
CustomLog "logs/www.test2.com-access_log" common
<Directory "/var/www/html">
Require all granted
</Directory>
</VirtualHost>
listen 80 # 监听80端口(默认80也可不用写这一行)
listen 8080 # 监听8080端口
注意:每次配置完毕后都需要重启服务
标签:根目录 ror 访问 关联 构建 基于 inf mkdir 需要
原文地址:https://blog.51cto.com/14227204/2424738