1.设置域名
打开Finder,command + shift + G ,搜索框内输入/etc ,也就是查找系统的根目录,找到一个叫hosts的文件,用文本编辑器打开。
默认情况下,他们的根目录都是你在/etc/apache2/httpd.conf文件中设置的根目录
DocumentRoot "/Users/edz/Sites(你自己设置的根目录)"
<Directory "/Users/edz/Sites">
如果不知道httpd.conf中怎么设置,请查看,http://blog.csdn.net/qq_31989521/article/details/50773492
或者我的第一篇 php开发环境搭建(汇总)
2.虚拟主机
(1).看你的php版本(终端里输入 php -version ),如果你是最新的,在httpd.conf文件中查找:php7_module(版本号对应),将此行代码#删除
LoadModule php7_module /usr/local/php5-7.1.0-20161202-092124/libphp7.so
(如果想用最新的配置环境,见更新PHP版本+appache的相应修改,里面也说了一下更新版本会遇到的坑)
(2)最重要的一步到来了,在/etc/apache2/extra/httpd-vhosts.conf,设置虚拟主机
<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>
一个域名可以对应一个虚拟主机,多个域名也可以对应一个主机。
注意:每次从新设置了,httpd-vhost.conf文件之后,最好,重启一下appach。(方法:打开终端,输入 sudo apachectl -k restart),加上-k,可以监听到,在配置文件中是否有错误。
下面是我创建的上面三个域名对应的虚拟主机
<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>