©
This document uses PHP Chinese website manual Release
从 OS X 10.0.0 版本开始,PHP 作为 Mac 机的标准配置被提供。在默认的 web 服务器中启用 PHP,只需将 Apache 配置文件 httpd.conf 中的几行配置指令最前面的注释符号去掉,而 CGI 或 CLI 默认都可使用(可以很容易的被终端程序使用)。
按照以下的使用说明,可以快速的建立一个本地 PHP 开发环境。强烈建议将 PHP 升级到最新的版本。在大多数活跃的软件中, 新的版本会修复错误和添加新的功能,PHP 也是如此。请参见相应的 Mac OS X 安装文档,以进一步了解详细的信息。以下的说明以初学者的角度来详细描述如何操作来得到一个缺省的运行环境。建议所有的用户都编译或者安装一个新的打包版本。
标准的安装类型为 mod_php,在 Mac OS X 的 Apache web 服务器(默认 web 服务器,可以从系统设置中访问)中启用 PHP 包含以下的步骤:
Note: 要打开这个文件,可以在命令行下面使用基于 Unix 的文本编辑器,例如 nano,因为他的属主是 root,所以我们需要使用 sudo 来打开(以 root 用户权限)。例如我们在 Terminal 程序中敲入下面的指令(操作后,会提示输入密码):sudo nano /private/etc/apache2/httpd.conf 注意 nano 中的命令:^w(搜索),^o(保存),以及 ^x(退出)。^ 表示 Ctrl 键。
Note: 在Mac OS X 10.5之前的版本中捆绑的是旧版本的 PHP 和 Apache。因此在旧的计算机中 Apache 配置文件的位置可能是 /etc/httpd/httpd.conf。
使用文本的编辑器取消注释(删除前面的 #)看起来类似于下面的行(这两行常常不在一起,需要在文件中找到这两行):
# LoadModule php5_module libexec/httpd/libphp5.so# AddModule mod_php5.c
确保将所需要的文件扩展名解析为 PHP(例如:.php .html 以及 .inc),否则不能正常运行。
由于以下的配置已经写入 httpd.conf(自 Mac Panther 版起),一旦 PHP 被启用则 .php 文件会被自动解析为 PHP 脚本。
<IfModule mod_php5.c> # If php is turned on, we respect .php and .phps files. AddType application/x-httpd-php .php AddType application/x-httpd-php-source .phps # Since most users will want index.php to work we # also automatically enable index.php <IfModule mod_dir.c> DirectoryIndex index.html index.php </IfModule> </IfModule>
Note:
在 OS X 10.5(Leopard)以前版本中,捆绑的是 PHP 4 而不是 PHP 5,因此上面的配置指令稍有不同,需要将 5 更改为 4。
phpinfo() 将会显示PHP的相关系统信息。可以在 DocumentRoot 下创建一个 PHP 文件,其代码如下:
<?php phpinfo (); ?>
要重启Apache,可以在 shell 中执行 sudo apachectl graceful,也可以停止/启动 OS X 系统首选项中的“Personal Web Server”选项。默认情况下,从浏览器访问本地文件的 URL 一般类似于:http://localhost/info.php,或者使用:http://localhost/~yourusername/info.php 来访问用户自己 DocumentRoot 中的文件。
CLI(或者旧版本中的 CGI)一般文件名为 php ,其路径可能是 /usr/bin/php。打开一个终端,参考 PHP 手册中的 PHP 的命令行模式一章,然后执行 php -v 可以检查当前运行的 PHP 的版本。调用 phpinfo() 也会显示相关的信息。
[#1] parampal [2014-09-01 04:39:44]
instead of step 3, add the following lines to the `/private/etc/apache2/mime.types` file
application/x-httpd-php php
application/x-httpd-php-source phps
[#2] cweiske at cweiske dot de [2014-07-07 08:56:24]
On recent (10.8/10.9) macs, the apache php config file is located at /etc/apache2/others/php5.conf
[#3] Julee [2013-03-16 15:49:40]
To work on Mac OS X Lion 10.7, I had to change /private/etc/php.ini.default to /private/etc/php.ini
[#4] Anonymous [2008-11-15 02:36:28]
You only have to uncomment:
#LoadModule php5_module libexec/apache2/libphp5.so
This is gone:
# AddModule mod_php5.c
The statement in 3 was changed to:
<IfModule mime_module>
#
# TypesConfig points to the file containing the list of mappings from
# filename extension to MIME-type.
#
TypesConfig /private/etc/apache2/mime.types
#
# AddType allows you to add to or override the MIME configuration
# file specified in TypesConfig for specific file types.
#
#AddType application/x-gzip .tgz
#
# AddEncoding allows you to have certain browsers uncompress
# information on the fly. Note: Not all browsers support this.
#
#AddEncoding x-compress .Z
#AddEncoding x-gzip .gz .tgz
#
# If the AddEncoding directives above are commented-out, then you
# probably should define those extensions to indicate media types:
#
AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz
#
# AddHandler allows you to map certain file extensions to "handlers":
# actions unrelated to filetype. These can be either built into the server
# or added with the Action directive (see below)
#
# To use CGI scripts outside of ScriptAliased directories:
# (You will also need to add "ExecCGI" to the "Options" directive.)
#
#AddHandler cgi-script .cgi
# For type maps (negotiated resources):
#AddHandler type-map var
#
# Filters allow you to process content before it is sent to the client.
#
# To parse .shtml files for server-side includes (SSI):
# (You will also need to add "Includes" to the "Options" directive.)
#
#AddType text/html .shtml
#AddOutputFilter INCLUDES .shtml
</IfModule>
Extra MIME types can either be added to the file /private/etc/apache2/mime.types or by using an AddType directive as commented on above.
[#5] jaffle [2008-10-24 02:36:12]
Additionally, it seems that the default installation of php on Mac OS X (10.5 tested) does not point to the default location of the mysql.sock file that is installed in a standard MySQL 5.0 installation.
This prevents php working with locally hosted MySQL databases.
Adding the following line to the /private/etc/apache2/other/php5.conf file did the trick:
php_value mysql.default_socket /tmp/mysql.sock
(make sure you put it in between the </IfModule> statements)
[#6] sachin at dharmapurikar dot in [2008-10-21 17:28:58]
I am using MacOSX 10.5.4 and only enabling the mod_php5 wasn't enough to get the PHP support. I had to add following block before I could use php on machine -
<IfModule mod_php5.c>
# If php is turned on, we respect .php and .phps files.
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
# Since most users will want index.php to work we
# also automatically enable index.php
<IfModule mod_dir.c>
DirectoryIndex index.html index.php
</IfModule>
</IfModule>