虚拟主机order,虚拟主机的概念

apache虚拟主机配置好了以后localhost打不开

其实你只需要在你的httpd-vhosts.conf里面添加一条localhost的规则就可以了

比如localhost的文档根目录是/var/www

<VirtualHost*:80>

ServerAdminlocalhost@localhost.com

DocumentRoot"/var/www"

ServerNamelocalhost

ServerAliaslocalhost

ErrorLog"logs/localhost.log"

CustomLog"logs/localhost.log"common

<Directory"/var/www">

OptionsFollowSymLinks

AllowOverrideAll

Requireallgranted

</Directory>

</VirtualHost>

原因是当你开启虚拟主机后apache会对访问本服务器的域名做规则匹配,localhost也是一域名,apache会捕获它,匹配虚拟主机的规则,发现没有,则会选取虚拟主机中与之最相似的第一条作为规则进行处理,如果你不用域名而是直接用IP就会跳过虚拟主机的规则匹配:127.0.0.1

mac 上 mamp 配置虚拟主机 具体过程

不知道楼主用的自带的还是mamp,过程基本上差不多只是配置文件的路径不太一样

首先进入apache的配置文件httpd.conf,如果是mamp,路径

是/Applications/MAMP/conf/apache/httpd.conf,用文本编辑器打开,command+f查找一下‘vhosts’关键字,来到了这一行

#Include/Applications/MAMP/conf/apache/extra/httpd-vhosts.conf

把前面的#号去掉,表示这句话生效,这句话的意思就是使虚拟地址的配置文件生效。

保存后,用文本编辑器打开/Applications/MAMP/conf/apache/extra/httpd-vhosts.conf,加入需要配置的虚拟地址,大概是这样的:

<VirtualHost*:80>

DocumentRoot"/Users/Cancel/Develop/MAMP/test"

ServerName lypton.test.com

ServerAlias lypton.test.com

<Directory"/Users/Cancel/Develop/MAMP/test">

Options Indexes FollowSymLinks

AllowOverride All

Order deny,allow

Allow from all

</Directory>

</VirtualHost>

其中DocumentRoot就是你的网站文件目录,ServerName是你希望配置的虚拟域名,ServerAlias也是一个别名,下面的Directory标签是一些配置,这些参数我也还在学习中。。例如第一个Options Indexes FollowSymLinks的意思是如果有index就显示index文件,如果没有就显示目录结构。

保存后,来到host配置文件,/etc/hosts,可以进入finder,shift+command+G,输入/etc,回车后找到hosts文件,用编辑器打开,也可以直接在终端输入 vi/etc/hosts,然后添加一行解析的语句

127.0.0.1 lypton.test.com

保存以后,理论上localhost和127.0.0.1和localhost已经解析不了了,我还在研究这个问题,重启一下apache,在浏览器输入lypton.test.com,就是你刚才设置的虚拟域名,看看有没有成功。

我配置的时候也遇到了不少问题,其中有些也是稀里糊涂解决的,比如我现在还不太清楚网站文件的权限对虚拟域名的访问是否有影响,我把网站目录文件的权限都改成777了现在。

如果楼主用的mac自带的apache,httpd.conf配置文件在/etc/apache2/httpd.conf这儿,httpd-vhosts.conf文件在/etc/apache2/extra//etc/vhosts.conf这儿,hosts还是原来的位置。

如何设置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>

ServerName www.test1.com

DocumentRoot/www/test1/

<Directory"/www/test1">

Options Indexes FollowSymLinks

AllowOverride None

Order allow,deny

Allow from all

</Directory>

</VirtualHost>

<VirtualHost*:80>

ServerName www.test2.com

DocumentRoot/www/test2/

<Directory"/www/test2">

Options Indexes FollowSymLinks

AllowOverride None

Order allow,deny

Allow from all

</Directory>

</VirtualHost>

<VirtualHost*:80>

ServerName www.test3.com

DocumentRoot/www/test3/

<Directory"/www/test3">

Options Indexes FollowSymLinks

AllowOverride None

Order allow,deny

Allow from all

</Directory>

</VirtualHost>

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

三、基于端口

1.修改配置文件

将原来的

Listen 80

改为

Listen 80

Listen 8080

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>

阅读剩余
THE END