在Apache/Mountain Lion上本地运行html嵌入式php


Running html-embedded php on Apache/Mountain Lion locally

在OS X Mountain Lion上预装了Apache和php后,以下是我认为足以在本地运行服务器的步骤,而无需安装MAMP(会安装重复的):

  1. 取消对/etc/apache2/httpd.conf中的行LoadModule php5_module libexec/apache2/libphp5.so的注释
  2. 添加适当的/etc/apache2/users/calaf.conf
  3. 将上面的php示例保存在~/Sites/Introdutory-example.html中(更正
  4. 通过运行sudo apachectl start(或sudo apachectl restart)重新启动Apache
  5. 将包含<html><head><title>Example</title></head><body><php? echo "Hi"; ?></body></html>(或者更确切地说,是这个)的文件保存两次——第一次保存在/Library/WebServer/Documents/introductory-example.html中,另一次保存在~(whoami)/Sites/introductory-example.html中)

我现在希望指向http://localhost/introductory-example.htmlhttp://localhost/~calaf/introductory-example.html会给我一个具有两个功能的页面:

  1. 页面的标题是"示例"
  2. 页面的正文是"嗨。"

无论我是在/Library/WebServer/Documents还是在~/Sites/中看introductory-example.html,我都得到了第一个(标题是正确的),但没有得到第二个(正文是空的)——为什么?

我将symlink/Library/WebServer/Documents放入主文件夹,并将apache更改为以我的用户身份运行,而不是以默认用户身份运行。此外,您还需要为大多数PHP代码启用htaccess(我认为将AllowOverride设置为"All"),并可能安装MySQL(官方pkg安装将使用苹果预装的PHP开箱即用)。

如果您对配置apache感到不舒服,那么MAMP是个好主意。

据我所知,PHP并没有在系统范围内激活。无论如何,它不在我尝试过的任何系统和/或组合中。Apache只为特定文件夹(及其子文件夹)中的文件运行PHP。标准的apache文件夹应该位于:

/图书馆/WebServer/Documents/

虽然我自己还没有试过,但这本指南应该会让你振作起来。

编辑:

您需要将包含PHP代码的文件保存在.PHP中,而不是.html中。重命名或重新保存文件至介绍性示例.PHP

以下URL不起作用,因为路径不存在。

 http://localhost/Users/calaf/Sites/introductory-example.html

使用以下URL(您可能需要删除波浪号~)

http://localhost/~calaf/introductory-example.php
相关文章: