当前位置: 移动技术网 > IT编程>开发语言>PHP > Mac配置虚拟主机

Mac配置虚拟主机

2019年03月23日  | 移动技术网IT编程  | 我要评论

一、启动apache

  终端输入:sudo apachectl start

apache的安装目录在:/etc/apache2/,etc默认是隐藏的。有三种方式查看: 

1.桌面位于finder时:shift+command+g,输入“/etc”(即finder-前往-前往文件夹的快捷键)

2.打开终端:cd /etc

3.可以在terminal 输入 "open /etc"

二、设置虚拟主机 

1.在终端运行“sudo vi /etc/apache2/httpd.conf”,打开apche的配置文件 

2.在httpd.conf中找到“#include /private/etc/apache2/extra/httpd-vhosts.conf”,去掉前面的“#”,保存并退出。 

3.运行“sudo apachectl restart”,重启apache后就开启了虚拟主机配置功能。 

4.运行“sudo vi /etc/apache2/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>

而实际上,这两个虚拟主机是不存在的,在没有配置任何其他虚拟主机时,可能会导致访问localhost时出现如下提示: 
forbidden 
you don't have permission to access /index.php on this server

最简单的办法就是在它们每行前面加上#,注释掉就好了,这样既能参考又不导致其他问题。

5.增加如下配置,支持localhost访问,添加虚拟主机访问

<virtualhost *:80> 
documentroot "/library/webserver/documents" 
servername localhost 
errorlog "/private/var/log/apache2/localhost-error_log" 
customlog "/private/var/log/apache2/localhost-access_log" common 
</virtualhost> 
<virtualhost *:80> 
documentroot "/library/webserver/documents" 
servername 虚拟主机地址 (如:www.test.com)
errorlog "/private/var/log/apache2/test-error_log" 
customlog "/private/var/log/apache2/test-access_log" common 
<directory /> 
options indexes followsymlinks multiviews 
allowoverride none 
order deny,allow 
allow from all 
</directory> 
</virtualhost>

保存,退出,重启apache。

6.运行“sudo vi /etc/hosts”,打开hosts配置文件,加入"127.0.0.1 www.test.com",这样就可以配置完成test虚拟主机了。

打开浏览器,输入:www.test.com

it works!

配置成功,和localhost一致!

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网