mac配置php7運(yùn)行環(huán)境

不用mac自帶的apache和php,安裝自己想要的版本。配置過(guò)程一直采坑,需要有閑時(shí)間和好的心理素質(zhì)才行,哈哈,因?yàn)榫W(wǎng)上很教程都有紕漏之處,所以先把采坑無(wú)數(shù)后發(fā)現(xiàn)的個(gè)人認(rèn)為最好的一個(gè)教程鏈接放在這里https://github.com/nodejh/nodejh.github.io/issues/25下面記錄我的采坑過(guò)程。

安裝mysql

官網(wǎng)https://www.mysql.com/downloads/下載mysql,有問(wèn)題百度。下載后安裝,安裝過(guò)程中會(huì)有一個(gè)臨時(shí)密碼的提示,要注意保存一下,后面會(huì)用到。安裝完畢進(jìn)入系統(tǒng)偏好設(shè)置在最下面找到mysql圖標(biāo)并打開,點(diǎn)擊Start MySQL Server啟動(dòng)mysql。命令行cd到/usr/local/mysql/bin,分別執(zhí)行alias mysql=/usr/local/mysql/bin/mysqlalias mysqladmin=/usr/local/mysql/bin/mysqladmin,這兩條命令是為了方便直接打開 iTerm 就可以運(yùn)行mysql命令,而不是必須進(jìn)入mysql安裝目錄才能運(yùn)行。接下來(lái),重置密碼。執(zhí)行命令mysqladmin -u root -p password ******是你的新密碼?;剀嚭筇崾据斎朊艽a,此時(shí)數(shù)次剛剛保存的臨時(shí)密碼,我的是)6m1d/m<joVi。
此時(shí)提示mysqladmin: [Warning] Using a password on the command line interface can be insecure.Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.說(shuō)明已經(jīng)重置密碼成功。我在這里浪費(fèi)了很多時(shí)間,以為這是錯(cuò)誤提示,其實(shí)已經(jīng)ok了。
登錄mysql。命令mysql -u root -p,然后輸入剛剛重置的密碼。然后登錄成功會(huì)跳出提示信息

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1032
Server version: 5.7.19 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

大意是說(shuō)sql命令以;結(jié)尾,當(dāng)前mysql版本是5.7.19。另外我們也可以通過(guò)sql命令select version();來(lái)查看版本。用exit;退出登錄。安裝完畢。

apache使用mac自帶的

(開始的時(shí)候想用mac自帶apache來(lái)著,后來(lái)和配置php7時(shí)候各種問(wèn)題,放棄了使用mac自帶apache)
基本用法的命令如下:
apachectl -v查看版本,提示Server version: Apache/2.4.25 (Unix)
sudo apachectl start啟動(dòng)
sudo spachectl stop停止
sudo apachectl restart重啟
相關(guān)目錄:
apache日志/private/var/log/apache2/error_log
apache配置主文件/etc/apache2/httpd.conf
apache的vhost配置/etc/apache2/extra/httpd-vhost.conf

安裝php7.0

參考博客http://www.cnblogs.com/redirect/p/6131751.html
添加brew的php擴(kuò)展庫(kù)(mac上沒(méi)有php的包,需要綁定其他人的git倉(cāng)庫(kù),命令brew tap github_user/repo),執(zhí)行如下命令

brew update
brew tap homebrew/dupes
brew tap homebrew/php

可以使用brew options php70來(lái)查看安裝php的選項(xiàng)。注意如果使用apache作為web server,安裝php命令要加--with-apache選項(xiàng),如果使用nginx作為web server,安裝php命令要加--with-fpm選項(xiàng),我的安裝命令和選項(xiàng)brew install php70 --with-apache --width-apxs2 --with-gmp --with-imap --with-tidy --with-debug,然后我按照教程http://www.cnblogs.com/redirect/p/6131751.html上執(zhí)行brew link php70開啟php7.0的進(jìn)程,但是查看php -v提示版本還是php5.6,執(zhí)行brew unlink php56來(lái)關(guān)閉php5.6的進(jìn)程但是報(bào)錯(cuò)提示Error: No such keg: /usr/local/Cellar/php56,反復(fù)試了很多次耽誤了時(shí)間,然后我查其他資料,執(zhí)行brew install php-version安裝了php-version來(lái)切換php版本,php-version是一個(gè)幫助管理從brew安裝的php版本切換的工具。安裝完執(zhí)行php-veision 7.0.22切換版本,然后php -v版本果然切換到php7.0.22了。
下面開始修改apache配置文件。又遇見很耗時(shí)的一個(gè)坑。
按照上面的教程,打開apache配置文件sudo vi /etc/apache2/httpd.conf,不加sudo權(quán)限不夠沒(méi)辦法修改,注釋(前面加#)掉LoadModule php5_module libexec/apache2/libphp5.soInclude /private/etc/apache2/other/*.conf兩句,然后加上LoadModule php7_module /usr/local/opt/php70/libexec/apache2/libphp7.so這句,注意,libphp7.so的地址是對(duì)應(yīng)自己電腦文件所在的地址,于是我去檢查下我的libphp7.so文件是不是上面命令中的地址。結(jié)果發(fā)現(xiàn)/usr/local/opt/php70/下并沒(méi)有libphp7目錄,搞了半天不知道怎么整,于是brew uninstall php7.0.22卸載php7.0,卸載后/usr/local/opt/下面沒(méi)有了php70文件夾,然后重新安裝php7.0,并且又加了一個(gè)選項(xiàng)--httpd24,因?yàn)榭雌渌坛汤镉屑印?code>brew install php70 --with-apxs2 --with-apache --with-gmp --with-imap --with-tidy --with-debug --with-httpd24,然后會(huì)提示如下

./configure --prefix=/usr/local/Cellar/php70/7.0.22_14 --localstatedir=/usr/local/var --sysconfdir=/usr/local/etc/php/7.0 --with-config-file-path=/usr/local/etc/php/7.0 --with-config-file-scan-dir=/us
Last 15 lines from /Users/yanhaoqi/Library/Logs/Homebrew/php70/01.configure:
checking for Kerberos support... /usr
checking whether to use system default cipher list instead of hardcoded value... no
checking for krb5-config... /usr/bin/krb5-config
checking for RAND_egd... no
checking for pkg-config... no
checking for OpenSSL version... >= 0.9.8
checking for CRYPTO_free in -lcrypto... yes
checking for SSL_CTX_set_ssl_version in -lssl... yes
checking for PCRE library to use... bundled
checking whether to enable PCRE JIT functionality... yes
checking whether to enable the SQLite3 extension... yes
checking bundled sqlite3 library... yes
checking for ZLIB support... yes
checking if the location of ZLIB install directory is defined... no
configure: error: Cannot find libz

其實(shí)前面最開始安裝php7的時(shí)候記得也有這個(gè)提示,當(dāng)時(shí)也沒(méi)有在意。這時(shí)候執(zhí)行php -v提示版本是php5.6,執(zhí)行php-version提示Sorry, but you do not seem to have any PHP versions installed.,折騰半天后來(lái)查到資料,需要執(zhí)行xcode-select --install,然后按照提示去安裝,安裝完畢后再重新裝php7.0,brew install php70 --with-apxs2 --with-apache --with-gmp --with-imap --with-tidy --with-debug --with-httpd24,這次沒(méi)有提示configure: error: Cannot find libz,然后查看版本是7.0,php-version也提示7.0.22,并且找到了/usr/local/opt/php70/libexec/apache2/libphp7.so文件。
ok,繼續(xù)apache配置文件。
注釋完上面所說(shuō)的兩句話后,加上如下配置

LoadModule php7_module /usr/local/opt/php70/libexec/apache2/libphp7.so
 
<FilesMatch .php$>
    SetHandler application/x-httpd-php
</FilesMatch>
 
<IfModule php7_module>
    AddType application/x-httpd-php .php
    AddType application/x-httpd-php-source .phps
    <IfModule dir_module>
        DirectoryIndex index.html index.php
    </IfModule>
</IfModule>

#ServerName www.example.com:80后面加上ServerName localhost:80,否則下面重啟apache的時(shí)候回報(bào)錯(cuò)提示AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using ::1. Set the 'ServerName' directive globally to suppress this message
這是個(gè)更大的坑。我卡在這里將近兩天,各種博客資料查詢各種嘗試。曲折過(guò)程已經(jīng)忘記,現(xiàn)在只記錄下結(jié)果。
我在上面寫apache用mac自帶的,版本是Apache/2.4.25 (Unix),但是后面經(jīng)過(guò)我安裝php7的各種折騰后,記不清在什么時(shí)候我通過(guò)brew安裝了Apache/2.4.27我想執(zhí)行brew uninstall httpd24卸載掉但是提示Refusing to uninstall /usr/local/Cellar/httpd24/2.4.27 because it is required by php70 7.0.22_14, which is currently installed.,php7引用了Apache/2.4.27。而且,brew安裝的Apache/2.4.27,配置文件所在目錄/usr/local/etc/apache2/2.4/httpd.conf,其編譯安裝的目錄/usr/local/Cellar/httpd24/2.4.27。oh my god!!!上面修改apache配置弄了這么長(zhǎng)時(shí)間,配置的文件根本就不是正確的文件!
apache配置的正確姿勢(shì)
執(zhí)行命令使用管理員權(quán)限為通過(guò)brew安裝的apache配置為自動(dòng)啟動(dòng)。

sudo cp -v  /usr/local/Cellar/httpd24/2.4.27/homebrew.mxcl.httpd24.plist /Library/LaunchDaemons
sudo chown -v root:wheel /Library/LaunchDaemons/homebrew.mxcl.httpd24.plist
sudo chmod -v 644 /Library/LaunchDaemons/homebrew.mxcl.httpd24.plist
sudo launchctl load /Library/LaunchDaemons/homebrew.mxcl.httpd24.plist

我的apache編譯安裝路徑是/usr/local/Cellar/httpd24/2.4.27,如果你的路徑不是這個(gè),自行替換就好了。
此時(shí)瀏覽器訪問(wèn)localhost將會(huì)看到

屏幕快照 2017-09-07 下午4.57.36.png

可以通過(guò)ps -aef | grep httpd命令查看apache服務(wù)是否啟動(dòng),如果apache正在運(yùn)行會(huì)看到一些http進(jìn)程。

sudo vi /usr/local/etc/apache2/2.4/httpd.conf

修改php模塊配置及apache端口

LoadModule php7_module /usr/local/Cellar/php70/7.0.22_14/libexec/apache2/libphp7.so替換為LoadModule php7_module /usr/local/Cellar/php70/7.0.22_14/libexec/apache2/libphp7.so
找到代碼塊

<IfModule dir_module>
    DirectoryIndex index.html
</IfModule>

替換為

<IfModule dir_module>
    DirectoryIndex index.php index.html
</IfModule>

<FilesMatch \.php$>
    SetHandler application/x-httpd-php
</FilesMatch>

修改ServerName為ServerName localhost:8080
Listen:8080 apache監(jiān)聽了8080端口,如果修改的話直接改端口號(hào)即可。

修改web服務(wù)的根目錄在自己用戶主目錄下方便開發(fā)

找到DocumentRoot "/usr/local/var/www/htdocs"修改為DocumentRoot "/Users/yanhaoqi/sites"
修改下面的<Directory><Directory "/Users/yanhaoqi/sites">
修改AllowOverride NoneAllowOverride All
取消注釋,使用mod_rewrite模塊 LoadModule rewrite_module libexec/mod_rewrite.so

修改用戶和用戶組

修改User和Group如下

User yanhaoqi
Group staff

用戶主目錄下新建sites目錄

cd /Users/yanhaoqi
mkdir sites
sudo vi sites/index.html
sudo vi sites/info.php

在新建的index.html中編輯<h1>yanhaoqi web root</h1>
在新建的info.php中編輯

<?php
    phpinfo();
?>

保存退出后重啟apachesudo apachectl restart
訪問(wèn)localhost:8080

屏幕快照 2017-09-07 下午5.32.28.png

訪問(wèn)localhost:8080/info.php

屏幕快照 2017-09-07 下午5.36.30.png

大功告成!幾乎熱淚盈眶?。?!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容