Mac OS X 內置了Apache 和 PHP,這樣使用起來很是方便。本文以Mac OS X 10.6.3爲例。主要內容包括: 啓動Apache 運行PHP 安裝MySQL 使用phpMyAdmin 配置PHP的MCrypt擴展庫 設置虛擬主機 啓動Apachephp
有兩種方法: 打開「系統設置偏好(System Preferences)」 -> 「共享(Sharing)」 -> 「Web共享(Web Sharing)」 打開「終端(terminal)」,而後(注意:sudo須要的密碼就是系統的root賬號密碼) 運行「sudo apachectl start」,再輸入賬號密碼,這樣Apache就運行了。 運行「sudo apachectl -v」,你會看到Mac OS X 10.6.3中的Apache版本號: Server version: Apache/2.2.14 (Unix) Server built: Feb 11 2010 14:40:31 這樣在瀏覽器中輸入「http://localhost」,就能夠看到出現一個內容爲「It works!」的頁面,它位於「/Library(資源庫)/WebServer/Documents/」下,這是Apache的默認根目錄。 注意:開啓了Apache就是開啓了「Web共享」,這時聯網的用戶就 會經過「http://[本地IP]/」來訪問「/Library(資源庫)/WebServer/Documents/」目錄,經過「http: //[本地IP]/~[用戶名]」來訪問「/Users/[用戶名]/Sites/」目錄,能夠經過設置「系統偏好設置」的「安全(Security)」 中的「防火牆(Firewall)」來禁止這種訪問。 運行PHPcss
在終端中運行「sudo vi /etc/apache2/httpd.conf」,打開Apache的配置文件。(若是不習慣操做終端和vi的能夠設置在Finder中顯示全部的系統 隱藏文件,記得須要重啓Finder,這樣就能夠找到對應文件,爲所欲爲編輯了,但須要注意的是某些文件的修改須要開啓root賬號,但總體上仍是在終端 上使用sudo來臨時獲取root權限比較安全。) 找到「#LoadModule php5_module libexec/apache2/libphp5.so」,把前面的#號去掉,保存(在命令行輸入:w)並退出vi(在命令行輸入:q)。 運行「sudo cp /etc/php.ini.default /etc/php.ini」,這樣就能夠經過php.ini來配置各類PHP功能了。好比: ;經過下面兩項來調整PHP提交文件的最大值,好比phpMyAdmin中導入數據的最大值 upload_max_filesize = 2M post_max_size = 8M ;好比經過display_errors來控制是否顯示PHP程序的報錯 display_errors = Off 運行「sudo apachectl restart」,重啓Apache,這樣PHP就能夠用了。 運行「cp /Library/WebServer/Documents/index.html.en /Library/WebServer/Documents/info.php」,即在Apache的根目錄下複製index.html.en文件並重命 名爲info.php 在終端中運行「vi /Library/WebServer/Document/info.php」,這樣就能夠在vi中編輯info.php文件了。在「It’s works!」後面加上「<?php phpinfo(); ?>」,而後保存之。這樣就能夠在http://localhost/info.php中看到有關PHP的信息,好比版本號是5.3.1。 安裝MySQLhtml
因爲Mac OS X中並無預裝MySQL,因此須要本身手動安裝,目前MySQL的最穩定版本是5.1。MySQL提供了Mac OS X下的安裝說明。 下載MySQL 5.1。選擇合適的版本,好比這裏選擇的是mysql-5.1.46-osx10.6-x86_64.dmg。 運行dmg,會發現裏面有4個文件。首先點擊安裝mysql- 5.1.46-osx10.6-x86_64.pkg,這是MySQL的主安裝包。通常狀況下,安裝文件會自動把MySQL安裝到/usr/local下 的同名文件夾下。好比點擊運行「mysql-5.1.46-osx10.6-x86_64.dmg」會把MySQ安裝到「/usr/local /mysql-5.1.46-osx10.6-x86_64」中。一路默認安裝完畢便可。 點擊安裝第2個文件MySQLStartupItem.pkg,這樣MySQL就會自動在開機時自動啓動了。 點擊安裝第3個文件MySQL.prefPane,這樣就會在「系統設置偏好」中看到名爲「MySQL」的ICON,經過它就能夠設置MySQL開始仍是中止,以及是否開機時自動運行。到這裏MySQL就基本安裝完畢了。 經過運行「sudo vi /etc/bashrc」,在bash的配置文件中加入mysql和mysqladmin的別名 #mysql alias mysql='/usr/local/mysql/bin/mysql' alias mysqladmin='/usr/local/mysql/bin/mysqladmin' 這樣就能夠在終端中比較簡單地經過命令進行相應的操做,好比安裝完畢之 後MySQL的root默認密碼爲空,若是要設置密碼能夠在終端運行「mysqladmin -u root password "mysqlpassword"」來設置,其中mysqlpassword即root的密碼。更多相關內容能夠參考B.5.4.1. How to Reset the Root Password。 注意:Mac OS X的升級或者其餘緣由可能會致使MySQL啓動或者開機自動運行,在MySQL的操做面板上會提示「Warning:The /usr/local/mysql/data directory is not owned by the 'mysql' or '_mysql' 」,或者在命令行下提示「Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)」,這應該是某種狀況下致使/usr/local/mysql/data的宿主發生了改變,只須要運行「sudo chown -R mysql /usr/local/mysql/data」便可。 另外,使用PHP鏈接MySQL可能會報錯「Can’t connect to local MySQL server through socket ‘/var/mysql/mysql.sock’」,或者使用localhost沒法鏈接MySQL而須要127.0.0.1,緣由是鏈接時候php默認 去找/var/mysql/mysql.sock了,可是MAC版本的MYSQL改動了文件的位置,放在/tmp下了。處理辦法是按以下修改 php.ini: mysql.default_socket = /tmp/mysql.sock 使用phpMyAdminhtml5
phpMyAdmin是用PHP開發的管理MySQL的程序,很是的流行和實用。可以實用phpMyAdmin管理MySQL是檢驗前面幾步成果的很是有效方式。 下載phpMyAdmin。選擇合適的版本,好比我選擇的是phpMyAdmin-3.3.2-all-languages.tar.bz2這個版本。 把「下載(downloads)」中的phpMyAdmin-3.32-all-languages文件夾複製到「/Users/[用戶名]/Sites」中,名更名爲phpmyadmin。 複製「/Users/[用戶名]/Sites/phpmyadmin/」中的config.sample.inc.php,並命名爲config.inc.php 打開config.inc.php,作以下修改: 用於Cookie加密,隨意的長字符串 $cfg['blowfish_secret'] = '';mysql
當phpMyAdmin中出現「#2002 沒法登陸 MySQL 服務器」時, 請把localhost改爲127.0.0.1就ok了, 這是由於MySQL守護程序作了IP綁定(bind-address =127.0.0.1)形成的 $cfg['Servers'][$i]['host'] = 'localhost';css3
把false改爲true,這樣就能夠訪問無密碼的MySQL了, 即便MySQL設置了密碼也能夠這樣設置,而後在登陸phpMyAdmin時輸入密碼 $cfg['Servers'][$i]['AllowNoPassword'] = false; 這樣就能夠經過http://localhost/~[用戶名]/phpmyadmin訪問phpMyAdmin了。這個時候就看到一個提示「沒法加載 mcrypt 擴展,請檢查您的 PHP 配置。」,這就涉及到下一節安裝MCrypt擴展了。 配置PHP的MCrypt擴展web
MCrypt是一個功能強大的加密算法擴展庫,它包括有22種算法,phpMyAdmin依賴這個PHP擴展庫。可是它在Mac OS X下的安裝卻不那麼友善,具體以下: 下載並解壓libmcrypt-2.5.8.tar.bz2。 在終端執行以下命令(注意以下命令須要安裝xcode支持): cd ~/Downloads/libmcrypt-2.5.8/ ./configure --disable-posix-threads --enable-static make sudo make install 下載並解壓PHP源碼文件php-5.3.1.tar.bz2。Mac OS X 10.6.3中預裝的PHP版本是5.3.1,而如今最新的PHP版本是5.3.2,你須要依據本身的實際狀況選擇對應的版本。 在終端執行以下命令: cd ~/Downloads/php-5.3.1/ext/mcrypt phpize ./configure make cd modules sudo cp mcrypt.so /usr/lib/php/extensions/no-debug-non-zts-20090626/ 打開php.ini sudo vi /etc/php.ini 在php.ini中加入以下代碼,並保存後退出,而後重啓Apache extension=/usr/lib/php/extensions/no-debug-non-zts-20090626/mcrypt.so 當你再訪問http://localhost/~[用戶名]/phpmyadmin時,你會發現「沒法加載 mcrypt 擴展,請檢查您的 PHP 配置。」的提示沒有了,這就表示MCrypt擴展庫安裝成功了。 設置虛擬主機算法
在終端運行「sudo vi /etc/apache2/httpd.conf」,打開Apche的配置文件 在httpd.conf中找到「#Include /private/etc/apache2/extra/httpd-vhosts.conf」,去掉前面的「#」,保存並退出。 運行「sudo apachectl restart」,重啓Apache後就開啓了它的虛擬主機配置功能。 運行「sudo vi /etc/apache2/extra/httpd-vhosts.conf」,這樣就打開了配置虛擬主機的文件httpd-vhost.conf,配置你須要的虛擬主機了。須要注意的是該文件默認開啓了兩個做爲例子的虛擬主機: <VirtualHost *:80> ServerAdmin webmaster@dummy-host.example.com DocumentRoot "/usr/docs/dummy-host.example.com" ServerName dummy-host.example.com ErrorLog "/private/var/log/apache2/dummy-host.example.com-error_log" CustomLog "/private/var/log/apache2/dummy-host.example.com-access_log" common </VirtualHost> <VirtualHost *:80> ServerAdmin webmaster@dummy-host2.example.com DocumentRoot "/usr/docs/dummy-host2.example.com" ServerName dummy-host2.example.com ErrorLog "/private/var/log/apache2/dummy-host2.example.com-error_log" CustomLog "/private/var/log/apache2/dummy-host2.example.com-access_log" common </VirtualHost> 而實際上,這兩個虛擬主機是不存在的,在沒有配置任何其餘虛擬主機時,可能會致使訪問localhost時出現以下提示: Forbidden You don't have permission to access /index.php on this server 最簡單的辦法就是在它們每行前面加上#,註釋掉就行了,這樣既能參考又不致使其餘問題。 增長以下配置 <VirtualHost *:80> DocumentRoot "/Users/[用戶名]/Sites" ServerName sites ErrorLog "/private/var/log/apache2/sites-error_log" CustomLog "/private/var/log/apache2/sites-access_log" common </VirtualHost> 保存退出,並重啓Apache。 運行「sudo vi /etc/hosts」,打開hosts配置文件,加入」127.0.0.1 sites「,這樣就能夠配置完成sites虛擬主機了,這樣就能夠用「http://sites」訪問了,其內容和「http://localhost /~[用戶名]」徹底一致。 這是利用Mac OS X 10.6.3中原生支持的方式來實現的配置,也能夠參考「Mac OS X Leopard: 配置Apache, PHP, SQLite, MySQL, and phpMyAdmin(一) 」和「Mac OS X Leopard: 配置Apache, PHP, SQLite, MySQL, and phpMyAdmin(二) 」。實際上,你還可使用XAMPP或MacPorts這種第三方提供的集成方案來實現簡單的安裝和使用。 原文地址:http://hi.baidu.com/html5css3/item/024c1ab4d18bb59a18469746sql
補充:apache
If your personal web sharing is not working for a user but is OK at the localhost level, the issue may be a configuration file for the user.
The option to turn on web sharing in the System Preferences pane under 「Sharing」 may not turn to green or web sharing may work at the higher document root level of http://localhost/ and the page displays 「It Works」 but when going to the user level, http://localhost/~username/ you get the:
Forbidden 403, You don’t have permission to access /~username/ on this server
Check that you have a 「username.conf」 filed under:
/etc/apache2/users/ If you don’t then create one named by the short username of the account with the suffix .conf, its contents should be (swapping out the real username):
cd /etc/apache2/users sudo nano username.conf
Then add the content below swapping in your username:
<Directory "/Users/username/Sites/"> Options Indexes MultiViews AllowOverride All Order allow,deny Allow from all </Directory>
Permissions on the file should be:
-rw-r--r-- 1 root wheel 298 Jun 28 16:47 username.conf