Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1820786
  • 博文数量: 473
  • 博客积分: 13997
  • 博客等级: 上将
  • 技术积分: 5953
  • 用 户 组: 普通用户
  • 注册时间: 2010-01-22 11:52
文章分类

全部博文(473)

文章存档

2014年(8)

2013年(38)

2012年(95)

2011年(181)

2010年(151)

分类: LINUX

2011-02-28 14:36:00

何谓 Virtual Hosting?

Virtual Hosting 就是同一台服务器可以同时处理超过一个网域(domain)。即是说,假设 两个网域都指向同一部计算机,如果计算机上的网页服务器 (WWW Server) 支援 Virtual Hosting,那您用 去访问同一个服务器就可以取得不同的网站。

[]

IP-based 和 Host-based Virtual Hosting

本身 Virtual Hosting 技术分 IP-based 和 Host-based。IP-based Virtual Hosting 是指运行网站服务器的计算机有多个 IP 地址(IP Address),访问不同的 IP 地址就可以取得不同的网站。Host-based Virtual Hosting 则是指在 DNS 上多个网域都是指向同一个 IP 地址,网页浏览器(WWW Browser)透过 HTTP 协定告知网页服务器要访问那个网站。

一来 IP-based Virtual Hosting 需要多 IP 地址,即每个网站都要多点成本,二来 Host-based Virtual Hosting 技术已有十年多,现存所有网页浏览器和网页服务器都有支援,所以现时很少人会采用 IP-based Virtual Hosting。不过因为 TLS/SSL 技术限制了每一个 IP 地址只可以支援一张电子证书(Digital Certificate),所以如果您要在同一部计算机使用多过一张电子证书,您仍然需要 IP-based Virtual Hosting。

当然您亦可以将 IP-based 及 Host-based Virtual Hosting 混合使用于同一台 Apache 网页服务器中。

[]

Debian GNU/Linux 的 Apache 1.3.x

安装 Debian 套件 :

# apt-get install apache

在 Apache 配置档案 /etc/apache/httpd.conf 的尾部加入以下内容:


    # 在 ServerName 后加上您的网站名称
    ServerName 
    # 如果您想多个网站名称都取得相同的网站,可以加在 ServerAlias 后加上其他网站别名。
    # 别名间以空格间开。
    ServerAlias web.example.net mail.example.net *.example.org
    # 在 ServerAdmin 后加上网站管理员的电邮地址,方便别人有问题是可以联络网站管理员。
    ServerAdmin webmaster@example.net

    # 在 DocumentRoot 后加上存放网站内容的目录路径
    DocumentRoot /var/www/
    /var/www/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride None
        Order allow,deny
        allow from all
    

    ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
    
        AllowOverride None
        Options ExecCGI -MultiViews +SymLinksIfOwnerMatch
        Allow from all
    

    ErrorLog /var/log/apache/error.log

    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn

    CustomLog /var/log/apache/access.log combined

记谨建立您在以上配置档中所提到的目录,例如 /var/www/ 。

# mkdir /var/www/

检查您的配置档中是否有任何语法错误:

# apachectl configtest

重新启动 Apache 。

# /etc/init.d/apache reload
[]

Debian GNU/Linux 的 Apache2 (2.0.x)

安装 Debian 套件 :

# apt-get install apache2

在目录 /etc/apache2/sites-available 建立一个新档案(建议使用新站域名称,例如 ”“)存放有关网站的设定:


    # 在 ServerName 后加上您的网站名称
    ServerName 
    # 如果您想多个网站名称都取得相同的网站,可以加在 ServerAlias 后加上其他网站别名。
    # 别名间以空格间开。
    ServerAlias web.examples.net mail.examples.net
    # 在 ServerAdmin 后加上网站管理员的电邮地址,方便别人有问题是可以联络网站管理员。
    ServerAdmin webmaster@examples.net

    # 在 DocumentRoot 后加上存放网站内容的目录路径
    DocumentRoot /var/www/
    /var/www/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride None
        Order allow,deny
        allow from all
    

    ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
    
        AllowOverride None
        Options ExecCGI -MultiViews +SymLinksIfOwnerMatch
        Allow from all
    

    ErrorLog /var/log/apache2/error.log

    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn

    CustomLog /var/log/apache2/access.log combined
    ServerSignature On

使用 "a2ensite domainname" 启动上面的配置档。例如:

# a2ensite 

记谨建立您在以上配置档中所提到的目录,例如 /var/www/ 。

# mkdir /var/www/

检查您的配置档中是否有任何语法错误:

# apache2ctl configtest

重新启动 Apache 。

# /etc/init.d/apache2 reload
[]

Fedora Linux 或 Red Hat Linux 的 Apache

确定在 Apache 配置档案 /etc/httpd/conf/httpd.conf 的"NameVirtualHost *"前的 "#" 已被删去:

NameVirtualHost *:80

在 /etc/httpd/conf/httpd.conf 的尾部加入以下内容:


    # 在 ServerName 后加上您的网站名称
    ServerName 
    # 如果您想多个网站名称都取得相同的网站,可以加在 ServerAlias 后加上其他网站别名。
    # 别名间以空格间开。
    ServerAlias web.examples.net mail.examples.net
    # 在 ServerAdmin 后加上网站管理员的电邮地址,方便别人有问题是可以联络网站管理员。
    ServerAdmin webmaster@examples.net

    # 在 DocumentRoot 后加上存放网站内容的目录路径
    DocumentRoot /var/www/
    /var/www/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride None
        Order allow,deny
        allow from all
    

    ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
    
        AllowOverride None
        Options ExecCGI -MultiViews +SymLinksIfOwnerMatch
        Allow from all
    

    ErrorLog /var/log/httpd/error.log

    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn

    CustomLog /var/log/httpd/access.log combined
    ServerSignature On

记谨建立您在以上配置档中所提到的目录,例如 /var/www/ 。

# mkdir /var/www/

检查您的配置档中是否有任何语法错误:

# apachectl configtest

重新启动 Apache 。

# /etc/init.d/httpd reload
[]

Mandriva Linux 的 Apache 2.0.x

安装 Mandriva 套件 apache-mpm-prefork:

# urpmi apache-mpm-prefork

确定在 Apache 配置档案 /etc/httpd/conf/httpd.conf的 "NameVirtualHost *:80" 及 "Setenv VLOG" 前的 "#" 已被删去:

NameVirtualHost *:80
Setenv VLOG

在目录 /etc/httpd/conf/vhosts.d 建立一个新档案 (建议使用新网站名称,例如 ”“) 存放有关网站的设定:


    # 在 ServerName 后加上您的网站名称
    ServerName 
    # 如果您想多个网站名称都取得相同的网站,可以加在 ServerAlias 后加上其他网站别名。
    # 别名间以空格间开。
    ServerAlias web.examples.net mail.examples.net
    # 在 ServerAdmin 后加上网站管理员的电邮地址,方便别人有问题是可以联络网站管理员。
    ServerAdmin webmaster@examples.net

    # 在 DocumentRoot 后加上存放网站内容的目录路径
    DocumentRoot /var/www/
    /var/www/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride None
        Order allow,deny
        allow from all
    

    ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
    
        AllowOverride None
        Options ExecCGI -MultiViews +SymLinksIfOwnerMatch
        Allow from all
    

    ErrorLog /var/log/httpd/error.log

    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn

    CustomLog /var/log/httpd/access.log combined
    ServerSignature On

记谨建立您在以上配置档中所提到的目录,例如 /var/www/ 。

# mkdir /var/www/

重新启动 Apache 。

# /etc/init.d/httpd reload
[]

配置选项详解

[]

IP Alias

如果您需要使用 IP-based virtual hosting,您先需要利用 IP Alias 令您的 GNU/Linux 同时拥有超过一个IP地站,做法是以 root 身份键入指令 "ifconfig 接口:编号 IP地址",例如:

# ifconfig eth0:0 192.168.0.101
[]

NameVirtualHost

选项的作用是告诉 Apache 那个 IP 地址所指的网络接口入来的要求需要用 Host-based Virtual Hosting。即是当有由 NameVirtualHost 标示过网络接口的要求入来时,Apache 会检查要求标头是否有 "Host:" 项目(标示了要访问的网站名称),并根据要访问网站名称决定采用那一组 ... 的设定。NameVirutalHost 的语法是:


NameVirtualHost IP地址[:端口]

例如:

NameVirutalHost 192.168.0.1

# ...

如果您要有多个网络接口需要 Name-based 设定,请重复多个 NameVirtualHost 选项标示所有网络接口对应的IP地址。如果您想计算机所有网络接口进来的要求都而要 Name-based Virtual Hosting,可以使用星号 "*" 代替IP地址:

NameVirtualHost *

要标示端口,可以:

NameVirtualHost *:8080
[]

用作包着每个虚拟网站的设定。其语法为:


地址[:端口] 地址[:端口] ...> ...

地址可以以下几种

  • 虚拟网站的 IP 地址
  • 对应 IP 地址的 Full Qualified Domain Name
  • * - 代表任何 IP 地址
  • _default_ - 适用于未符合所有其他IP地址的设定

例如:


 ServerAdmin webmaster@host.example.org
 DocumentRoot /www/docs/host.example.org
 ServerName host.example.net
 ErrorLog logs/host.example.net-error_log
 TransferLog logs/host.example.net-access_log

[]


阅读(781) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2011-03-05 13:44:32

很好的, 收藏了 推荐一个博客,提供很多免费软件编程电子书下载: http://free-ebooks.appspot.com