本文重点讲解基于IP 和多端口的虚拟主机配置;单个IP地址的服务器上基于域名的虚拟主机配置;在多个IP 地址的服务器上配置基于域名的虚拟主机;在不同的端口上运行不同的站点(基于多端口的服务器上配置基于域名的虚拟主机);基于域名和基于IP 的混合虚拟主机的配置;基于IP地址的虚拟主机配置方法
很多朋友在初次使用apache虚拟主机时,都会觉得apache虚拟主机特别难配置,其实不然,只要掌握虚拟主机的配置原则,你就会发现要配置好一台虚拟主机其实很简单,话不多说直接看如何配置?
基于IP 和多端口的虚拟主机配置
Listen 172.20.30.40:80
Listen 172.20.30.40:8080
Listen 172.20.30.50:80
Listen 172.20.30.50:8080
<VirtualHost 172.20.30.40:80>
DocumentRoot /www/example1-80
ServerName www.example1.com
</VirtualHost>
<VirtualHost 172.20.30.40:8080>
DocumentRoot /www/example1-8080
ServerName www.example1.com
</VirtualHost>
<VirtualHost 172.20.30.50:80>
DocumentRoot /www/example2-80
ServerName www.example1.org
</VirtualHost>
<VirtualHost 172.20.30.50:8080>
DocumentRoot /www/example2-8080
ServerName www.example2.org
</VirtualHost>
单个IP地址的服务器上基于域名的虚拟主机配置
# Ensure that Apache listens on port 80
Listen 80
# Listen for virtual host requests on all IP addresses
NameVirtualHost *:80
<VirtualHost *:80>
DocumentRoot /www/example1
ServerName www.example1.com
ServerAlias example1.com. *.example1.com
# Other directives here
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /www/example2
ServerName www.example2.org
# Other directives here
</VirtualHost>
在多个IP 地址的服务器上配置基于域名的虚拟主机
Listen 80
# This is the "main" server running on 172.20.30.40
ServerName server.domain.com
DocumentRoot /www/mainserver
# This is the other address
NameVirtualHost 172.20.30.50
<VirtualHost 172.20.30.50>
DocumentRoot /www/example1
ServerName www.example1.com
# Other directives here ...
</VirtualHost>
<VirtualHost 172.20.30.50>
DocumentRoot /www/example2
ServerName www.example2.org
# Other directives here ...
</VirtualHost>
在不同的端口上运行不同的站点(基于多端口的服务器上配置基于域名的虚拟主机)
Listen 80
Listen 8080
NameVirtualHost 172.20.30.40:80
NameVirtualHost 172.20.30.40:8080
<VirtualHost 172.20.30.40:80>
ServerName www.example1.com
DocumentRoot /www/domain-80
</VirtualHost>
<VirtualHost 172.20.30.40:8080>
ServerName www.example1.com
DocumentRoot /www/domain-8080
</VirtualHost>
<VirtualHost 172.20.30.40:80>
ServerName www.example2.org
DocumentRoot /www/otherdomain-80
</VirtualHost>
<VirtualHost 172.20.30.40:8080>
ServerName www.example2.org
DocumentRoot /www/otherdomain-8080
</VirtualHost>
基于域名和基于IP 的混合虚拟主机的配置
Listen 80
NameVirtualHost 172.20.30.40
<VirtualHost 172.20.30.40>
DocumentRoot /www/example1
ServerName www.example1.com
</VirtualHost>
<VirtualHost 172.20.30.40>
DocumentRoot /www/example2
ServerName www.example2.org
</VirtualHost>
<VirtualHost 172.20.30.40>
DocumentRoot /www/example3
ServerName www.example3.net
</VirtualHost>
基于IP地址的虚拟主机
Listen 80
<VirtualHost 172.20.30.40>
DocumentRoot /home/httpd/html1
ServerName www.ok1.com
ErrorLog /usr/local/apache/logs/error1_log
CustomLog /usr/local/apache/logs/access1_log combined
</VirtualHost>
<VirtualHost 172.20.30.50>
DocumentRoot /home/httpd/html2
ServerName www.ok2.com
ErrorLog /usr/local/apache/logs/error2_log
CustomLog /usr/local/apache/logs/access2_log combined
</VirtualHost>
通过阅读这篇文章你是否掌握了apache虚拟主机配置方法?其实要配置好一台虚拟主机没有想象中那么难。
河南亿恩科技股份有限公司(www.enkj.com)始创于2000年,专注服务器托管租用,是国家工信部认定的综合电信服务运营商。亿恩为近五十万的用户提供服务器托管、服务器租用、机柜租用、云服务器、网站建设、网站托管等网络基础服务,另有网总管、名片侠网络推广服务,使得客户不断的获得更大的收益。
服务器/云主机 24小时售后服务电话:
0371-60135900
虚拟主机/智能建站 24小时售后服务电话:
0371-55621053
网络版权侵权举报电话:
0371-60135995
服务热线:
0371-60135900