Apache VirtualHost的配置

自从电脑更换为mac后, 一直没有时间去配置php的环境。导致每次要更改php代码的时候, 都是本地更改,然后直接推送到服务器上运行 这样的开发和测试及其耗时且繁琐, 所以早上特地决定弄好mac下的php开发环境,毕竟磨刀不误砍柴工,一劳永逸嘛。

下载按照好mamp后,选择web服务器为apache。更改配置文件httpd.conf,添加相应的加载moudle。打开虚拟配置。httpd-vhost.conf,因为当前的项目存在两个独立的项目,分别是front.xxx.local.admin.xxx.local

httpd-vhost.conf配置如下:

#
# 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>
    DocumentRoot "/Users/fly_popc/Desktop/code/front/web"
    ServerName front.xxx.local
    ServerAlias front.xxx.local
    <Directory "/Users/fly_popc/Desktop/code/front/web">
        RewriteEngine on
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule . index.php
    </Directory>
</VirtualHost>

<VirtualHost *:80>
    DocumentRoot "/Users/fly_popc/Desktop/code/admin/web"
    ServerName admin.xxx.local
    ServerAlias admin.xxx.local
    <Directory "/Users/fly_popc/Desktop/code/admin/web">
        RewriteEngine on
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule . index.php
    </Directory>
</VirtualHost>

重启apache后,环境运行正常。

但是...

当访问后台项目的时候,却一直路由到了第一个地址上, 也就是apache配置多个域名指向的虚拟主机访问总是指向第一个虚拟主机。查询了好几个帖子后,依然不能解决问题。最后终于了解到有个配置没有打开,那就是:

#
# Use name-based virtual hosting.
#
NameVirtualHost *:80

本着追本溯源的精神, 查询了下这个配置的意思,得到的原因如下:

NameVirtualHost 如果没有这个,<VirtualHost>标签就没什么作用。基于域名访问的情况,若是基于IP访问的,以第一个指定IP的VirtualHost为准,每个IP可以单独指定)一个NameVirtualHost 可以对用多个<VirtualHost>,每个<VirtualHost>必须有自己的NameVirtualHostNameVirutalHost *:80制定这个主机的IP和端口,如果服务器上有多个IP,就可以制定某个IP的某个端口是哪个主机。(新版的Apache已经去除了NameVirtualHost 这个配置,因为确实没什么用,参数在VirtualHost中都已经指明了)。

至此打开浏览器,分别访问http://front.xxx.local 和http://admin.xxx.local 都能各自访问到对应的页面,大功告成!下面记录下整个流程.

1.先要在httpd.conf(这个是Apache 总的配置文件)中,将虚拟路径的注释去掉。
#Include etc/extra/httpd-vhosts.conf
使httpd-vhosts.conf文件起作用,或者直接在httpd.conf中写配置也可以,但不建议这么做。
相关的配置有:Listen NameVirtualHost <VirtualHost>

2.Listen要监听的端口,多个端口,要写多个Listen;否则Apache启动的时候,不会启动相应的套接字。

比如

Listen 80
Listen 8080

3.NameVirtualHost 如果没有这个,<VirtualHost>标签就没什么作用。

4.最关键的VirtualHost。
重要:Apache 在接受到请求时,首先会默认第一个VirtualHost,然后再找匹配的,如果没有匹配的,就是第一个VirtualHost起作用。
因此在httpd.conf中,将<Dicrectory />(这个是所有目录的默认配置)
和<Direcotry /opt/lampp/htdocs>的权限,都是deny from all.作为默认;或者直接在httpd.conf中配置

<Directory />
    Options Indexes FollowSymLinks
    AllowOverride All
</Directory>

至此,项目就能正常运行起来了!

时间: 2024-07-30 12:29:48

Apache VirtualHost的配置的相关文章

Apache虚拟主机(VirtualHost)配置

首先找到Apache的配置文件httpd.conf find / -name httpd.conf 找到以下代码处 #Virtual hosts #Include conf/extra/httpd-vhosts.conf 按照上面的格式加入一行,你的虚拟主机配置文件,当然,你可以先加入再去写配置 Include conf/extra/httpd-vhosts.端口号.conf #此处填写端口号是为了统一管理,便于记忆 之后新建配置文件,在配置文件中输入以下信息 touch conf/extra/

Apache22中配置虚拟主机(Apache VirtualHost)

Apache VirtualHost的作用就是可以让一个apache为多个域名服务,相当于一个服务器挂了N多个网站,举个例子: 我的apache服务器,ip为x.x.x.x,我有两个域名www.tootoogo.org和lp.tootoogo.org,设置这两个域名都指向x.x.x.x,在apache服务器上设置virtual host后,apache服务器可以实现同时为这两个域名服务,使得这两个域名分别指向不同的网页,于是我的一个服务器上就同时有了两个网站. Apache VirtualHos

Apache虚拟主机配置

在一个Apache服务器上可以配置多个虚拟主机,实现一个服务器提供多站点服务,其实就是访问同一个服务器上的不同目录.Apache虚拟主机配置有3中方法:基于IP配置.基于域名配置和基于端口配置,这里介绍基于域名配置和基于端口配置,基于IP配置方法类似. 1. Apache基于域名配置虚拟主机: 打开Apache安装目录下的配置文件conf/extra/httpd-vhosts.conf,添加如下配置信息: <VirtualHost _default_:80> DocumentRoot &quo

Win2008R2+Apache+PHP+Tomcat配置

一.VC运行库 对于Apache和PHP,在Windows上运行的话都需要对应VC运行库的支持,即Apache.PHP和VC运行库这三者的版本必须相对应,这就会带来很多问题,比如下了较新的Apache或PHP,但是找不到对应的和对方连接的VC运行库,此外还要注意系统是32位还是64位.而Linux就没这种破问题,所以我很想不通那些用Windows来做这种较复杂服务器的人脑子里装的是神马.当前常见的VC运行库包括VC9和VC11,x86和x64都装上才保险.可以去http://www.micros

asp.net 站点在Apache下的配置,就这么简单

asp.net 站点在Apache下的配置,就这么简单 # # 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 t

Apache安装与配置(Linux)

======关于APACHE安装和配置======--离线包解压并进入解压后文件夹tar -zvxf httpd-2.2.25.tar cd httpd-2.2.25 ./configure \ --enable-mods-shared=max \ --enable-module=most \ --with-mpm=prefork \ --enable-ext-filter \ --enable-file-cache \ --enable-suexec \ --enable-cache \ --

Apache在本地配置多个站点

在我们安装APACHE的时候一般默认的apache的配置是只有一个网站,这样切换起来很不方便.其实这个问题很好解决,就是把本机的 apache配置成为虚拟服务器.但是,网上大多数教程的是教用 apache如何配置基于域名的虚拟主机的,而在本机调试网站的时候,一般都是用本地ip(127.0.0.1 或 localhost)直接访问,没有用到域名.所以得把apache配置成为基于ip地址的虚拟主机. 首先,我们都知道,所有以127打头的ip地址都应该指向本机,并不只有127.0.0.1,这点大家可以

Apache多站点配置及启动失败解决办法

一. Apache多站点配置方法 1.打开Apache安装目录下conf/httpd.conf文件,找到下面两行文字,把最前面的 # 号去掉,然后保存. #LoadModule vhost_alias_module modules/mod_vhost_alias.so #Include conf/extra/httpd-vhosts.conf 2.接着找到同一文件中的DocumentRoot和Directory,改为站点目录的上一级目录 例如站点放在 C:/XAMPP/Appserv/Web/h

apache多站点配置

先转:http://www.cnblogs.com/ganmk/archive/2012/07/10/2585315.html#undefined apache 多站点配置 1: 安装好AppServ2.5.9软件,官网是:[url]http://www.appservnetwork.com/[/url],2.59下载地址是:[url]http://nchc.dl.sourceforge.net/sourceforge/appserv/appserv-win32-2.5.9.exe[/url]