配置Apache支持多个网站(详细教程)
Virtual Hosting 就是同一台服务器可以同时处理超过一个网域(domain)。即是说,假设www.example1.net 和 www.example2.net 两个网域都指向同一部计算机,如果计算机上的网页服务器 (WWW Server) 支援 Virtual Hosting,那您用 www.example1.net 和 www.example2.net去访问同一个服务器就可以取得不同的网站。
本身 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 套件 apache:
# apt-get install apache
在 Apache 配置档案 /etc/apache/httpd.conf 的尾部加入以下内容:
<VirtualHost *> # 在 ServerName 后加上您的网站名称 ServerName www.example.net # 如果您想多个网站名称都取得相同的网站,可以加在 ServerAlias 后加上其他网站别名。 # 别名间以空格间开。 ServerAlias web.example.net mail.example.net *.example.org # 在 ServerAdmin 后加上网站管理员的电邮地址,方便别人有问题是可以联络网站管理员。 ServerAdmin webmaster@example.net # 在 DocumentRoot 后加上存放网站内容的目录路径 DocumentRoot /var/www/www.examples.net <Directory /var/www/www.examples.net> 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
安装 Debian 套件 apache2:
# apt-get install apache2
在目录 /etc/apache2/sites-available 建立一个新档案(建议使用新站域名称,例如 ”www.examples.net“)存放有关网站的设定:
<VirtualHost *> # 在 ServerName 后加上您的网站名称 ServerName www.examples.net # 如果您想多个网站名称都取得相同的网站,可以加在 ServerAlias 后加上其他网站别名。 # 别名间以空格间开。 ServerAlias web.examples.net mail.examples.net # 在 ServerAdmin 后加上网站管理员的电邮地址,方便别人有问题是可以联络网站管理员。 ServerAdmin webmaster@examples.net # 在 DocumentRoot 后加上存放网站内容的目录路径 DocumentRoot /var/www/www.examples.net <Directory /var/www/www.examples.net> 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
确定在 Apache 配置档案 /etc/httpd/conf/httpd.conf 的"NameVirtualHost *
"前的 "#
" 已被删去:
NameVirtualHost *:80
在 /etc/httpd/conf/httpd.conf 的尾部加入以下内容:
<VirtualHost *> # 在 ServerName 后加上您的网站名称 ServerName www.examples.net # 如果您想多个网站名称都取得相同的网站,可以加在 ServerAlias 后加上其他网站别名。 # 别名间以空格间开。 ServerAlias web.examples.net mail.examples.net # 在 ServerAdmin 后加上网站管理员的电邮地址,方便别人有问题是可以联络网站管理员。 ServerAdmin webmaster@examples.net # 在 DocumentRoot 后加上存放网站内容的目录路径 DocumentRoot /var/www/www.examples.net <Directory /var/www/www.examples.net> 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 套件 apache-mpm-prefork:
# urpmi apache-mpm-prefork
确定在 Apache 配置档案 /etc/httpd/conf/httpd.conf的 "NameVirtualHost *:80
" 及 "Setenv VLOG
" 前的 "#
" 已被删去:
NameVirtualHost *:80Setenv VLOG
在目录 /etc/httpd/conf/vhosts.d 建立一个新档案 (建议使用新网站名称,例如 ”www.examples.net“) 存放有关网站的设定:
<VirtualHost *> # 在 ServerName 后加上您的网站名称 ServerName www.examples.net # 如果您想多个网站名称都取得相同的网站,可以加在 ServerAlias 后加上其他网站别名。 # 别名间以空格间开。 ServerAlias web.examples.net mail.examples.net # 在 ServerAdmin 后加上网站管理员的电邮地址,方便别人有问题是可以联络网站管理员。 ServerAdmin webmaster@examples.net # 在 DocumentRoot 后加上存放网站内容的目录路径 DocumentRoot /var/www/www.examples.net <Directory /var/www/www.examples.net> 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-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:" 项目(标示了要访问的网站名称),并根据要访问网站名称决定采用那一组 <VirtualHost>...</VirtualHost> 的设定。NameVirutalHost 的语法是:
例如:
NameVirutalHost 192.168.0.1<VirtualHost 192.168.0.1># ...</VirtualHost>
如果您要有多个网络接口需要 Name-based 设定,请重复多个 NameVirtualHost 选项标示所有网络接口对应的IP地址。如果您想计算机所有网络接口进来的要求都而要 Name-based Virtual Hosting,可以使用星号 "*" 代替IP地址:
NameVirtualHost *
要标示端口,可以:
NameVirtualHost *:8080
<VirtualHost> 用作包着每个虚拟网站的设定。其语法为:
地址可以以下几种
例如:
<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>