XAMPP 二级域名配置

12/26/2017

一直以来Linux下习惯了用Nginx,最近团队使用了Windowx服务器,而本地Win7我都是用xampp,现在新服务器想想也用这个吧,接着需要配置,总结了一下:

找到安装目录下的xampp\apache\conf\httpd.conf文件,默认xampp\htdocs为根目录,配置二级域名你可以在httpd.conf文件中新增代码也可以在apache/conf/extra/httpd-vhosts.conf文件配置,下面讲解从httpd-vhosts.conf文件配置。

1、打开xampp\apache\conf\httpd.conf文件,搜索 “Include conf/extra/httpd-vhosts.conf”,确保前面没有 # 注释符,即是引入了 vhosts 虚拟主机配置文件,开启了httpd-vhosts.conf,此时默认的httpd.conf默认配置失效(见最后);

2、在httpd-vhosts.conf最后新增:Include “conf/extra/vhostlist/www.wei-blog.com.conf”,这样每次有新虚拟目录只用增加一句Include,再新增对应文件,这样避免httpd-vhosts.conf字符太多,容易出错吧,个人习惯。

3、新增www.abc.com.conf文件,内容如下

<VirtualHost *:80>
  ServerAdmin admin@wei-blog.com
  DocumentRoot “D:\xampp\htdocs\blog”
  ServerName wei-blog.com
  ServerAlias www.wei-blog.com
  ErrorLog “D:\xampp\apache\logs\abc.localhost-error.log”
  CustomLog “D:\xampp\apache\logs\abc.localhost.access.log” combined
</VirtualHost>
参数含义说明
ServerAdmin     管理员邮箱
DocumentRoot 所需指向路径
ServerName     域名名称
ServerAlias       域名别名 可要可不要的参数
ErrorLog           错误日志

CustomLog      访问日志

这里有一个问题,如果我项目程序不在D:\xampp\htdocs下,而在E:\blog下,那如何配置呢,有2中方法,如下:

方法一:
<VirtualHost *:80>
  ServerAdmin admin@wei-blog.com
  DocumentRoot “E:\blog”
  ServerName wei-blog.com
  ServerAlias www.wei-blog.com
  ErrorLog “E:\xampp\apache\logs\abc.localhost-error.log”
  CustomLog “E:\xampp\apache\logs\abc.localhost.access.log” combined

  <Directory E:\blog>
     Order deny,allow
     Allow from all
   </Direcotry>
</VirtualHost>

即新增一个Directory节点来允许目录可访问。注意:E:\blog 文件夹的最后不需要再加”\”;

方法二:
直接修改xampp\apache\conf\httpd.conf文件,将DocumentRoot “D:/xampp/htdocs”修改为DocumentRoot E:\blog,找到<Directory “D:/xampp/htdocs”> 修改为 <Directory “E:/blog”>。

最后重启Apache。

—————————————————————————————————-

如1中所说,当虚拟主机配置生效时,默认httpd.conf文件中的配置是没用的,此时httpd-vhosts.conf中的第一个站点配置将成为默认配置,即默认发现使用localhost直接定位到了\xampp\htdocs\blog下的内容,而我期望的是还是定位到/xampp/htdocs/的内容,解决防方法就是把localhost的配置在httpd-vhosts.conf里配置回来。

再在文件的最后添加上如下内容,并重启Apache.

<VirtualHost *:80>
  ServerAdmin admin@wei-blog.com
  DocumentRoot “D:\xampp\htdocs\”
  ServerName wei-blog.com
  ServerAlias www.wei-blog.com
  ErrorLog “D:\xampp\apache\logs\abc.localhost-error.log”
  CustomLog “D:\xampp\apache\logs\abc.localhost.access.log” combined
</VirtualHost>