前边文章讲了mac上搭建好了本地服务器,现在来搭建虚拟主机,使得本地也能以一个域名的形式访问服务器
1.参考上一篇搭建本地服务器,备份配置文件:http://blog.csdn.net/qq_25639809/article/details/77988470
2.编辑httpd.conf文件,输入命令:
sudo vim /etc/apache2/httpd.conf
将Include /private/etc/apache2/extra/httpd-vhosts.conf这行前的注释符号#去掉。
3.编辑httpd-vhosts.conf文件,输入命令:
vim /etc/apache2/extra/httpd-vhosts.conf
按i进入编辑模式,在httpd-vhosts.conf 中默认的两个VirtualHost删除或者加#注释
然后添加以下内容,beta.test.com可任意起名,注意对应自己的服务器路径(我将localhost对应sites文件夹,beta.test.com对应sites/test文件夹)
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot "/Users/lax/sites"
ServerName localhost
ErrorLog "/private/var/log/apache2/beta.test.com-error_log"
CustomLog "/private/var/log/apache2/beta.test.com-access_log" common
<Directory "/Users/lax/sites">
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerAdmin webmaster@wzrm.vxinghe.com
DocumentRoot "/Users/lax/sites/test"
ServerName beta.test.com
ErrorLog "/private/var/log/apache2/beta.test.com-error_log"
CustomLog "/private/var/log/apache2/beta.test.com-access_log" common
<Directory "/Users/lax/sites/test">
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
按esc,输入:wq回车保存退出
4.修改hosts文件,来将你的域名beta.test. com对应本地的IP,输入命令:
sudo vim /etc/hosts
添加一行内容:
127.0.0.1 beta.test.com
5.重启服务器
sudo apachectl restart
6.在浏览器中访问localhost和beta.test.com查看