基于Apache名称的VirtualHost示例

时间:2020-01-09 10:37:10  来源:igfitidea点击:

问能否在CentOS/RHEL/Fedora Linux下为基于Apache名称的VirtualHost功能提供示例或指定示例?
答:虚拟主机一词是指在一台Apache服务器上维护多个网站的做法。

如果要配置基于名称的虚拟主机,则必须使用NameVirtualHost指令。

使用NameVirtualHost指令,您可以指定服务器将在其上接收基于名称的虚拟主机请求的IP地址。
这通常是基于名称的虚拟主机名解析到的地址。

基于Apache 2名称的virtualhost配置

例如,如果您的IP地址是203.54.2.5。
在httpd.conf文件中进行如下设置:

# vi /etc/httpd/conf/httpd.conf

如下设置NameVirtualHost:

NameVirtualHost 203.54.2.5:80

让我们看看您的虚拟主机条目如何查找名为theos.in和theitroad.com的两个域。

theos.in域中的代码:

<VirtualHost theos.in/>
ServerAdmin [email protected]
DocumentRoot /var/www/theos.in
ServerName theos.in
ServerAlias www.theos.in
ErrorLog /var/logs/httpd/theos.in/error_log
CustomLog /var/logs/httpd/theos.in/access_log common
</VirtualHost>

theitroad.com域的代码:

<VirtualHost theitroad.com/>
ServerAdmin [email protected]
DocumentRoot /var/www/theitroad.com
ServerName theitroad.com
ServerAlias www.theitroad.com
ErrorLog /var/logs/httpd/theitroad.com/error_log
CustomLog /var/logs/httpd/theitroad.com/access_log common
</VirtualHost>

保存并关闭文件。
重新启动Apache:

# /etc/init.d/httpd restart

确保两个域名theos.in和theitroad.com都指向(A地址)203.54.2.5。
还要确保您创建了/var/logs/httpd/domain-name目录,并将ftp用户目录指向相应的/var/www/domain-name目录。

虚拟主机和IP地址

IPv4的IP地址集有限。
如果要在计算机上维护多个域/主机名,则可以为其设置VirtualHost容器。
大多数配置仅使用基于名称的虚拟主机,因此服务器无需担心IP地址,直接在配置文件中使用星号,如下所示:

NameVirtualHost *:80
 
<VirtualHost *:80>
    ServerAdmin [email protected]
    DocumentRoot "/usr/local/docs/dummy-host.example.com"
    ServerName dummy-host.example.com
    ServerAlias www.dummy-host.example.com
    ErrorLog "/var/log/dummy-host.example.com-error_log"
    CustomLog "/var/log/dummy-host.example.com-access_log" common
</VirtualHost>
 
<VirtualHost *:80>
    ServerAdmin [email protected]
    DocumentRoot "/usr/local/docs/dummy-host2.example.com"
    ServerName dummy-host2.example.com
    ErrorLog "/var/log/dummy-host2.example.com-error_log"
    CustomLog "/var/log/dummy-host2.example.com-access_log" common
</VirtualHost>