什么是虚拟主机?html
一个 http 服务要配置多个站点,就须要用到虚拟主机。linux
什么状况会用到虚拟主机?
apache
部署多个站点,每一个站点但愿用不一样的域名和站点目录,或者是不一样的端口,不一样的 IP。vim
虚拟主机的分类浏览器
1)基于域名app
2)基于端口ide
3)基于 IPspa
由于搭建基于端口,IP的虚拟主机的状况较少,因此本文只介绍的是搭建基于域名的虚拟主机3d
虚拟主机的规划htm
配置三个基于域名的虚拟主机,域名及站点目录以下
www.yuci.com /var/html/www
blog.yuci.com /var/html/blog
bbs.yuci.com /var/html/bbs
当前环境介绍
CentOS6.7x86_64,分别关闭了 iptables,selinux
uname -r
在搭建虚拟主机前,系统中须要有安装好的 Apache 环境
Apache环境搭建:http://aby028.blog.51cto.com/5371905/1896181
一键建立三个站点目录,并分别在其目录里建立首页文件
mkdir -p /var/html/{www,blog,bbs}
touch /var/html/{www,blog,bbs}/index.html
经过 tree 命令查看是否建立成功
tree /var/html
若是报错须要 yum 安装一下:yum -y install tree
经过 for 循环分别将对应的域名写入三个目录中的 index.html 文件
for name in www blog bbs;do echo "http://$name.yuci.com" > /var/html/$name/index.html;done
还能够经过 for 循环使用 cat 命令检查
for name in www blog bbs;do cat /var/html/$name/index.html;done
编辑虚拟主机的配置文件
vim /application/apache/conf/extra/httpd-vhosts.conf
编辑 apache 的主配置文件
vim /application/apache/conf/httpd.conf
在 httpd.conf 文件的最后加入如下字段
<Directory "/var/html">
AllowOverride None
Order allow,deny
Allow from all
</Directory>
检车语法没有报错后,平滑重启
/application/apache/bin/apachectl -t
/application/apache/bin/apachectl graceful
编辑本机的 hosts 文件
hosts 文件路径 C:\Windows\System32\drivers\etc
也能够经过 cmd 执行 drivers
编辑内容以下:地址在前,域名在后
当本机分别 ping 三个域名,检测是否能够 ping 通
ping www.yuci.com
ping blog.yuci.com
ping bbs.yuci.com
经过浏览器分别访问三个域名,都可以正常访问并正确看到内容