MacOS 下 XAMPP配置基于域名的虚拟主机

第一步

在 MAC 的 /Applications/XAMPP/xamppfiles/etc 打开 httpd.conf 文件.

然后搜索 httpd-vhosts.conf 去掉前边的 # 注释符号

《MacOS 下 XAMPP配置基于域名的虚拟主机》

第二步

在 MAC 的 /Applications/XAMPP/xamppfiles/etc/extra 打开 httpd-vhosts.conf 文件.
打开了配置虚拟主机的文件 httpd-vhost.conf 后就可以配置你需要的虚拟主机了。注意的是该文件默认开启了两个作为例子的虚拟主机

这两个虚拟主机是不存在的,它们每行前面加上#,注释掉就好了,这样既能参考又避免导致其他问题。

<VirtualHost *:80>
ServerAdmin webmaster@dummy-host.example.com
DocumentRoot "/usr/docs/dummy-host.example.com"
ServerName dummy-host.example.com
ErrorLog "/private/var/log/apache2/dummy-host.example.com-error_log"
CustomLog "/private/var/log/apache2/dummy-host.example.com-access_log" common
</VirtualHost>
<VirtualHost *:80>
ServerAdmin webmaster@dummy-host2.example.com
DocumentRoot "/usr/docs/dummy-host2.example.com"
ServerName dummy-host2.example.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 nansong.com
    DocumentRoot "/Applications/XAMPP/xamppfiles/htdocs/0316/public"

    <Directory "/Applications/XAMPP/xamppfiles/htdocs/0316/public">
        #Options Indexes FollowSymLinks ExecCGI Includes #don't permission see list
        Options All
        #Options FollowSymLinks
        AllowOverride all
        Order allow,deny
        Allow from all
    </Directory>
    ServerName nansong.com
    ServerAlias www.nansong.com
    ErrorLog "/Applications/XAMPP/xamppfiles/htdocs/0316/test_error_log"
    CustomLog "/Applications/XAMPP/xamppfiles/htdocs/0316/test_access_log" common
</VirtualHost>

说明:
“Options All”是允许目录浏览,有安全性风险,适合用于个人调试程序,需注意当站点根目录含index.html页面时,会默认打开网页,而不是目录列表,因此此模式需删除index.html.
“Options Indexes FollowSymLinks ExecCGI Includes”是不允许目录浏览,适合正式站点

如图
《MacOS 下 XAMPP配置基于域名的虚拟主机》
重启Apache。

第三步

运行终端 输入 “sudo vi /etc/hosts”,打开hosts配置文件,加入”127.0.0.1 your.website.com“

sudo vi /etc/hosts

打开的是 vim 编辑器

需要用到的 vim 命令
命令模式下按下 i 就进入了输入模式。
在输入模式按 ESC 就进入 命令行模式
按:q! 若曾修改过文件,又不想存储,使用!为强制离开不存储文件
按:w! 保存并退出
vim命令参考网址
http://www.cnblogs.com/jeakon/archive/2012/08/13/2816802.html
https://zh.wikibooks.org/zh-hans/Vim/%E4%B8%89%E7%A7%8D%E6%A8%A1%E5%BC%8F

《MacOS 下 XAMPP配置基于域名的虚拟主机》

这样就可以配置完成sites虚拟主机了,这样就可以用 “http://your.website.com” 访问了,
其内容和 “http://localhost/” 完全一致
但是会有一点问题就是 localhost 无法访问了

第四步

我们再次在 MAC 的 /Applications/XAMPP/xamppfiles/etc/extra 打开 httpd-vhosts.conf 文件.
添加

<VirtualHost *:80>
    DocumentRoot "/Applications/XAMPP/xamppfiles/htdocs/"
    ServerName localhost
</VirtualHost>

《MacOS 下 XAMPP配置基于域名的虚拟主机》

修改完成后再次进行第三步,将localhost指向127.0.0.1地址

    原文作者:南宋X
    原文地址: https://segmentfault.com/a/1190000008719139
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞