mac系統使用內置的 PHP


   從 OS X 10.0.0 版本開始,PHP 作為 Mac 機的標准配置被提供。在默認的 web 服務器中啟用 PHP,只需將 Apache 配置文件 httpd.conf 中的幾行配置指令最前面的注釋符號去掉,而 CGICLI 默認都可使用(可以很容易的被終端程序使用)。

按照以下的使用說明,可以快速的建立一個本地 PHP 開發環境。強烈建議將 PHP 升級到最新的版本。在大多數活躍的軟件中, 新的版本會修復錯誤和添加新的功能,PHP 也是如此。請參見相應的 Mac OS X 安裝文檔,以進一步了解詳細的信息。以下的說明以初學者的角度來詳細描述如何操作來得到一個缺省的運行環境。建議所有的用戶都編譯或者安裝一個新的打包版本。

標准的安裝類型為 mod_php,在 Mac OS X 的 Apache web 服務器(默認 web 服務器,可以從系統設置中訪問)中啟用 PHP 包含以下的步驟:

 

  1. 找到並打開Apache的配置文件。默認情況下,這個配置文件的位置是: /private/etc/apache2/httpd.conf 使用 Finder 或者 Spotlight 來找到這個文件可能不是很容易的事情,因為在默認情況下它一般是 root 用戶擁有所有權的私有文件。

    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

  2. 使用文本的編輯器取消注釋(刪除前面的 #)看起來類似於下面的行(這兩行常常不在一起,需要在文件中找到這兩行):

    # LoadModule php5_module libexec/httpd/libphp5.so
    
    # AddModule mod_php5.c
    
    注意位置/路徑。如果在以后重新編譯了 PHP,以上文件應被更換或者注釋掉。
  3. 確保將所需要的文件擴展名解析為 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。

  4. 確保 DirectoryIndex 加載了所需的默認索引文件。 這個也是在 httpd.conf 中設置的。 通常情況下使用 index.phpindex.html 。默認情況下 index.php 會被啟用,因為在我們上面的配置指令中寫明了。根據實際情況可以做相應的調整。
  5. 設置 php.ini 的位置或者使用默認的位置。 Mac OS X 上通常默認的位置是 /usr/local/php/php.ini ,調用 phpinfo() 也可以得到此信息。如果沒有使用 php.ini,PHP 將使用所有的默認值。參見常見問題中的尋找 php.ini
  6. 定位或者設置 DocumentRoot 這是網站所有文件的根目錄。此目錄中的文件由 web 服務器提供服務,從而使得 PHP 文件將在輸出到瀏覽器之前解析為 PHP 腳本。通常情況下默認的路徑是 /Library/WebServer/Documents,但是可以根據需要在 httpd.conf中設置為任何其他目錄。另外,用戶自己的缺省 DocumentRoot/Users/yourusername/Sites
  7. 創建一個 phpinfo() 文件。

    phpinfo() 將會顯示PHP的相關系統信息。可以在 DocumentRoot 下創建一個 PHP 文件,其代碼如下:

    <?php phpinfo(); ?>
  8. 重啟 Apache,然后從瀏覽器訪問上面創建的文件。

    要重啟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() 也會顯示相關的信息。


User Contributed Notes 4 notes

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>
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM