天天看点

关于PHP应用中的APACHE的虚拟目录和虚拟主机相关的设置

背景

有多个网站在同一个apache服务下,由于之前的服务已经部署了,不能大动,可是后来开发的php应用的路径只能是根目录,因为所有的页面开发,程序中的跳转路径都是写的/根目录下的。怎么办呢?静下心来想想,我这里有两个域名,有4个服务三个服务可以在同一个域名下,但是可以是二级目录,另一个服务有另一个域名。想到这里

想到nginx可以设置虚拟主机(都在同一个端口下,根据不同的域名区分访问),也可以设置二级目录,apache,同样是web服务器应该也可以做到相应的情况。想到这里我们来具体实现吧

前置条件

  1. apache server
  2. 多个网站
  3. 网站可执行文件路径分别是
C:\phpStudy\PHPTutorial\WWW\xxx
C:\phpStudy\PHPTutorial\WWW\abc
C:\phpStudy\PHPTutorial\WWW\def
C:\phpStudy\PHPTutorial\WWW\cnn
           
  1. 域名 分别是 wodetian.com,www.wodedi.cn
  2. xxx 对应 wodetian.com ,abc对应www.wodedi.cn/ ,def 对应 www.wodedi.cn/def,cnn 对应 www.wodedi.cn/cnn

配置httpd.conf

首先找到对应 httpd-vhosts.conf的配置,取消#的注释,也可以复制一行,我的习惯是复制一行,在取消注释

#Include conf/extra/httpd-vhosts.conf
Include conf/extra/httpd-vhosts.conf
           

然后设置虚拟主机 ,设置第一个服务xxx到域名wodetian.com

<VirtualHost *:80>  
    DocumentRoot "C:\phpStudy\PHPTutorial\WWW\xxx"   
    ServerName wodetian.com 
</VirtualHost>
           

设置第二个主机wodedi 下面的abc为根目录的应用,设置def为二级目录,设置cnn也为二级目录,并且他们的物理路径也都不一样

<VirtualHost *:80>
DocumentRoot "C:\phpStudy\PHPTutorial\WWW\abc"
ServerName www.wodedi.cn
Alias /def "C:\phpStudy\PHPTutorial\WWW\def"  
Alias /cnn"C:\phpStudy\PHPTutorial\WWW\cnn" 
  <Directory "C:\phpStudy\PHPTutorial\WWW\abc">
    Options -Indexes -FollowSymLinks +ExecCGI
    AllowOverride All
    Order allow,deny
    Allow from all
    Require all granted
  </Directory>
</VirtualHost>
           

设置完毕后,重启apache ,一切如此美好,问题解决