mac使用brew安裝LNMP,LAMP

注意:不要去刪除系統自帶的Apache 和php 我開始就刪除了系統自帶的Apache,導致後面php一直無法安裝成功,重裝了Apache後才弄好,所以建議不要輕易刪除系統自帶的Apache和php,

homebrew

homebrew是mac系統下特別好用的一個軟件包工具,而且它的安裝也是極爲簡單。

網上有很多人直接給出了homebrew的安裝命令,但是大部分的地址已經失效。在homebrew網站上 http://brew.sh/ ,正中間就是homebrew的安裝命令,直接複製到你的終端裏執行就可以了。

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

homebrew安裝完成了,我們再用brew來安裝Nginx,有了brew後,安裝就變得很簡單了,不需要自己去make之類的。

安裝nginx

brew install nginx

就這一句命令,nginx就安裝好了,不過我們還是需要配置下。

1.給nginx 設置管理員權限:如果不設置管理員權限,80端口是不能監聽的

#這裏的目錄根據你實際安裝的目錄來填寫,默認是這個目錄,不同的只是nginx的版本號而已
sudo chown root:wheel /usr/local/Cellar/nginx/1.10.1/bin/nginx    
sudo chmod u+s /usr/local/Cellar/nginx/1.10.1/bin/nginx

2.加入launchctl啓動控制

mkdir -p ~/Library/LaunchAgents
cp /usr/local/opt/nginx/homebrew.mxcl.nginx.plist ~/Library/LaunchAgents/
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist

到這裏,nginx基本上是完工了

運行nginx :

sudo nginx #打開 nginx
nginx -s reload|reopen|stop|quit  #重新加載配置|重啓|停止|退出 nginx
nginx -t   #測試配置是否有語法錯誤

安裝MySql

brew install mysql

也是一句命令搞定,等執行完後,mysql也安裝完畢,接下來就是對mysql的一些配置

1.先cd到mysql的目錄中:

cd /usr/local/opt/mysql/

2.加入launchctl啓動控制

mkdir -p ~/Library/LaunchAgents/
cp /usr/local/opt/mysql/homebrew.mxcl.mysql.plist ~/Library/LaunchAgents/
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
#取消啓動
#launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist

3.執行安全設置腳本,設置root賬號密碼,如果不執行這一步,是無法用mysql -u root -p這個命令登錄mysql的,網上很多教程就是沒有這個說明,所以這裏特別強調下:

./bin/mysql_secure_installation

執行上面的命令後,會進入mysql的配置,具體步驟就不寫了,每一個選項都有說明是幹什麼的,等這個命令執行完畢後,你就可以用 mysql -u root -p 來登錄mysql 了。

安裝php

php 的安裝相對nginx和mysql來說,要複雜點,因爲brew 默認沒有php的包

brew tap homebrew/dupes
brew tap josegonzalez/homebrew-php

執行完上面命令後,這個時候纔可以安裝php,不過php有很多的版本,你可以用:brew search php 來查看具體的版本。

我電腦上是安裝的php 5.6版本的:

brew install php56 --with-imap --with-tidy --with-debug --with-mysql --with-fpm

1.安裝成功後,就是對php的配置了,因爲mac默認是自帶php的,所以我們要把我們安裝的php加到環境變量裏,而不是繼續使用mac自帶的php

sudo vim ~/.bash_profile

#在這個文件最後添加下列語句:
export PATH="$(brew --prefix php56)/bin:$PATH"

#保存文件後,source下這個文件,使剛剛添加的環境變量生效
source ~/.bash_profile

這個時候,你在命令行裏執行 php -v 看到的不再是系統自帶的php了,而是我們剛剛安裝的php

2.加入launchctl啓動控制

mkdir -p ~/Library/LaunchAgents
cp /usr/local/opt/php56/homebrew.mxcl.php56.plist ~/Library/LaunchAgents/
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.php56.plist

配置文件路徑

/usr/local/etc/php/5.6/php.ini
/usr/local/etc/php/5.6/php-fpm.conf

配置Nginx 支持 php

執行 sudo vim /usr/local/etc/nginx/nginx.conf 修改nginx 的配置文件

把配置文件中 # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 下的 location ~ .php$ {} 註釋取消掉,並修改成下面樣子:

location ~ \.php$ {
    fastcgi_intercept_errors on;
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  /usr/local/Cellar/nginx/1.10.1/html$fastcgi_script_name;
    include        /usr/local/etc/nginx/fastcgi_params;
}

保存文件後,一定要去重啓nginx ,否則是不會加載配置文件的。

如果重啓後,訪問php文件顯示404 File Not Found. 你就需要檢查下你 fastcgi_param SCRIPT_FILENAME 後面跟的目錄是否是正確的。

到這裏,環境搭建就完成了。

順便提下nginx 配置虛擬域名:
1.在nginx中,監聽80端口,server_name 後面輸入你要綁定的域名就好
2.sudo vim /etc/hosts ,在文件最後加入一行:

127.0.0.1    nginx中配置的域名

問題列表:

1.nginx重啓後丟失pid,出現如下錯誤:nginx: [error] open() "/usr/local/var/run/nginx.pid" failed (2: No such file or directory)

shell
#出現這個你只需要執行下面命令就好了: nginx -c 後面跟的是你nginx配置文件的目錄,有可能和我的不一樣
nginx -c /usr/local/etc/nginx/nginx.conf

2.訪問html文件正常,但是訪問php文件直接下載文件
有可能是你忘記重啓nginx,導致nginx還沒有加載php文件的解析配置,所以無法解析。如果重啓後還是不能成功訪問的話,需要確認nginx配置文件是否有誤。

參考地址:http://www.jianshu.com/p/255889464b2f      and       http://www.codeceo.com/article/mac-brew-php-lnmp-lamp.html


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章