1.設(shè)置域名
打開(kāi)Finder,command + shift + G ,搜索框內(nèi)輸入/etc ,也就是查找系統(tǒng)的根目錄,找到一個(gè)叫hosts的文件,用文本編輯器打開(kāi)。

默認(rèn)情況下,他們的根目錄都是你在/etc/apache2/httpd.conf文件中設(shè)置的根目錄
DocumentRoot "/Users/edz/Sites(你自己設(shè)置的根目錄)"
<Directory "/Users/edz/Sites">
如果不知道httpd.conf中怎么設(shè)置,請(qǐng)查看,http://blog.csdn.net/qq_31989521/article/details/50773492
或者我的第一篇 php開(kāi)發(fā)環(huán)境搭建(匯總)
2.虛擬主機(jī)
(1).看你的php版本(終端里輸入 php -version ),如果你是最新的,在httpd.conf文件中查找:php7_module(版本號(hào)對(duì)應(yīng)),將此行代碼#刪除
LoadModule php7_module /usr/local/php5-7.1.0-20161202-092124/libphp7.so
(如果想用最新的配置環(huán)境,見(jiàn)更新PHP版本+appache的相應(yīng)修改,里面也說(shuō)了一下更新版本會(huì)遇到的坑)
(2)最重要的一步到來(lái)了,在/etc/apache2/extra/httpd-vhosts.conf,設(shè)置虛擬主機(jī)
<VirtualHost *:80>
ServerAdmin webmaster@dummy-host.example.com
DocumentRoot "/Users/edz/Sites"
ServerName localhost
ServerAlias 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
#DirectoryIndex info.php
<Directory />
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order deny,allow
Allow from all
</Directory>
</VirtualHost>

一個(gè)域名可以對(duì)應(yīng)一個(gè)虛擬主機(jī),多個(gè)域名也可以對(duì)應(yīng)一個(gè)主機(jī)。
注意:每次從新設(shè)置了,httpd-vhost.conf文件之后,最好,重啟一下appach。(方法:打開(kāi)終端,輸入 sudo apachectl -k restart),加上-k,可以監(jiān)聽(tīng)到,在配置文件中是否有錯(cuò)誤。
下面是我創(chuàng)建的上面三個(gè)域名對(duì)應(yīng)的虛擬主機(jī)
<VirtualHost *:80>
ServerAdmin webmaster@dummy-host.example.com
DocumentRoot "/Users/edz/Sites"
ServerName localhost
ServerAlias 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
#DirectoryIndex info.php
<Directory />
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerAdmin webmaster@dummy-host.example.com
DocumentRoot "/Users/edz/web1"
ServerName dev.php.com
ErrorLog "/private/var/log/apache2/dummy-host2.example.com-error_log"
CustomLog "/private/var/log/apache2/dummy-host2.example.com-access_log" common
DirectoryIndex info.php
<Directory />
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerAdmin webmaster@dummy-host.example.com
DocumentRoot "/Users/edz/web2"
ServerName www.2017.com
#ErrorLog "/private/var/log/apache2/dummy-host2.example.com-error_log"
#CustomLog "/private/var/log/apache2/dummy-host2.example.com-access_log" common
DirectoryIndex formlist.html
<Directory />
Options Indexes
AllowOverride None
Order deny,allow
Allow from all
</Directory>
</VirtualHost>