个人工具
登录
查看“Virtual hosting with Apache”的源代码 - Ubuntu中文
页面
讨论
查看源代码
历史
搜索
导航
首页
最近更改
随机页面
页面分类
帮助
编辑
编辑指南
沙盒
新闻动态
字词处理
工具
链入页面
相关更改
特殊页面
页面信息
查看“Virtual hosting with Apache”的源代码
来自Ubuntu中文
←
Virtual hosting with Apache
跳转至:
导航
,
搜索
因为以下原因,你没有权限编辑本页:
您所请求的操作仅限于该用户组的用户使用:
用户
您可以查看与复制此页面的源代码。
== 如何配置Apache 支援多个网域? == === 何谓Virtual Hosting? === Virtual Hosting就是同一台服务器可以同时处理超过一个网域(domain)。即是说,假设''www.example1.net''和 ''www.example2.net''两个网域都指向同一部计算机,如果计算机上的网页服务器(WWW Server)支援Virtual Hosting, 那你用''www.example1.net''和''www.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 套件 [http://packages.ubuntu.com/cgi-bin/search_packages.pl?searchon=names&subword=1&version=all&release=all&keywords=apache apache]: <pre><nowiki> # apt-get install apache </nowiki></pre> 在Apache配置档案 /etc/apache/httpd.conf 的尾部加入以下内容: <pre><nowiki> <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> </nowiki></pre> 记谨建立你在以上配置档中所提到的目录,例如 /var/www/www.examples.net 。 <pre><nowiki> # mkdir /var/www/www.examples.net </nowiki></pre> 检查你的配置档中是否有任何语法错误: <pre><nowiki> # apachectl configtest </nowiki></pre> 重新启动 Apache 。 <pre><nowiki> # /etc/init.d/apache reload </nowiki></pre> === Ubuntu 的 Apache2 (2.0.x) === 安装 Ubuntu 套件 [http://packages.ubuntu.com/cgi-bin/search_packages.pl?searchon=names&subword=1&version=all&release=all&keywords=apache2 apache2]: <pre><nowiki> # apt-get install apache2 </nowiki></pre> 在目录 /etc/apache2/sites-available 建立一个新档案(建议使用新站域名称,例如 ”www.examples.net“)存放有关网站的设定: <pre><nowiki> <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> </nowiki></pre> 使用 "a2ensite ''domainname''" 启动上面的配置档。例如: <pre><nowiki> # a2ensite www.examples.net </nowiki></pre> Apache 2.x 只会读取在目录 /etc/apache2/sites-enabled 下的配置档,所以你需要在这个目录下面建立一个连去刚才在/etc/apache2/sites-available 新建的档案。而 a2ensite(即是apache 2 enable site的缩写) 就是Debian帮你完成以上动作的小程序。除了a2ensite,你亦可以手动地打: <pre><nowiki> # ln -s /etc/apache2/sites-available/domainname /etc/apache2/sites-enabled/domainname</nowiki></pre> 建立有关连结。 记谨建立你在以上配置档中所提到的目录,例如 /var/www/www.examples.net 。 <pre><nowiki> # mkdir /var/www/www.examples.net </nowiki></pre> 检查你的配置档中是否有任何语法错误: <pre><nowiki> # apache2ctl configtest </nowiki></pre> 重新启动 Apache 。 <pre><nowiki> # /etc/init.d/apache2 reload </nowiki></pre> === Fedora Linux 或 Red Hat Linux 的 Apache === 确定在Apache配置档案/etc/httpd/conf/httpd.conf的"NameVirtualHost *"前的"#"已被删去: <pre><nowiki> NameVirtualHost * </nowiki></pre> 在/etc/httpd/conf/httpd.conf的尾部加入以下内容: <pre><nowiki> <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> </nowiki></pre> 记谨建立你在以上配置档中所提到的目录,例如 /var/www/www.examples.net 。 <pre><nowiki> # mkdir /var/www/www.examples.net </nowiki></pre> 检查你的配置档中是否有任何语法错误: <pre><nowiki> # apachectl configtest </nowiki></pre> 重新启动 Apache 。 <pre><nowiki> # /etc/init.d/httpd reload </nowiki></pre> === Mandriva Linux 的 Apache 2.0.x === 安装 Mandriva 套件 apache-mpm-prefork: <pre><nowiki> # urpmi apache-mpm-prefork </nowiki></pre> 确定在Apache配置档案/etc/httpd/conf/httpd.conf的"NameVirtualHost *:80"及"Setenv VLOG"前的 "#"已被删去: <pre><nowiki> NameVirtualHost *:80 Setenv VLOG </nowiki></pre> 在目录 /etc/httpd/conf/vhosts.d 建立一个新档案(建议使用新网站名称,例如 ”www.examples.net“)存放有关网站的设定: <pre><nowiki> <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> </nowiki></pre> 记谨建立你在以上配置档中所提到的目录,例如 /var/www/www.examples.net 。 <pre><nowiki> # mkdir /var/www/www.examples.net </nowiki></pre> 重新启动 Apache 。 <pre><nowiki> # /etc/init.d/httpd reload </nowiki></pre> === 配置选项详解 === ==== IP Alias ==== 如果你需用使用IP-based virtual hosting,你先需要利用IP Alias令你的GNU/Linux同时拥有超过一个IP地站,做法是以 root身份键入指令"ifconfig ''接口'':''编号'' ''IP地址''",例如: <pre><nowiki> # ifconfig eth0:0 192.168.0.101 </nowiki></pre> ==== NameVirtualHost ==== [http://httpd.apache.org/docs/2.0/mod/core.html#namevirtualhost NameVirtualHost]选项的作用是告诉Apache那个IP地址所指的网络接口入来的要求需要用Host-based Virtual Hosting。即是当有由NameVirtualHost标示过网络接口的要求入来时,Apache 会检查要求标头是否有"Host:"项目(标示了要访问的网站名称),并根据要访问网站名称决定采用那一组<VirtualHost>...</VirtualHost>的设定。NameVirutalHost的语法是: NameVirtualHost''IP地址''[[''端口'']]例如: <pre><nowiki> NameVirutalHost 192.168.0.1 <VirtualHost 192.168.0.1> # ... </VirtualHost> </nowiki></pre> 如果你要有多个网络接口需要Name-based设定,请重复多个NameVirtualHost选项标示所有网络接口对应的IP地址。如果你想计 算机所有网络接口进来的要求都而要Name-based Virtual Hosting,可以使用星号 "*" 代替IP地址: <pre><nowiki> NameVirutalHost * </nowiki></pre> 要标示端口,可以: <pre><nowiki> NameVirtualHost *:8080 </nowiki></pre> ==== <VirtualHost> ==== [http://httpd.apache.org/docs/2.0/mod/core.html#virtualhost <VirtualHost>] 用作包着每个虚拟网站的设定。其语法为: <VirtualHost''地址''[[''端口'']]''地址''[[''端口'']] ...> ... </VirtualHost>地址可以以下几种 * '''虚拟网站的IP地址''' * '''对应IP地址的Full Qualified Domain Name''' * '''*''' - 代表任何IP地址 * '''_default_''' - 适用于未符合所有其他IP地址的设定 例如: <pre><nowiki> <VirtualHost 10.1.2.3> 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 </VirtualHost> </nowiki></pre> ==== ServerName ==== ==== ServerAlias ==== ==== ServerAdmin ==== ==== DocumentRoot ==== === 相关文章 === * [http://httpd.apache.org/docs/2.0/vhosts/ Apache Virtual Host documentation] === 来源 === * [http://wiki.debian.org.hk/w/Virtual_hosting_with_Apache 如何配置Apache 支援多个网域?]
返回至
Virtual hosting with Apache
。