个人工具

Virtual hosting with Apache

来自Ubuntu中文

跳转至: 导航, 搜索

如何配置Apache 支援多个网域?

何谓Virtual Hosting?

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

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网页服务器中。

Ubuntu 的 Apache 1.3.x

安装 Ubuntu 套件 apache

# apt-get install apache 

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

<VirtualHost *>
# 在ServerName后加上你的网站名称
ServerName
# 如果你想多个网站名称都取得相同的网站,可以加在ServerAlias后加上其他网站别名。
# 别名间以空格间开。
ServerAlias
# 在ServerAdmin后加上网站管理员的电邮地址,方便别人有问题是可以联络网站管理员。
ServerAdmin
# 在DocumentRoot后加上存放网站内容的目录路径
DocumentRoot
<Directory >
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options ExecCGI -MultiViews +SymLinksIfOwnerMatch
Allow from all
</Directory>
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
</VirtualHost>

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

# mkdir /var/www/www.examples.net 

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

# apachectl configtest 

重新启动 Apache 。

# /etc/init.d/apache reload 

Ubuntu 的 Apache2 (2.0.x)

安装 Ubuntu 套件 apache2

# apt-get install apache2 

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

<VirtualHost *>
# 在ServerName后加上你的网站名称
ServerName
# 如果你想多个网站名称都取得相同的网站,可以加在ServerAlias后加上其他网站别名。
# 别名间以空格间开。
ServerAlias
# 在ServerAdmin后加上网站管理员的电邮地址,方便别人有问题是可以联络网站管理员。
ServerAdmin
# 在DocumentRoot后加上存放网站内容的目录路径
DocumentRoot
<Directory >
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options ExecCGI -MultiViews +SymLinksIfOwnerMatch
Allow from all
</Directory>
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
</VirtualHost>

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

# a2ensite www.examples.net 

Apache 2.x 只会读取在目录 /etc/apache2/sites-enabled 下的配置档,所以你需要在这个目录下面建立一个连去刚才在/etc/apache2/sites-available 新建的档案。而 a2ensite(即是apache 2 enable site的缩写) 就是Debian帮你完成以上动作的小程序。除了a2ensite,你亦可以手动地打:

# ln -s /etc/apache2/sites-available/domainname /etc/apache2/sites-enabled/domainname

建立有关连结。 记谨建立你在以上配置档中所提到的目录,例如 /var/www/www.examples.net 。

# mkdir /var/www/www.examples.net 

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

# apache2ctl configtest 

重新启动 Apache 。

# /etc/init.d/apache2 reload 

Fedora Linux 或 Red Hat Linux 的 Apache

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

NameVirtualHost *

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

<VirtualHost *>
# 在ServerName后加上你的网站名称
ServerName
# 如果你想多个网站名称都取得相同的网站,可以加在ServerAlias后加上其他网站别名。
# 别名间以空格间开。
ServerAlias
# 在ServerAdmin后加上网站管理员的电邮地址,方便别人有问题是可以联络网站管理员。
ServerAdmin
# 在DocumentRoot后加上存放网站内容的目录路径
DocumentRoot
<Directory >
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options ExecCGI -MultiViews +SymLinksIfOwnerMatch
Allow from all
</Directory>
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
</VirtualHost>

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

# mkdir /var/www/www.examples.net 

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

# 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 建立一个新档案(建议使用新网站名称,例如 ”www.examples.net“)存放有关网站的设定:

<VirtualHost *>
# 在ServerName后加上你的网站名称
ServerName
# 如果你想多个网站名称都取得相同的网站,可以加在ServerAlias后加上其他网站别名。
# 别名间以空格间开。
ServerAlias
# 在ServerAdmin后加上网站管理员的电邮地址,方便别人有问题是可以联络网站管理员。
ServerAdmin
# 在DocumentRoot后加上存放网站内容的目录路径
DocumentRoot
<Directory >
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options ExecCGI -MultiViews +SymLinksIfOwnerMatch
Allow from all
</Directory>
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
</VirtualHost>

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

# mkdir /var/www/www.examples.net 

重新启动 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

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

NameVirtualHostIP地址''端口''例如:

NameVirutalHost 192.168.0.1
<VirtualHost 192.168.0.1>
# ...
</VirtualHost>

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

NameVirutalHost *

要标示端口,可以:

NameVirtualHost *:8080

<VirtualHost>

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

<VirtualHost地址''端口''地址''端口'' ...> ... </VirtualHost>地址可以以下几种

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

例如:

<VirtualHost 10.1.2.3>
ServerAdmin [email protected]
DocumentRoot /www/docs/host.example.org
ServerName host.example.net
ErrorLog logs/host.example.net-error_log
TransferLog logs/host.example.net-access_log
</VirtualHost>

ServerName

ServerAlias

ServerAdmin

DocumentRoot

相关文章

来源