apache基于域名的虚拟主机,如何设置apache的虚拟主机

阅读(124)发布于 2023-09-25

Apache是世界使用排名第一的Web服务器。Apache源于NCSAd服务器,经过多次修改,成为世界上最流行的Web服务器软件之一。Apache有多种产品,可以支持SSL技术,支持多个虚拟主机。到目前为止Apache仍然是世界上用的最多的Web服务器,市场占有率达60%左右。Apache的诞生极富有戏剧性。就这样,诞生了ApacheGroup,后来这个团体在NCSA的基础上创建了Apache。

配图

分类:电脑/网络>>互联网

问题描述:

就是TOMCAT软件吗?

解析:

apache

Apache音译为阿帕奇,是北美印弟安人的一个部落,叫阿帕奇族,在美国的西南部。相传阿帕奇是一个武士,他英勇善战,且战无不胜,被印弟安人奉为勇敢和胜利的代表,因此后人便用他的名字为印弟安部落命名,而阿帕奇族在印第安史上也以强悍著称。

Apache是世界使用排名第一的Web服务器。它可以运行在几乎所有广泛使用的计算机平台上。

Apache源于NCSAd服务器,经过多次修改,成为世界上最流行的Web服务器软件之一。Apache取自“apatchyserver”的读音,意思是充满补丁的服务器,因为它是自由软件,所以不断有人来为它开发新的功能、新的特性、修改原来的缺陷。Apache的特点是简单、速度快、性能稳定,并可做代理服务器来使用。

本来它只用于小型或试验Inter网络,后来逐步扩充到各种Unix系统中,尤其对Linux的支持相当完美。Apache有多种产品,可以支持SSL技术,支持多个虚拟主机。Apache是以进程为基础的结构,进程要比线程消耗更多的系统开支,不太适合于多处理器环境,因此,在一个ApacheWeb站点扩容时,通常是增加服务器或扩充群集节点而不是增加处理器。到目前为止Apache仍然是世界上用的最多的Web服务器,市场占有率达60%左右。世界上很多著名的网站如Amazon、Yahoo!、W3Consortium、FinancialTimes等都是Apache的产物,它的成功之处主要在于它的源代码开放、有一支开放的开发队伍、支持跨平台的应用(可以运行在几乎所有的Unix、Windows、Linux系统平台上)以及它的可移植性等方面。

Apache的诞生极富有戏剧性。当NCSAWWW服务器项目停顿后,那些使用NCSAWWW服务器的人们开始交换他们用于该服务器的补丁程序,他们也很快认识到成立管理这些补丁程序的论坛是必要的。就这样,诞生了ApacheGroup,后来这个团体在NCSA的基础上创建了Apache。

Apache服务器拥有以下特性:

支持最新的HTTP/1.1通信协议

拥有简单而强有力的基于文件的配置过程

支持通用网关接口

支持基于IP和基于域名的虚拟主机

支持多种方式的HTTP认证

集成Perl处理模块

集成代理服务器模块

支持实时监视服务器状态和定制服务器日志

支持服务器端包含指令(SSI)

支持安全Socket层(SSL)

提供用户会话过程的跟踪

支持FastCGI

通过第三方模块可以支持JavaServlets

如果你准备选择Web服务器,毫无疑问Apache是你的最佳选择。

apache/

是它的官方网站.

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

复制代码代码如下:

Listen80

<VirtualHost172.20.30.40>

DocumentRoot/home/httpd/html1

ServerNamewww.ok1.com

ErrorLog/usr/local/apache/logs/error1_log

CustomLog/usr/local/apache/logs/access1_logcombined

</VirtualHost>

<VirtualHost172.20.30.50>

DocumentRoot/home/httpd/html2

ServerNamewww.ok2.com

ErrorLog/usr/local/apache/logs/error2_log

CustomLog/usr/local/apache/logs/access2_logcombined

</VirtualHost>

2、基于IP和多端口的虚拟主机配置

复制代码代码如下:

Listen172.20.30.40:80

Listen172.20.30.40:8080

Listen172.20.30.50:80

Listen172.20.30.50:8080

<VirtualHost172.20.30.40:80>

DocumentRoot/www/example1-80

ServerNamewww.example1.com

</VirtualHost>

<VirtualHost172.20.30.40:8080>

DocumentRoot/www/example1-8080

ServerNamewww.example1.com

</VirtualHost>

<VirtualHost172.20.30.50:80>

DocumentRoot/www/example2-80

ServerNamewww.example1.org

</VirtualHost>

<VirtualHost172.20.30.50:8080>

DocumentRoot/www/example2-8080

ServerNamewww.example2.org

</VirtualHost>

3、单个IP地址的服务器上基于域名的虚拟主机配置

复制代码代码如下:

#EnsurethatApachelistensonport80

Listen80

#ListenforvirtualhostrequestsonallIPaddresses

NameVirtualHost*:80

<VirtualHost*:80>

DocumentRoot/www/example1

ServerNamewww.example1.com

ServerAliasexample1.com.*.example1.com

#Otherdirectiveshere

</VirtualHost>

<VirtualHost*:80>

DocumentRoot/www/example2

ServerNamewww.example2.org

#Otherdirectiveshere

</VirtualHost>

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

实验目标:在apache实现基于域名的虚拟主机

实验用的XAMPP版本为1.7.7,内含apache版本为2.2.21

实验前准备:

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

复制代码代码如下:

127.0.0.1test1.net

127.0.0.1test2.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网站内容

在这两个目录中各新建helloworld一网页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>

ServerAdminXXXXXXXX

DocumentRoot"XXXXXXXX"

ServerNameXXXXXXX

ServerAliasXXXXXX

ErrorLog"logs/XXXXXX-error.log"

CustomLog"logs/XXXXXXX-error.log"combined

</VirtualHost>

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

复制代码代码如下:

<VirtualHost*:80>

ServerAdminadm@test1.net

DocumentRoot"C:/xampp/htdocs1/test1.net"

ServerNametest1.net

ServerAliaswww.test1.net

ErrorLog"logs/test1-error.log"

CustomLog"logs/test1-access.log"combined

<Directory"C:/xampp/htdocs1/test1.net">

orderallow,deny

allowfromall

</Directory>

</VirtualHost>

<VirtualHost*:80>

ServerAdminadm@test2.net

DocumentRoot"C:/xampp/htdocs1/test2.net"

ServerNametest2.net

ServerAliaswww.test2.net

ErrorLog"logs/test1-error.log"

CustomLog"logs/test1-access.log"combined

<Directory"C:/xampp/htdocs1/test2.net">

orderallow,deny

allowfromall

</Directory>

</VirtualHost>

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

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

复制代码代码如下:

<VirtualHost*:80>

ServerAdminadm@localhost

DocumentRoot"C:/xampp/htdocs"

ServerNamelocalhost

ErrorLog"logs/localhost-error.log"

CustomLog"logs/localhost-access.log"combined

<Directory"C:/xampp/htdocs">

orderallow,deny

allowfromall

</Directory>

</VirtualHost>

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

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

#

#VirtualHosts

#

#Ifyouwanttomaintainmultipledomains/hostnamesonyour

#machineyoucansetupVirtualHostcontainersforthem.Mostconfigurations

#useonlyname-basedvirtualhostssotheserverdoesn'tneedtoworryabout

#IPaddresses.Thisisindicatedbytheasterisksinthedirectivesbelow.

#

#Pleaseseethedocumentationat

#<URL:http://httpd.apache.org/docs/2.2/vhosts/>

#forfurtherdetailsbeforeyoutrytosetupvirtualhosts.

#

#Youmayusethecommandlineoption'-S'toverifyyourvirtualhost

#configuration.

#

#Usename-basedvirtualhosting.

#

NameVirtualHost*:80

#

#VirtualHostexample:

#AlmostanyApachedirectivemaygointoaVirtualHostcontainer.

#ThefirstVirtualHostsectionisusedforallrequeststhatdonot

#matchaServerNameorServerAliasinany<VirtualHost>block.

#

<VirtualHost*:80>

ServerAdminkongdaoxian@gmail.com

DocumentRoot"E:/skydao/apache2/htdocs"

ServerNamelocalhost

ServerAliaswww.skydao.com

ErrorLog"logs/localhost-error.log"

CustomLog"logs/localhost-access.log"combined

<Directory"E:/skydao/apache2/htdocs">

orderallow,deny

allowfromall

</Directory>

</VirtualHost>

<VirtualHost*:80>

ServerAdminkongdaoxian@gmail.com

DocumentRoot"E:/skydao/apache2/htdocs/project1"

ServerNameproject1.com

ServerAliaswww.project1.com

ErrorLog"logs/project1-error.log"

CustomLog"logs/project1-access.log"combined

<Directory"E:/skydao/apache2/htdocs/project1">

orderallow,deny

allowfromall

</Directory>

</VirtualHost>

<VirtualHost*:80>

ServerAdminkongdaoxian@gmail.com

DocumentRoot"E:/skydao/apache2/htdocs/zendTest/public"

ServerNamezendTest.com

ServerAliaswww.zendTest.com

DirectoryIndexindex.php

<Directory/>

OptionsFollowSymLinks

AllowOverrideNone

Orderallow,deny

Allowfromall

</Directory>

</VirtualHost>

<VirtualHost*:80>

ServerAdminkongdaoxian@gmail.com

DocumentRoot"E:/skydao/apache2/htdocs/testRewrite"

ServerNametestRewrite.com

ServerAliaswww.testRewrite.com

#DirectoryIndexindex.php

<Directory/>

OptionsFollowSymLinks

AllowOverrideNone

Orderallow,deny

Allowfromall

</Directory>

</VirtualHost>

<VirtualHost*:80>

ServerAdminkongdaoxian@gmail.com

DocumentRoot"E:/skydao/apache2/htdocs/test"

ServerNametest.com

ServerAliaswww.test.com

ErrorLog"logs/zendTest-error.log"

CustomLog"logs/zendTest-access.log"combined

<Directory"E:/skydao/apache2/htdocs/test">

orderallow,deny

allowfromall

</Directory>

</VirtualHost>

方法/步骤

Apache配置虚拟主机方法在windows和linux有些差异,今天给大家演示如何在windows上配置Apache虚拟主机,此方法主要适用于独立的Apache环境。

1.打开目录Apache的安装目录/conf/extra/,找到httpd-vhosts.conf文件;

2.添加一段代码来指定某一域名的网站,如图我配置虚拟域名是:

project.com,文件目录指向的是CI框架;

<VirtualHost*:80>

ServerAdminwebmaster@dummy-host.project.com

DocumentRoot"D:/AMP/Apache/htdocs/ci"

ServerNameproject.com

ErrorLog"logs/project.com-error.log"

CustomLog"logs/project.com-access.log"common

</VirtualHost>

3.打开httpd.conf文件,添加如下代码:

#Virtualhosts

Includeconf/extra/httpd-vhosts.conf

如果已存在,将Include前面的#去掉;

4.重启Apache服务,打开虚拟域名之后,结果如图所示:

Apache配置虚拟主机三种方式

一、基于IP

1.假设服务器有个IP地址为192.168.1.10,使用ifconfig在同一个网络接口eth0上绑定3个IP:

[root@localhostroot]#ifconfigeth0:1192.168.1.11

[root@localhostroot]#ifconfigeth0:2192.168.1.12

[root@localhostroot]#ifconfigeth0:3192.168.1.13

2.修改hosts文件,添加三个域名与之一一对应:

192.168.1.11www.test1.com

192.168.1.12www.test2.com

192.168.1.13www.test3.com

3.建立虚拟主机存放网页的根目录,如在/www目录下建立test1、test2、test3文件夹,其中分别存放1.html、2.html、3.html

/www/test1/1.html

/www/test2/2.html

/www/test3/3.html

4.在httpd.conf中将附加配置文件httpd-vhosts.conf包含进来,接着在httpd-vhosts.conf中写入如下配置:

<VirtualHost192.168.1.11:80>

ServerNamewww.test1.com

DocumentRoot/www/test1/

<Directory"/www/test1">

OptionsIndexesFollowSymLinks

AllowOverrideNone

Orderallow,deny

AllowFromAll

</Directory>

</VirtualHost>

<VirtualHost192.168.1.12:80>

ServerNamewww.test1.com

DocumentRoot/www/test2/

<Directory"/www/test2">

OptionsIndexesFollowSymLinks

AllowOverrideNone

Orderallow,deny

AllowFromAll

</Directory>

</VirtualHost>

<VirtualHost192.168.1.13:80>

ServerNamewww.test1.com

DocumentRoot/www/test3/

<Directory"/www/test3">

OptionsIndexesFollowSymLinks

AllowOverrideNone

Orderallow,deny

AllowFromAll

</Directory>

</VirtualHost>

5.大功告成,测试下每个虚拟主机,分别访问www.test1.com、www.test2.com、www.test3.com

二、基于主机名

1.设置域名映射同一个IP,修改hosts:

192.168.1.10www.test1.com

192.168.1.10www.test2.com

192.168.1.10www.test3.com

2.跟上面一样,建立虚拟主机存放网页的根目录

/www/test1/1.html

/www/test2/2.html

/www/test3/3.html

3.在httpd.conf中将附加配置文件httpd-vhosts.conf包含进来,接着在httpd-vhosts.conf中写入如下配置:

为了使用基于域名的虚拟主机,必须指定服务器IP地址(和可能的端口)来使主机接受请求。可以用NameVirtualHost指令来进行配置。如果服务器上所有的IP地址都会用到,你可以用*作为NameVirtualHost的参数。在NameVirtualHost指令中指明IP地址并不会使服务器自动侦听那个IP地址。这里设定的IP地址必须对应服务器上的一个网络接口。

下一步就是为你建立的每个虚拟主机设定<VirtualHost>配置块,<VirtualHost>的参数与NameVirtualHost指令的参数是一样的。每个<VirtualHost>定义块中,至少都会有一个ServerName指令来指定伺服哪个主机和一个DocumentRoot指令来说明这个主机的内容存在于文件系统的什么地方。

如果在现有的web服务器上增加虚拟主机,必须也为现存的主机建造一个<VirtualHost>定义块。其中ServerName和DocumentRoot所包含的内容应该与全局的保持一致,且要放在配置文件的最前面,扮演默认主机的角色。

NameVirtualHost*:80

<VirtualHost*:80>

ServerName*

DocumentRoot/www/

</VirtualHost>

<VirtualHost*:80>

ServerNamewww.test1.com

DocumentRoot/www/test1/

<Directory"/www/test1">

OptionsIndexesFollowSymLinks

AllowOverrideNone

Orderallow,deny

Allowfromall

</Directory>

</VirtualHost>

<VirtualHost*:80>

ServerNamewww.test2.com

DocumentRoot/www/test2/

<Directory"/www/test2">

OptionsIndexesFollowSymLinks

AllowOverrideNone

Orderallow,deny

Allowfromall

</Directory>

</VirtualHost>

<VirtualHost*:80>

ServerNamewww.test3.com

DocumentRoot/www/test3/

<Directory"/www/test3">

OptionsIndexesFollowSymLinks

AllowOverrideNone

Orderallow,deny

Allowfromall

</Directory>

</VirtualHost>

4.大功告成,测试下每个虚拟主机,分别访问www.test1.com、www.test2.com、www.test3.com

三、基于端口

1.修改配置文件

将原来的

Listen80

改为

Listen80

Listen8080

2.更改虚拟主机设置:

<VirtualHost192.168.1.10:80>

DocumentRoot/var/www/test1/

ServerNamewww.test1.com

</VirtualHost>

<VirtualHost192.168.1.10:8080>

DocumentRoot/var/www/test2

ServerNamewww.test2.com

</VirtualHost>

以上就是关于apache基于域名的虚拟主机的解答,如果对你有帮助,不妨关注本站,本站将为你整理更多内容。