词条信息

admin
admin
超级管理员
词条创建者 发短消息   

相关词条

热门词条

更多>>
什么是端口?到底是做什么的呢?
端口一般指两种,一种是硬件比如路由器或者交换机的插网线的端口,一种是软件的逻辑的概念,比如http的80端口!...
7种进阶方法让你快速测试端口连通性
Ping是Windows、Linux和Unix系统下的一个检查网络连通性的命令工具,对于大部分互联网用户来说很...
电脑开机,总需要按F1,是什么原因造成的?
一.主板掉电这个说法是行业内的叫法了,一般是主板的CMOS电池没电了导致的。也是最常见的一种提示你按F1的提示...
社保降费对个人有什么影响?
下调城镇职工基本养老保险单位缴费比例是政府给企业发的一个大红包,特别是对于企业来说是一个利好,但是对个人来说有...
车辆“出险”对下年保费的影响,到底有多大?
【出险对交强险的影响】【出险对商业险的影响】车辆“出险”对下年保费的影响,到底有多大?这里有必要先提下车险第三...

精选图集

更多>>
简易百科旧版 >>所属分类 >> 网站建设    环境配置   

如何配置apache虚拟主机(案例)

标签: apache 虚拟主机 主机配置

顶[0] 发表评论(0) 编辑词条

  如果你是第一次配置apache虚拟主机,那么通过阅读这篇文章你将会了解到如何实现apache虚拟主机配置。其实要配置好一台虚拟主机没有想象中那么难

1、基于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>

 2、基于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>

3、单个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>



如果您感觉上面的文章还不够详细可以看下下面的文章:

实验目标:在apache实现基于域名的虚拟主机 
实验用的XAMPP版本为1.7.7,内含apache版本为2.2.21 

实验前准备:

1. 为了测试不同的域名,在Windows/System32/drivers/etc/下觅得hosts文件,在其中添加实验用的域名若干,如 -


127.0.0.1   test1.net
127.0.0.1   test2.net

如此,则在浏览器中输入该俩域名时,Windows将其解析为127.0.0.1本地地址。即,在浏览器中访问localhost, test1.net, test2.net均可访问XAMPP的欢迎页。

2. 在apache目录下建立目录,以放置您不同的网站。为保护XAMPP原有的htdocs中的欢迎页内容,实验另外建立了与htdocs平级的htdocs1目录,在其下建立了test1.net, test2.net两个子目录用以放置实验用的网站。如下 -
apache/htdocs1/test1.net - 放置test1.net网站内容
apache/htdocs1/test2.net - 放置test2.net网站内容

 在这两个目录中各新建hello world一网页 index.html,内容 -


<HTML>
<HEAD></HEAD>
<BODY>
<H1>hello~, 这是[对应的网站名,用以区别].net</H1></BODY>
</HTML>  

实验步骤:

1. 找到apache/conf/httpd.conf, 将其中的

ServerAdmin
ServerName
DocumentRoot
注释掉。

2. 在httpd.conf中,找到行

 Include "conf/extra/httpd-vhosts.conf"
如被注释则解注。该文件记载了虚拟主机的参数。[以前虚拟主机参数是直接填写在httpd.conf中的,为了更好地组织文件,将其分离出去,类似于某些编程语言一样。因此httpd.conf中include它,即相当于把它的内容填在了httpd.conf中。]

3. 这个httpd-vhosts.conf文件格式基本如下 -

#blah-blah
NameVirtualHost *:80
#blah-blah
#blah-blah
<VirtualHost *:80>
    ServerAdmin XXXXXXXX
    DocumentRoot "XXXXXXXX"
    ServerName XXXXXXX
    ServerAlias XXXXXX
    ErrorLog "logs/XXXXXX-error.log"
    CustomLog "logs/XXXXXXX-error.log" combined   
</VirtualHost>

需要修改的,就是<VirtualHost>中的参数了。这个可以参见apache官方文档。根据实验域名,可以增加两个<VirtualHost>:

<VirtualHost *:80>
    ServerAdmin adm@test1.net
    DocumentRoot "C:/xampp/htdocs1/test1.net"
    ServerName test1.net
    ServerAlias www.test1.net
    ErrorLog "logs/test1-error.log"
    CustomLog "logs/test1-access.log" combined

    <Directory "C:/xampp/htdocs1/test1.net">
    order allow,deny
    allow from all
    </Directory>    
</VirtualHost>
<VirtualHost *:80>
    ServerAdmin adm@test2.net
    DocumentRoot "C:/xampp/htdocs1/test2.net"
    ServerName test2.net
    ServerAlias www.test2.net
    ErrorLog "logs/test1-error.log"
    CustomLog "logs/test1-access.log" combined

    <Directory "C:/xampp/htdocs1/test2.net">
    order allow,deny
    allow from all
    </Directory>    
</VirtualHost>

注意,如果不在各VirtualHost中定义Directory的可访问性,你将遇到的是Access Forbidden!就连原来的localhost也是。

4. 由于之前注释掉了httpd.conf中的ServerName, DocumentRoot等,为了仍然能以localhost访问原XAMPP欢迎页,就在增加一个VirtualHost,如下 -

<VirtualHost *:80>
    ServerAdmin adm@localhost
    DocumentRoot "C:/xampp/htdocs"
    ServerName localhost

    ErrorLog "logs/localhost-error.log"
    CustomLog "logs/localhost-access.log" combined

    <Directory "C:/xampp/htdocs">
    order allow,deny
    allow from all
    </Directory>    
</VirtualHost>

为了避免出错,把它放置在第一个Virtualhost位置。

至此,apache基于域名的虚拟主机配置完成。可以通过http://localhost访问XAMPP欢迎页,通过http://test1.net和http://test2.net访问各自的主页。 

#
# Virtual Hosts
#
# If you want to maintain multiple domains/hostnames on your
# machine you can setup VirtualHost containers for them. Most configurations
# use only name-based virtual hosts so the server doesn't need to worry about
# IP addresses. This is indicated by the asterisks in the directives below.
#
# Please see the documentation at 
# <URL:http://httpd.apache.org/docs/2.2/vhosts/>
# for further details before you try to setup virtual hosts.
#
# You may use the command line option '-S' to verify your virtual host
# configuration.
#
# Use name-based virtual hosting.
#
NameVirtualHost *:80
#
# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for all requests that do not
# match a ServerName or ServerAlias in any <VirtualHost> block.
#
<VirtualHost *:80>
    ServerAdmin kongdaoxian@gmail.com
    DocumentRoot "E:/skydao/apache2/htdocs"
    ServerName localhost
    ServerAlias www.skydao.com
    ErrorLog "logs/localhost-error.log"
    CustomLog "logs/localhost-access.log" combined

    <Directory "E:/skydao/apache2/htdocs">
    order allow,deny
    allow from all
    </Directory>    
</VirtualHost>
<VirtualHost *:80>
    ServerAdmin kongdaoxian@gmail.com
    DocumentRoot "E:/skydao/apache2/htdocs/project1"
    ServerName project1.com
    ServerAlias www.project1.com
    ErrorLog "logs/project1-error.log"
    CustomLog "logs/project1-access.log" combined

    <Directory "E:/skydao/apache2/htdocs/project1">
    order allow,deny
    allow from all
    </Directory>    
</VirtualHost>
<VirtualHost *:80>
    ServerAdmin kongdaoxian@gmail.com
    DocumentRoot "E:/skydao/apache2/htdocs/zendTest/public"
    ServerName zendTest.com
    ServerAlias www.zendTest.com
    DirectoryIndex index.php
    <Directory />
Options FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
    </Directory>    
</VirtualHost>
<VirtualHost *:80>
    ServerAdmin kongdaoxian@gmail.com
    DocumentRoot "E:/skydao/apache2/htdocs/testRewrite"
    ServerName testRewrite.com
    ServerAlias www.testRewrite.com
    # DirectoryIndex index.php
    <Directory />
Options FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
    </Directory>    
</VirtualHost>
<VirtualHost *:80>
    ServerAdmin kongdaoxian@gmail.com
    DocumentRoot "E:/skydao/apache2/htdocs/test"
    ServerName test.com
    ServerAlias www.test.com
    ErrorLog "logs/zendTest-error.log"
    CustomLog "logs/zendTest-access.log" combined

    <Directory "E:/skydao/apache2/htdocs/test">
    order allow,deny
    allow from all
    </Directory>    
</VirtualHost>


 

 

附件列表


按字母顺序浏览:A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

→我们致力于为广大网民解决所遇到的各种电脑技术问题
 如果您认为本词条还有待完善,请 编辑词条

上一篇apache虚拟主机三种配置方式
下一篇linux中rmdir命令使用详解

0
1. 本站部分内容来自互联网,如有任何版权侵犯或其他问题请与我们联系,我们将立即删除或处理。
2. 本站内容仅供参考,如果您需要解决具体问题,建议您咨询相关领域专业人士。
3. 如果您没有找到需要的百科词条,您可以到百科问答提问或创建词条,等待高手解答。

关于本词条的提问

查看全部/我要提问>>