要配置的PHP環(huán)境是Apache,PHP和MySQL的軟件組合子檀,由Apache提供服務(wù),MySQL提供數(shù)據(jù)庫支持嚼摩,PHP作為開發(fā)語言壹瘟,在此基礎(chǔ)上開發(fā)人員可以進行PHP腳本的編寫與運行。
在Mac OS X中银室,Apache是自帶的涂佃,只需要對其進行配置励翼,讓它能夠支持PHP的運行就可以了。具體步驟如下:
- 打開命令行終端辜荠,輸入如下命令:
sudo su -
這樣就可以以root用戶的角色來執(zhí)行之后的命令汽抚,以此來保證每條命令的執(zhí)行都不會有權(quán)限問題。
- 確保Apache是在運行中伯病,可以執(zhí)行如下命令:
apachectl start
可以打開瀏覽器造烁,訪問localhost來判斷Apache是否已經(jīng)正常啟動。
- 配置Apache來讓其能夠支持PHP的運行
1 執(zhí)行如下命令:
cd /etc/apache2/
vim httpd.conf
找到Apache以模塊方式加載PHP的配置午笛,如下:
#LoadModule php5_module libexec/apache2/libphp5.so
把前面的#去掉惭蟋,如下:
LoadModule php5_module libexec/apache2/libphp5.so
因為Apache默認(rèn)的文檔目錄在/Library/WebServer/Documents
,所以需要根據(jù)實際情況來決定是否修改該配置季研。如果修改的話在httpd.conf
中找到DocumentRoot
敞葛,將值改為需要的值,同時需要修改的是DocumentRoot
對應(yīng)的Directory
与涡,以我本地為例:
DocumentRoot "/Users/betterzfz/sites"
<Directory "/Users/betterzfz/sites">
#
# Possible values for the Options directive are "None", "All",
# or any combination of:
# Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
#
# Note that "MultiViews" must be named *explicitly* --- "Options All"
# doesn't give it to you.
#
# The Options directive is both complicated and important. Please see
# http://httpd.apache.org/docs/2.4/mod/core.html#options
# for more information.
#
Options FollowSymLinks Multiviews
MultiviewsMatch Any
#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
# AllowOverride FileInfo AuthConfig Limit
#
AllowOverride None
#
# Controls who can get stuff from this server.
#
Require all granted
</Directory>
2 重啟Apache
apachectl restart
3 在項目目錄中創(chuàng)建文件index.php
惹谐,內(nèi)容如下:
<?php
phpinfo();
4 打開瀏覽器訪問http://localhost/index.php,如能看到PHP的信息則表示已經(jīng)配置成功驼卖。
5可以繼續(xù)配置Apache讓訪問http://localhost/時就能默認(rèn)訪問目錄下的index.php
氨肌,配置如下:
<IfModule dir_module>
DirectoryIndex index.php index.html
</IfModule>
記住需要重啟Apache,否則新的配置不會生效酌畜。
安裝MySQL怎囚,到MySQL官網(wǎng)下載,按照Mac OS下DMG安裝包的安裝流程安裝MySQL桥胞。安裝完成后恳守,用戶root會得到一個為空的默認(rèn)密碼。正常情況下MySQL會被安裝到/usr/local/mysql
目錄下贩虾,為了方便使用MySQL命令催烘,可以將/usr/local/mysql/bin
加到PATH:
export PATH=/usr/local/mysql/bin:$PATH
這樣就可以在任意目錄下使用MySQL命令了。
MySQL安裝好后就可以用PHP對其進行連接了缎罢,修改index.php
為如下內(nèi)容:
<?php
$mysqli = new mysqli('localhost', 'root', '');
if ($mysqli->connect_error) {
die('connect error('.$mysqli->connect_errno.')'.$mysqli->connect_error);
}
echo 'success... '.$mysqli->host_info;
$mysqli->close();
刷新http://localhost/index.php后將得到下圖顯示的內(nèi)容:
如果連接沒有成功則需要根據(jù)提示進行相應(yīng)的調(diào)整了伊群。
windows下安裝教程可以參考php初級講義2-環(huán)境的安裝與配置