Apache的虚拟主机配置

使用虚拟主机要先取消中心主机,注释掉DocumentRoot

#DocumentRoot "/www/htdoc"

虚拟主机的单独配置:

  • 用户认证
  • 访问日志
  • 错误日志
  • 别名
  • 脚本别名

一、基于IP的虚拟主机

一台服务器多个IP地址搭建多个网站

例如:

<VirtualHost 10.10.10.201:80>

    DocumentRoot /www/vhost1

    ServerName www.a.com

    ErrorLog logs/vhost1-err.log

    CustomLog logs/vhost1-acc.log combined

</VirtualHost>

<VirtualHost 10.10.10.202:80>

    DocumentRoot /www/vhost2

    ServerName www.b.com

    ErrorLog logs/vhost1-err.log

    CustomLog logs/vhost1-acc.log combined

</VirtualHost>

二、基于端口号的虚拟主机

一台服务器一个IP搭建多个网站,每个网站使用不同的端口号访问

Listen 8081

Listen 8082

<VirtualHost 10.10.10.201:8081>

    DocumentRoot /www/vhost1

    ServerName www.a.com

    ErrorLog logs/vhost1-err.log

    CustomLog logs/vhost1-acc.log combined

</VirtualHost>

<VirtualHost 10.10.10.201:8082>

    DocumentRoot /www/vhost2

    ServerName www.b.com

    ErrorLog logs/vhost1-err.log

    CustomLog logs/vhost1-acc.log combined

</VirtualHost>

三、基于域名(FQDN)的虚拟主机

一台服务器一个IP搭建多个网站每个网站使用不同的域名访问

在2.2中如果使用基于FQDN的方式要将NameVirtualHost *:80的注释去掉,并且NameVirtualHost的格式要和VirtualHost中的格式相同:

<VirtualHost *:80>

    DocumentRoot /www/vhost1

    ServerName www.a.com

    ErrorLog logs/vhost1-err.log

    CustomLog logs/vhost1-acc.log combined

</VirtualHost>

<VirtualHost *:80>

    DocumentRoot /www/vhost2

    ServerName www.b.com

    ErrorLog logs/vhost1-err.log

    CustomLog logs/vhost1-acc.log combined

</VirtualHost>

posted @ 2016-07-01 16:28  fansik  阅读(224)  评论(0编辑  收藏  举报