如何在OS X Yosemite 配置php web站点及虚拟主机?

来源:互联网 发布:网络用语qq爱是什么 编辑:程序博客网 时间:2024/05/02 04:53

本人刚接触 Mac os (Yosemite 10.10.5)如何配置apache web服务,经网上找资料及实践,成功配置了php web站点及虚拟主机,可对初学者借鉴参考,现纪录如下。


1、在OS X Yosemite打开终端运行 sudo  apachectl start ,之后在地址栏输入 http://localhost ,页面显示 It works!  ,说明运行成功!OS X Yosemite自带安装 apache ,可以终端输入:httpd -v 或 apachectl -v 查看版本:Server version: Apache/2.4.16 (Unix)


2、要使站点支持php ,只要在httpd.conf 开启php开关就可以了。步骤:终端输入open /etc 回车,自动弹出窗口找到 apache2文件夹,打开此文夹找到 httpd.conf ,编辑此文件找到 #LoadModule php5_module libexec/apache2/libphp5.so (httd.conf 文件中第169行),把此句前边#去除取消注释,保存文件修改。在终端输入 sudo apachectl restart 重启apache服务。若要配置php各种功能,可在php配置文件/etc/php.ini.default 复制一份命名为  /etc/php.ini即可配置PHP功能。


3、测试php是否成功开启,进入默认站点,此站点路径为:/Library/WebServer/Documents (httd.conf 文件中第236行),进入站点文件夹,终端输入:open  /Library/WebServer/Documents ,在此文件建一文件test.php,内容为:

 

<?phpphpinfo();?>

 

在浏览器地址栏输入 http://localhost/test.php 将出现 PHP Version 5.5.27 说明配置成功!

以上几个步骤很容易就配置好web站点。一般情况默认站点不方便管理,当你把默认站点的路径/Library/WebServer/Documents  改成为 /Users/username/Documents/mytest,此时httd.conf 文件236行237行代码如下:

DocumentRoot /Users/username/Documents/mytest<Directory /Users/username/Documents/mytest">


同时在 /Users/username/Documents/mytest 目录下放一个php测试文件如上面的 test.php,重启apache服务,当你输入地址栏 http://localhost/test.php,问题来了,页面出现为 403 Forbidden,You don't have permission to access /test.php on this server.这是咋回事呢?网上经常见到 Deny from all,应改为 allow from all,由于之前默认站点可以访问,说明web配置没有问题,猜想是文件访问权限受限。网上说只要把mytest权限改为777即可,通过终端输入 chmod 777 /Users/username/Documents/mytest,打开测试页 test.php 还是出现 403 Forbidden,说明此路不通。 后来发现 httd.conf 中IfModule unixd_module 节点中有 User _www ,Group _www (httd.conf 文件中第181,182行),我把这两行注释掉,在前加#

#User _www#Group _www

 

重启apache服务后,403 Forbidden就消失了。说明这样可行。

 

前面完成后,web站点就可以随心所欲为我们工作了。接下来如何配置虚拟主机。为了方便,我复制一份mytest为mytest2作为虚拟主机目录。


4、配置虚拟主机:打开 httd.conf 找到499行,#Include /private/etc/apache2/extra/httpd-vhosts.conf,把此行#去除取消注释,保存httd.conf文件。接下来打开/private/etc/apache2/extra/httpd-vhosts.conf 此文件,注释原有内容,本人是初次配置虚拟主机,网上找了下,复制代码添加内容到httpd-vhosts.conf最后:


 

<VirtualHost *:80>   <strong> ServerName test</strong>    DocumentRoot /Users/username/Documents/mytest2    <Directory "/Users/username/Documents/mytest2"></span> Options Indexes MultiViews    AllowOverride AuthConfig Limit    Order allow,deny    Allow from all    </Directory></VirtualHost> 

 

保存此文件,同时打开 /private/etc/hosts 内容最后输入 

127.0.0.1localhost255.255.255.255broadcasthost::1             localhost 127.0.0.1test


保存文件后,重启apach服务。 当你在地址栏输入 http://test/test.php 时,页面出现了:403 Forbidden ,这是为何呢?查找了资料,虚拟主机配置文件有误,我们可以从 httd.conf里复制。找到网站配置DocumentRoot,<Directory 节点(httd.conf 文件中第236行左右)复制配制相关站点内容,改成如下:

<VirtualHost *:80>    ServerName test    DocumentRoot /Users/username/Documents/mytest2<Directory "/Users/username/Documents/mytest2"></span>    Options FollowSymLinks Multiviews    MultiviewsMatch Any    AllowOverride FileInfo AuthConfig Limit    AllowOverride None    Require all granted</Directory></VirtualHost> 

 

保存文件后重启apach服务。在地址栏输入http://test/test.php 时,页面403 Forbidden消失,出现 PHP Version 5.5.27 说明虚拟主机配置成功!


为什么网上代码在我这里就不行了,原来apache版本的不同,OS X Yosemite(10.10.5)下,Server version: Apache/2.4.16 (Unix) 与 apache 2.2 配置有差异的。可参考一下文章:Apache2.2和Apache2.4中httpd.conf配置文件的异同。


 

0 0
原创粉丝点击