Apache2.4 Virtual Hosts配置:模拟真实网站+同时开发多个Web项目

来源:互联网 发布:linux view 编辑:程序博客网 时间:2024/05/18 01:42

材料

  1. Apache2.4
  2. 一个或多个本地网站

流程

  • 打开Apache的httpd.conf配置文件[apache_dir/conf/httpd.conf]

  • 引用子配置文件httpd_vhosts.conf(将原本第二行有的注释”#”去掉即可)

# Virtual hostsInclude conf/extra/httpd-vhosts.conf
  • 打开Apache的子配置文件——httpd-vhosts.conf [apache_dir/conf/extra/httpd-vhosts.conf]

  • 然后在后面添加如下代码

<VirtualHost *:80>    # 管理员邮箱    ServerAdmin 3dkltsyt@gmail.com    # 网站根目录    DocumentRoot "C:/Develop/Prj/web/LMS"    # 网站域名    ServerName test2.dowhat.com    # log文件保存路径    ErrorLog "C:/ServerLogs/error.log"    CustomLog "C:/ServerLogs/access.log" common    # 该虚拟主机根目录权限相关设置    <Directory />        # 允许根目录中的.htaccess生效并覆盖此处设置        AllowOverride All        # 允许该目录所有的用户操作权限        Require all granted    </Directory></VirtualHost>
  • 如果还有一个项目需要挂载到Apache上,可以继续在httpd_vhosts.conf中继续添加:
<VirtualHost *:80>    ServerAdmin 3dkltsyt@gmail.com    # 另一个网站根目录    DocumentRoot "C:/Develop/Prj/web/ecnu_mind"    # 另一个网站的域名    ServerName test2.dowhat.com    ErrorLog "C:/ServerLogs/error.log"    CustomLog "C:/ServerLogs/access.log" common    <Directory />        AllowOverride All        Require all granted    </Directory></VirtualHost>
  • 然后打开C:\Windows\System32\drivers\etc\hosts(如果没有,则新建一个hosts文件,注意没有后缀),在文件最后加上:
127.0.0.1 test.dowhat.com127.0.0.1 test2.dowhat.com
  • 二者的端口都为80,通过这种本地dns解析(hosts文件)联合apache自动关联ServerName的方式,让这两个网站都独立工作,并且可以通过不同的域名(test.dowhat.com和test2.dowhat.com)登录两个网站。

  • 是不是看上去和真的网站一样呢?XD
    模拟真实网站


  • 最后附上我的.htaccess配置,因为是项目后台基于thinkPHP,有具体业务逻辑,所以这里就不赘述了。如果你的后台也是基于thinkPHP或者想了解一点点其他的apache相关配置,也可以看看。
# 允许在该目录下的所有符号链接(软连接,即文件中的连接路径,包括下面的URL重写所代表的路径,所以必须要有,同时不启用indexes以防止用户直接查看网站目录)Options FollowSymlinks# 默认索引文件为index.phpDirectoryIndex index.php<IfModule mod_rewrite.c>  # url重启模块开启  RewriteEngine On  # url重写条件匹配-d文件夹,-f文件    RewriteCond %{REQUEST_FILENAME} !-d    RewriteCond %{REQUEST_FILENAME} !-f  # 在URL中省略index.php  RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]</IfModule>
0 0
原创粉丝点击