Apache 自身就支持虚拟域名。这是我唯一推荐使用内部虚拟域名机制的程序。当您通过 inetd 运行某些程序时,会产生开销,程序每次运行时都必须启动。这会导致响应时间变慢,对于大多数服务来说这完全可以接受,但对于 Web 服务来说是完全不可接受的。Apache 还有一个机制可以在连接过多时停止连接,这对于中等流量的站点也至关重要。
简而言之,使用 virtuald 虚拟化 Apache 是一个非常糟糕的主意。virtuald 的重点是填补服务*没有*自己的内部机制来完成这项工作时所产生的空白。Virtuald 并非旨在取代已经可以完成任务的优秀代码。
尽管有上述警告,但以下是如何为那些胆大妄为的人做到这一点的方法。
编辑 /etc/inetd.conf
vi /etc/inetd.conf # Add this line www stream tcp nowait www /usr/local/bin/virtuald \ virtuald /virtual/conf.www httpd -f /var/www/conf/httpd.conf
编辑 /var/www/conf/httpd.conf
vi /var/www/conf/httpd.conf # Or wherever you put the Apache config files It should say: ServerType standalone Replace it with: ServerType inetd
然后像往常一样为单域名使用配置 Apache 服务器的每个实例。
由于服务器是通过 inetd 运行的,因此不需要 httpd.init 文件。
Apache 有三个配置文件 access.conf
、 httpd.conf
和 srm.conf
。较新版本的 Apache 已经不再需要这三个配置文件。但是,我发现将配置分成三个部分可以更易于管理,因此我将在本 HOWTO 文档中保持这种风格。
此配置文件用于控制 Web 目录结构中目录的可访问性。这是一个示例配置文件,展示了如何为每个域设置不同的选项。
# /var/www/conf/access.conf: Global access configuration # Options are inherited from the parent directory # Set the main directory with default options <Directory /> AllowOverride None Options Indexes </Directory> # Give one domain a passwd protected directory <Directory /virtual/domain1.com/var/www/html/priv> AuthUserFile /var/www/passwd/domain1.com-priv AuthGroupFile /var/www/passwd/domain1.com-priv-g AuthName PRIVSECTION AuthType Basic <Limit GET PUT POST> require valid-user </Limit> </Directory> # Give another domain Server Side Includes <Directory /virtual/domain2.com/var/www/html> Options IncludesNOEXEC </Directory>
此配置文件用于控制 Apache 服务器的主要选项。这是一个示例配置文件,展示了如何为每个域设置不同的选项。
# /var/www/conf/httpd.conf: Main server configuration file # Begin: main conf section # Needed since not using inetd ServerType standalone # Port to run on Port 80 # Log clients with names vs IP addresses HostnameLookups on # User to run server as User www Group www # Where server config, error and log files are ServerRoot /var/www # Process Id of server in this file PidFile /var/run/httpd.pid # Internal server process info ScoreBoardFile /var/www/logs/apache_status # Timeout and KeepAlive options Timeout 400 KeepAlive 5 KeepAliveTimeout 15 # Number of servers to run MinSpareServers 5 MaxSpareServers 10 StartServers 5 MaxClients 150 MaxRequestsPerChild 30 # End: main conf section # Begin: virtual host section # Tell server to accept requests for ip:port # I have one for each IP needed so you can explicitly ignore certain domains Listen 10.10.10.129:80 Listen 10.10.10.130:80 # VirtualHost directive allows you to specify another virtual # domain on your server. Most Apache options can be specified # within this section. <VirtualHost www.domain1.com> # Mail to this address on errors ServerAdmin webmaster@domain1.com # Where documents are kept in the virtual domain DocumentRoot /virtual/domain1.com/var/www/html # Name of the server ServerName www.domain1.com # Log files Relative to ServerRoot option ErrorLog logs/domain1.com-error_log TransferLog logs/domain1.com-access_log RefererLog logs/domain1.com-referer_log AgentLog logs/domain1.com-agent_log # Use CGI scripts in this domain ScriptAlias /cgi-bin/ /var/www/cgi-bin/domain1.com/ AddHandler cgi-script .cgi AddHandler cgi-script .pl </VirtualHost> <VirtualHost www.domain2.com> # Mail to this address on errors ServerAdmin webmaster@domain2.com # Where documents are kept in the virtual domain DocumentRoot /virtual/domain2.com/var/www/html # Name of the server ServerName www.domain2.com # Log files Relative to ServerRoot option ErrorLog logs/domain2.com-error_log TransferLog logs/domain2.com-access_log RefererLog logs/domain2.com-referer_log AgentLog logs/domain2.com-agent_log # No CGI's for this host </VirtualHost> # End: virtual host section
此配置文件用于控制如何处理请求以及如何格式化结果。对于虚拟域,您无需在此处编辑任何内容。来自 Apache 的示例配置文件应该可以工作。
httpd.init 文件不需要进行任何特殊操作。使用 Apache 配置附带的标准文件即可。
这仅适用于独立风格的 Apache 服务器。通过 inetd 运行的服务器不与其他域交互,因此它拥有整个文件描述符表。
Apache 服务器打开的每个日志文件都是进程的另一个文件描述符。在 Linux 中,每个进程的文件描述符数量限制为 256 个。由于您有多个域,因此您使用的文件描述符更多。如果您在一个 Apache Web 服务器进程上运行太多域,则可能会溢出此表。这将意味着某些日志将无法工作,并且 CGI 将会失败。
如果您假设每个域五个文件描述符,您可以在 Apache 服务器上运行 50 个域而不会出现任何问题。但是,如果您发现您的服务器出现此类问题,您可以创建 /var/www1,其中一个 Apache 服务器负责 domain1 - domain25,并创建 /var/www2,其中一个 Apache 服务器负责 domain26 - domain50,依此类推。这将为每个服务器提供自己的配置、错误和日志目录。每个服务器都应使用其自己的 Listen 和 VirtualHost 指令单独配置。不要忘记在您的 httpd.init 文件中运行多个服务器。
HTTP(超文本传输协议)版本 1.1 添加了一项功能,可以将服务器名称传达给客户端。这意味着客户端不需要从其 IP 地址查找服务器。因此,两个虚拟服务器可以具有相同的 IP 地址,但却是不同的网站。Apache 配置与上述相同,只是您不必输入不同的 Listen 指令,因为这两个域将具有相同的 IP。
唯一的问题是 virtuald 使用 IP 地址来区分域。在其当前形式下,virtuald 将无法为每个域 chroot
到不同的假脱机目录。因此,邮件将只能作为一个 IP 响应,并且每个域将不再有唯一的假脱机目录。所有共享 IP 的 Web 客户端都必须共享该 IP 的假脱机目录。这意味着重复的用户名将再次成为问题。但是,这就是您为共享 IP 付出的代价。
本 HOWTO 仅展示了如何在 Apache Web 服务器上实现虚拟支持。大多数 Web 服务器都使用类似的接口。有关虚拟 Web 托管的更多信息,请查阅 WWW HOWTO、Apache 站点上的 Apache 文档或 ApacheWeek 上的文档。