Mac下apache多站点配置笔记
本文纯属个人笔记,毫无指导倾向和价值!
折腾了一下apache的多站点配置,留些笔记。
纯属个人备忘,毫无权威的指导建议,LOL
-
先说文档根路径
在 httpd.conf 文件(默认位置在 /private/etc/apache2/ 下)中:
DocumentRoot "/Library/webServer/Documents"
<Directory "/Library/webServer/Documents">
以上为默认根路径(默认入口)。当在浏览器地址栏输入 localhost 时,效果如下:

在 /Library/webServer/Documents 这个路径下,有一个index.html.en的文件,上面的 "It works!" 就是由这个文件输出。
我们可以在此路径下新建目录(名字随意~),在目录中新建index.html文件,像下面这样:


如此,在浏览器地址栏输入 localhost/SingPageDemo 时,展现的内容便是由新建的这个 index.html 输出的内容了。
这就是根目录的用处,这很好理解。
-
也可以更改根路径为我们喜欢的路径
先用"#"注释掉默认根路径,然后添加自己的路径(此处我添加的是"/Users/willli/Sites"
)即可:
#DocumentRoot "/Library/webServer/Documents"
#<Directory "/Library/webServer/Documents">
DocumentRoot "/Users/willli/Sites"
<Directory "/Users/willli/Sites">
自建的根路径用法和默认根路径(大致上)没什么差。
-
再说多站点配置的方法:
还是看 httpd.conf 这个文件,找到:
# Virtual hosts
# Include /private/etc/apache2/extra/httpd-vhosts.conf
把 Include 之前的“#”去掉(也就是解除了这一行的注释~)
这样我们便开启了虚拟端口。我们刚刚设置好的这样那样的路径也就不再作用了,此时在浏览器地址栏输入 localhost ,是Not Found:

接下来我们配置虚拟端口上的地址。
找到 httpd-vhosts.conf 这个文件 (默认在 /private/etc/apache2/extra/ 下)。文件中已经给出了例子,照抄一下就OK了~
DocumentRoot 就是站点资源所在的路径,
ServeName 就是我们自己起的名字(域名)
<VirtualHost *:80>
ServerAdmin webmaster@dummy-host2.example.com
DocumentRoot "/Users/willli/Sites/demo"
ServerName www.demo.com
ErrorLog "/private/var/log/apache2/dummy-host2.example.com-error_log"
CustomLog "/private/var/log/apache2/dummy-host2.example.com-access_log" common
</VirtualHost>
最后一步,找到 private/etc/hosts 这个文件,将 ServerName 映射到本机的IP地址(127.0.0.1)
127.0.0.1 www.budiu.com
这时,在浏览器地址栏输入 www.demo.com ,内容就是 /Users/willli/Sites/demo 路径下 index.html 的输出内容了。
可以添加多个:
<VirtualHost *:80>
ServerAdmin webmaster@dummy-host2.example.com
DocumentRoot "/Users/willli/Sites/demoA"
ServerName www.demoa.com
ErrorLog "/private/var/log/apache2/dummy-host2.example.com-error_log"
CustomLog "/private/var/log/apache2/dummy-host2.example.com-access_log" common
</VirtualHost>
<VirtualHost *:80>
ServerAdmin webmaster@dummy-host2.example.com
DocumentRoot "/Users/willli/Sites/demoB"
ServerName www.demob.com
ErrorLog "/private/var/log/apache2/dummy-host2.example.com-error_log"
CustomLog "/private/var/log/apache2/dummy-host2.example.com-access_log" common
</VirtualHost>
<VirtualHost *:80>
ServerAdmin webmaster@dummy-host2.example.com
DocumentRoot "/Users/willli/Sites/demoC"
ServerName www.democ.com
ErrorLog "/private/var/log/apache2/dummy-host2.example.com-error_log"
CustomLog "/private/var/log/apache2/dummy-host2.example.com-access_log" common
</VirtualHost>
只要对应更改在 hosts 中的IP映射就可以通过域名来访问站点了。
大致如此。
一点儿补充:
- 更改那些文件后要重启我们的 apache ,
命令行(macOS下)操作的话,三个基本命令分别是:启动,停止,重启:
sudo apachectl start
sudo apachectl stop
sudo apachectl restart
- 至于为啥要地配置这些设置,我想无非就是想通过域名来快速访问我们本地的站点咯(毕竟我们在本地测试自己站点的时候,频繁输入很麻烦的)。
这些配置替代的是类似 localhost/demo 或是 localhost/demo/view/index.html 这样输入。如果可以接受这样的输入,大可不必折腾这么多。
当然,其实很多时候折腾不就是为了想弄清到底咋回事儿么~
玩脱了之后的一些补充,就当做是个人最佳实践吧:
127.0.0.1 和个人的 IP 地址还是要预留出来。

👆 就是把这两个路径留出来,并且虚拟端口开关关闭掉👇

这样做的好处是,随时有想要测试的页面效果啊,可以随时丢到 /Library/webServer/Documents 或者 /Users/willli/Sites 中来查看(访问地址即:localhost/mysite)。
那么什么时候用到虚拟端口对域名映射这样的设置呢?——当然就是你好长一段时间要和某个项目打交道的时候(就会方便些)。
现在,又学会了一个新发访问方法:通过本地 IP 地址访问。
在 httpd-vhosts.conf 中添加配置:

如此,只要在 DocumentRoot 一行更改项目路径就可以方便访问了。
这种方式的好处是,方便生成一个手机可扫并可访问的二维码。