如何配置apache虚拟主机的实例小结

admin3年前主机评测49

1、基于ip地址的虚拟主机

 

复制代码 代码如下:

Listen 80
<VirtualHost 172.20.30.40>
    DocumentRoot /home/httpd/html1
    ServerName ok1
    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 ok2
    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 example1
</VirtualHost>
<VirtualHost 172.20.30.40:8080>

 DocumentRoot /www/example1-8080
 ServerName example1
</VirtualHost>
<VirtualHost 172.20.30.50:80>
 DocumentRoot /www/example2-80
 ServerName example1.org
</VirtualHost>
<VirtualHost 172.20.30.50:8080>
 DocumentRoot /www/example2-8080
 ServerName 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 example1
ServerAlias example1. *.example1
# Other directives here
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /www/example2
ServerName 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
127.0.0.1   test2

 

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

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

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

 

复制代码 代码如下:

<HTML>
<HEAD></HEAD>
<BODY>
<H1>hello~, 这是[对应的网站名,用以区别]</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
    DocumentRoot "C:/xampp/htdocs1/test1"
    ServerName test1
    ServerAlias test1
    ErrorLog "logs/test1-error.log"
    CustomLog "logs/test1-access.log" combined

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

    <Directory "C:/xampp/htdocs1/test2">
    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基于域名的虚拟主机配置完成。可以通过localhost访问XAMPP欢迎页,通过test1和test2访问各自的主页。

#
# 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: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
    DocumentRoot "E:/skydao/apache2/htdocs"
    ServerName localhost
    ServerAlias skydao
    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
    DocumentRoot "E:/skydao/apache2/htdocs/project1"
    ServerName project1
    ServerAlias project1
    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
    DocumentRoot "E:/skydao/apache2/htdocs/zendTest/public"
    ServerName zendTest
    ServerAlias zendTest
    DirectoryIndex index.php
    <Directory />
Options FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
    </Directory>   
</VirtualHost>

<VirtualHost *:80>
    ServerAdmin kongdaoxian@gmail
    DocumentRoot "E:/skydao/apache2/htdocs/testRewrite"
    ServerName testRewrite
    ServerAlias testRewrite
    # DirectoryIndex index.php
    <Directory />
Options FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
    </Directory>   
</VirtualHost>

<VirtualHost *:80>
    ServerAdmin kongdaoxian@gmail
    DocumentRoot "E:/skydao/apache2/htdocs/test"
    ServerName test
    ServerAlias test
    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>

免责声明:本文内容来自用户上传并发布,站点仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。请核实广告和内容真实性,谨慎使用。

相关文章

国内免备案虚拟主机有哪些

国内免备案虚拟主机有:1、香港主机,访问速度快,但带宽价格较贵,适合用来做小型的网站或博客;2、小型IDC运营商,类似于CDN,但机房不正规,容易出现数据丢失问题,且数据无法恢复;3、免备案的CDN主...

国外虚拟主机空间有哪些优缺点

国外虚拟主机空间的优缺点:1、国外虚拟主机空间的优点是功能齐全、空间容量大、免备案、数据库大小及流量没有限制、服务完善且价格便宜;2、国外虚拟主机空间的缺点是安全性差,网站容易被查封,国内访问速度较慢...

虚拟主机和云服务器哪个好?

虚拟主机和云服务器哪个好?虚拟主机和云服务器都是搭建网站所需要的,就整体而言,云服务器资源、性能以及安全方面,都高于虚拟主机,但虚拟主机成本更低,适合中小企业。至于使用哪个更好,这其实要看用户的需求。...

美国虚拟主机空间的类型有哪些

美国虚拟主机空间的类型有:1、HTML空间,能将网站搭建的内容、信息展示给用户;2、ASP空间,能使数据和其他程序进行交互,且能向下兼容HTML静态程序;3、PHP空间,能被编译为Apache模块;4...

韩国虚拟主机服务商怎么选择

韩国虚拟主机服务商选择的方法:1、看服务商的资质,确认服务商的供应链存在时间及是直接提供服务还是通过第三方提供服务;2、看服务水平协议及合同,包括定价政策、付款条件、合同期限和终止条件、状况处理预案及...

韩国免备案虚拟主机租用怎么选择提供商

选择韩国免备案虚拟主机租用提供商时,需要考虑以下几点:1、服务质量选择提供商时,需要关注其服务器的稳定性、网络速度和可靠性等方面,可以通过查看用户评价和在线测试来了解。2、价格虚拟主机租用的价格差异较...