kaywang 发表于 2018-11-19 06:21:36

Apache部署虚拟主机

  什么是虚拟主机?
  一个 http 服务要配置多个站点,就需要用到虚拟主机。
  什么情况会用到虚拟主机?

  部署多个站点,每个站点希望用不同的域名和站点目录,或者是不同的端口,不同的 IP。
  虚拟主机的分类
  1)基于域名
  2)基于端口
  3)基于 IP
  因为搭建基于端口,IP的虚拟主机的情况较少,所以本文只介绍的是搭建基于域名的虚拟主机
  
  虚拟主机的规划
  配置三个基于域名的虚拟主机,域名及站点目录如下
  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 文件的最后加入以下字段
  
           AllowOverride None
           Order allow,deny
           Allow from all
  
  
  
  

  检车语法没有报错后,平滑重启
  /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

  通过浏览器分别访问三个域名,均可以正常访问并正确看到内容



页: [1]
查看完整版本: Apache部署虚拟主机