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>

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

原文地址:https://www.cnblogs.com/_popc/p/7228106.html