To work on Mac OS X Lion 10.7, I had to change /private/etc/php.ini.default to /private/etc/php.ini
從 OS X 10.0.0 版本開始,PHP 做爲 Mac 機的標準配置被提供。在默認的 web 服務器中啓用 PHP,只需將 Apache 配置文件 httpd.conf 中的幾行配置指令最前面的註釋符號去掉,而 CGI 或 CLI 默認均可使用(能夠很容易的被終端程序使用)。php
按照如下的使用說明,能夠快速的創建一個本地 PHP 開發環境。強烈建議將 PHP 升級到最新的版本。在大多數活躍的軟件中, 新的版本會修復錯誤和添加新的功能,PHP 也是如此。請參見相應的 Mac OS X 安裝文檔,以進一步瞭解詳細的信息。如下的說明以初學者的角度來詳細描述如何操做來獲得一個缺省的運行環境。建議全部的用戶都編譯或者安裝一個新的打包版本。html
標準的安裝類型爲 mod_php,在 Mac OS X 的 Apache web 服務器(默認 web 服務器,能夠從系統設置中訪問)中啓用 PHP 包含如下的步驟:mysql
Note: 要打開這個文件,能夠在命令行下面使用基於 Unix 的文本編輯器,例如 nano,由於他的屬主是 root,因此咱們須要使用 sudo 來打開(以 root 用戶權限)。例如咱們在 Terminal 程序中敲入下面的指令(操做後,會提示輸入密碼):sudo nano /private/etc/apache2/httpd.conf 注意 nano 中的命令:^w(搜索),^o(保存),以及 ^x(退出)。^ 表示 Ctrl 鍵。 web
Note: 在Mac OS X 10.5以前的版本中捆綁的是舊版本的 PHP 和 Apache。所以在舊的計算機中 Apache 配置文件的位置多是 /etc/httpd/httpd.conf。 sql
使用文本的編輯器取消註釋(刪除前面的 #)看起來相似於下面的行(這兩行經常不在一塊兒,須要在文件中找到這兩行):shell
# LoadModule php5_module libexec/httpd/libphp5.so # AddModule mod_php5.c
確保將所須要的文件擴展名解析爲 PHP(例如:.php .html 以及 .inc),不然不能正常運行。macos
因爲如下的配置已經寫入 httpd.conf(自 Mac Panther 版起),一旦 PHP 被啓用則 .php 文件會被自動解析爲 PHP 腳本。apache
<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() 也會顯示相關的信息。
To work on Mac OS X Lion 10.7, I had to change /private/etc/php.ini.default to /private/etc/php.ini
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.
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)
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>