Mac OS 10.12 Sierra 用HomeBrew安裝MNMP開發環境 (Mac+Nginx+Mysql+Php)

原文地址:http://www.zhoujiping.com/notes/mnmp.htmlphp

2011年的MacBook Pro(機械硬盤,8G內存),以前升級到Mac 10.11,但會常常的卡頓,因此一直使用的是Mac 10.10系統,最近不知道怎麼了,瀏覽器在後臺會自動播放廣告聲音,如今mac 10.12出來了,該系統除了添加Siri,基本上就是10.11的一個升級,嘗試下載安裝,很好,居然跑的很流暢,在mac 10.11上出現的卡頓現象也沒有了,嗯,仍是不錯的。html

我是在App Store上下載系統後,而後作成U盤啓動盤後,電腦格盤從新安裝的,製做U盤啓動盤很簡單,首先要使用Mac磁盤工具把你的U盤或者移動硬盤進行格式化,最好用移動硬盤,速度會快點:
Mac格式化U盤python

而後打開終端,執行下面的命令就能夠了:mysql

sudo /Applications/Install\ macOS\ Sierra.app/Contents/Resources/createinstallmedia --volume /Volumes/Sierra --applicationpath /Applications/Install\ macOS\ Sierra.app

上面這個代碼應該是能看懂它的意思的,若是看不懂的話,那也沒有關係,那麼你在格式化U盤的時候,必定要注意取名叫Sierra,而後拷貝上面的代碼到終端執行就能夠了。就是那麼的簡單,你的U盤啓動盤就作好了。而後備份你電腦的資料(應該也沒啥資料,代碼都在github或者coding,別的東西放網盤,這年頭電腦上誰還放東西呢),重啓電腦,按住option選擇U盤的盤符安裝就好了。nginx

安裝HomeBrew

BrewMac下面的包管理工具,就像centos下面的yum同樣。HomeBrew能夠經過ruby來安裝,mac系統是自帶ruby的,因此只要在終端運行下面的代碼便可安裝HomeBrewlaravel

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

在安裝的時候會有下面的提示,你應該仔細看下,要清楚知道HomeBrew給你安裝了哪些腳本,給你建立了哪些文件夾,路徑是在哪裏的。仔細的看下安裝時出現的說明信息,這樣即便沒有用過的軟件,你根本不用看文檔,都是可以使用它的最基礎功能的。若是你必定要研究下細緻的,能夠看HomeBrew的官網:http://brew.shc++

==> This script will install:
/usr/local/bin/brew
/usr/local/share/doc/homebrew
/usr/local/share/man/man1/brew.1
/usr/local/share/zsh/site-functions/_brew
/usr/local/etc/bash_completion.d/brew
/usr/local/Homebrew
==> The following new directories will be created:
/usr/local/Cellar
/usr/local/Homebrew
/usr/local/Frameworks
/usr/local/bin
/usr/local/etc
/usr/local/include
/usr/local/lib
/usr/local/opt
/usr/local/sbin
/usr/local/share
/usr/local/share/zsh
/usr/local/share/zsh/site-functions
/usr/local/var

如今咱們已經安裝完HomeBrew了,其實你只要運行brew help就能知道該怎麼用這個軟件了。git

自檢Brew

安裝完成以後,建議執行一下自檢,brew doctor若是看到Your system is ready to brew. 那麼你的brew已經能夠開始使用。github

brew doctor

美化下終端

brew咱們已經安裝好了,如今已經可使用了,可是看上面的圖,咱們的終端太他媽的醜了,mac默認使用的shell是bash,若是把它換成zsh,並安裝zsh的插件oh-my-zsh,那就會好不少了,mac系統已經自帶了zsh,咱們就不用去安裝了,執行zsh --version能夠查看zsh的版本,咱們須要安裝個zsh的插件oh-my-zshsql

安裝oh-my-zsh

sh -c "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"

終端執行上面的代碼便可,程序安裝完oh-my-zsh後會自動幫咱們把shell切換爲zsh,如今咱們的終端就變成這樣了,注:oh-my-zsh還能夠更換主題和裝插件的,具體訪問:http://ohmyz.sh
oh my zsh

安裝FiraCode字體

嗯,上面的差很少了,咱們再安裝個比較流行的編程字體FireCode,能夠進入:https://github.com/tonsky/FiraCode點擊下載,上面的地址可能會下載失敗,我也把這個字體放在百度網盤上了,你也能夠點擊這裏下載, 下載後解壓而後雙擊字體就能夠安裝上了。

如今咱們打開終端,點擊終端->偏好設置,咱們在這裏按本身的喜歡設置一下背景,字體,顏色,行間距等便可,我設置完就成爲下面這樣了,嗯,這樣就好看多了
美化後的終端

安裝終端的命令自動提示

在終端下操做會常常須要輸入一些經常使用的命令,要不出錯的輸入這些命令也不是件容易的事,zsh還有一個很好用的補全命令的插件zsh-autosuggestions, 咱們經過下面命令安裝下:

brew install zsh-autosuggestions

我記得之前只要這麼安裝完就能夠的,可是此次我安裝完仍是沒有出現自動提示,不去想這個緣由了,咱們本身設置下就好了,先找到zsh-autosuggestions.zsh文件的安裝路徑,在/usr/local/Cellar/zsh-autosuggestions/0.3.3/share/zsh-autosuggestions裏面(這裏就是前面我爲何要你們看brew的安裝提示了,咱們知道brew將程序安裝在什麼文件夾,那就很容易的找到咱們要的文件),咱們把zsh-autosuggestions.zsh放入~/.zshrc文件中,就是加入下面這句話:

source /usr/local/Cellar/zsh-autosuggestions/0.3.3/share/zsh-autosuggestions/zsh-autosuggestions.zsh

好了,而後你能夠執行source ~/.zshrc或者從新打開終端,看下效果:
自動提示

如上圖,我輸入一個b就會自動提示我以前輸入過的命令,而後按一下右方向鍵就能自動補全了。

使用homebrew-cask安裝sublime

先安裝cask, 使用cask能夠安裝一些應用程序,如QQ,迅雷,谷歌瀏覽器等,功能和App stroe差很少,我的以爲這個更好管理

# 網上大部分方法都是用brew tap添加源再安裝的,其實不用那麼麻煩,只要輸入brew cask便可,brew會自動幫咱們完成cask安裝。
brew cask # 沒有安裝cask會自動安裝,已經安裝cask的,會跳出cask命令列表

Cask 經常使用命令

brew cask search        #列出全部能夠被安裝的軟件
brew cask search sublime    #查找全部和sublime相關的應用
brew cask list          #列出全部經過cask安裝的軟件
brew cask info sublime-text  #查看 sublime 的信息
brew cask install sublime-text  # 安裝sublime
brew cask uninstall sublime-text   #卸載sublime

一般咱們是不知道軟件的全稱的,好比安裝sublime,咱們能夠先使用brew cask serach搜索:

brew cask search sublime
==> Exact match
caskroom/cask/sublime
==> Partial matches
caskroom/cask/sublime-text               caskroom/cask/sublime-text

從上面咱們能夠看出,要安裝的sublime全稱是sublime-text,咱們使用brew cask install sublime-text來安裝它

brew cask install sublime-text

咱們安裝好的sublime text 3以下:
sublime-default

整體來講,字體和樣式都挺不錯了,不過大多數人都會換一個本身喜歡的主題的。

給sublime換主題

先給sublime text 3安裝Package Control, 按住control + ` ,在彈出的命令行輸入:

import urllib.request,os; pf = 'Package Control.sublime-package'; ipp = sublime.installed_packages_path(); urllib.request.install_opener( urllib.request.build_opener( urllib.request.ProxyHandler()) ); open(os.path.join(ipp, pf), 'wb').write(urllib.request.urlopen( 'http://sublime.wbond.net/' + pf.replace(' ','%20')).read())

而後command + shift + p,打開Package Control,輸入install,選擇Package Control:Install Package回車,在跳出的命令行輸入選擇Material Theme安裝,而後按下圖啓用
啓用Material Theme

好了,而後鼠標移到菜單欄Sublime Text -> Preferences > Settings在右邊的窗口(user)中輸入你本身要的配置,個人配置以下:

{
    "always_show_minimap_viewport": false,
    "color_scheme": "Packages/Material Theme/schemes/OLD/Material-Theme.tmTheme",
    "font_face": "Fira Code",
    "font_size": 15,
    "ignored_packages":
    [
        "Vintage"
    ],
    "line_padding_bottom": 4,
    "line_padding_top": 4,
    "theme": "Material-Theme.sublime-theme",
    "translate_tabs_to_spaces": true,
    "tab_size": 4,

}

最後來看下效果,嗯,好看多了
sublime Material Theme 主題

安裝Mysql

先查找下mysql

➜  ~ brew search mysql
automysqlbackup            mysql-cluster              mysql-sandbox
mysql                      mysql-connector-c          mysql-search-replace
mysql++                    mysql-connector-c++        mysqltuner
homebrew/php/php53-mysqlnd_ms            homebrew/versions/mysql56
homebrew/php/php54-mysqlnd_ms            Caskroom/cask/mysql-connector-python
homebrew/php/php55-mysqlnd_ms            Caskroom/cask/mysqlworkbench
homebrew/php/php56-mysqlnd_ms            Caskroom/cask/navicat-for-mysql
homebrew/versions/mysql55                Caskroom/cask/sqlpro-for-mysql

看一下mysql的版本信息

➜  ~ brew info mysql     
mysql: stable 5.7.16 (bottled)
Open source relational database management system
# 下面信息省略

下面就用brew install mysql安裝下吧,安裝時的消息有這麼一句話We've installed your MySQL database without a root password. To secure it run:mysql_secure_installation,那就來設置下root的密碼

# 第一步:打開mysql服務
mysql.server start
# 第二步:執行mysql_secure_installation
mysql_secure_installation # 執行後按照提示信息進行設置,慢慢看下英文,都能看懂的

好了,進入mysql看下

➜  ~ mysql -u root -p         
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.16 Homebrew

Copyright (c) 2000, 2016, 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.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

mysql>

安裝php

直接brew search php 莫有用,你能夠試下看看提示信息,要先添加php擴展

brew update # 安裝軟件前都要習慣的更新下brew源
brew tap homebrew/dupes
brew tap josegonzalez/homebrew-php

如今brew search php就能夠了,出來一堆版本的php,咱們就安裝php7.0版本吧,我用php7.1版本在跑laravel的時候,出現過錯誤,我那時候沒有解決掉,裝回php7.0沒有問題。

能夠先brew options ph70看看你安裝的時候要哪些選項,我選擇了下面這些,由於後面咱們會用nginx做爲反向代理,因此就不要用--with-apache選項了,也不要添加--without-fpm選項

brew install php70 --with-debug --with-gmp --with-homebrew-curl --with-homebrew-libressl --with-homebrew-libxml2 --with-homebrew-libxslt --with-imap --with-libmysql --with-mssql

因爲Mac自帶了php和php-fpm,所以須要添加系統環境變量PATH來替代自帶PHP版本,咱們用的是zsh,因此放進.zshrc中,若是你用的shell是bash,那麼能夠把下面的信息寫入到~/.bash_profile文件中,若是這個文件沒有,你本身建一個就行。

echo 'export PATH="$(brew --prefix php70)/bin:$PATH"' >> ~/.zshrc  #for php
echo 'export PATH="$(brew --prefix php70)/sbin:$PATH"' >> ~/.zshrc  #for php-fpm
echo 'export PATH="/usr/local/bin:/usr/local/sbib:$PATH"' >> ~/.zshrc #for other brew install soft
source ~/.zshrc

測試下效果

# brew安裝的php
➜  ~ php -v    
PHP 7.0.12 (cli) (built: Oct 24 2016 00:06:38) ( NTS DEBUG )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies

# brew安裝的php-fpm
➜  ~ php-fpm -v
PHP 7.0.12 (fpm-fcgi) (built: Oct 24 2016 00:06:45) (DEBUG)
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies

# Mac自帶的php, 我之前的項目有些依賴不支持php7,因此這個我是留着的,隨時切換使用
➜  ~ /usr/bin/php -v     
PHP 5.6.24 (cli) (built: Aug  8 2016 16:58:37) 
Copyright (c) 1997-2016 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies

# Mac自帶php-fpm
➜  ~ /usr/sbin/php-fpm -v
PHP 5.6.24 (fpm-fcgi) (built: Aug  8 2016 16:58:54)
Copyright (c) 1997-2016 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies
➜  ~

下面先來看下php-fpm的配置文件,路徑在/usr/local/etc/php/7.0/php-fpm.conf, 你們應該都猜的到。咱們主要看下pid和log文件會放在哪裏

13 [global]
 14 ; Pid file
 15 ; Note: the default prefix is /usr/local/var
 16 ; Default Value: none
 17 ;pid = run/php-fpm.pid
 18 
 19 ; Error log file
 20 ; If it's set to "syslog", log is sent to syslogd instead of being written
 21 ; in a local file.
 22 ; Note: the default prefix is /usr/local/var
 23 ; Default Value: log/php-fpm.log
 24 ;error_log = log/php-fpm.log

本身看下上面的信息,去掉17行和24行前面的分號,使用php-fpm -t測試下配置是否正確,按提示信息是無論它也能夠,默認就是在/usr/local/var路徑下的,不過仍是設置下吧;

➜  7.0 php-fpm -t
[24-Oct-2016 11:20:31] NOTICE: configuration file /usr/local/etc/php/7.0/php-fpm.conf test is successful

php-fpm的一些管理:

#測試php-fpm配置
php-fpm -t

#啓動php-fpm
php-fpm -D

#關閉php-fpm
kill -INT `cat /usr/local/var/run/php-fpm.pid`

#重啓php-fpm
kill -USR2 `cat /usr/local/var/run/php-fpm.pid`

#也能夠用上文提到的brew命令來管理php-fpm
brew services start|stop|restart php70

#還能夠用這個命令來管理php-fpm
php70-fpm start|stop|restart

啓動php-fpm以後,確保它正常運行監聽9000端口:

➜  ~ lsof -Pni4 | grep LISTEN | grep php
php-fpm   18381  zjp    8u  IPv4 0xbca78421d968b30f      0t0  TCP 127.0.0.1:9000 (LISTEN)
php-fpm   18382  zjp    0u  IPv4 0xbca78421d968b30f      0t0  TCP 127.0.0.1:9000 (LISTEN)
php-fpm   18383  zjp    0u  IPv4 0xbca78421d968b30f      0t0  TCP 127.0.0.1:9000 (LISTEN)

安裝Nginx

和前面同樣先brew search nginx查找nginx, 看下信息brew info nginx
而後安裝brew install nginx

安裝信息記錄下,用的到:

➜  ~ brew install nginx
==> Auto-updated Homebrew!
Updated 1 tap (homebrew/core).
==> New Formulae
gitless
==> Updated Formulae
fzf                                                                                    gammu

==> Downloading https://homebrew.bintray.com/bottles/nginx-1.10.2.sierra.bottle.tar.gz
######################################################################## 100.0%
==> Pouring nginx-1.10.2.sierra.bottle.tar.gz
==> Using the sandbox
==> Caveats
Docroot is: /usr/local/var/www

The default port has been set in /usr/local/etc/nginx/nginx.conf to 8080 so that
nginx can run without sudo.

nginx will load all files in /usr/local/etc/nginx/servers/.

To have launchd start nginx now and restart at login:
  brew services start nginx
Or, if you don't want/need a background service you can just run:
  nginx
==> Summary

Nginx啓動關閉命令:

#測試配置是否有語法錯誤
nginx -t

#打開 nginx
sudo nginx

#從新加載配置|重啓|中止|退出 nginx
nginx -s reload|reopen|stop|quit
好了,來跑下nginx
sudo nginx

nginx

到這裏爲止,咱們已經安裝完了mysql php nginx, 在安裝php的時候添加了--with-mysql, 因此php操做mysql是沒有問題的,如今咱們就要配置nginx,讓它監聽php-fpm的進程,這樣當用戶打開瀏覽器訪問的時候,身爲反向代理的nignx就能把東西讓php去執行了。

接下來,咱們要配置nginx.conf文件,建立一個php-fpm文件(監聽php-fpm), 還要約定下將nginx.pid文件,log日誌,以及之後咱們要配置的站點.conf的路徑,咱們的路徑約定仍是按照brew默認的目錄來設置,以下:

# nginx.conf,已經被建立好了,咱們一會要更改下
/usr/local/etc/nginx/nginx.conf

# php-fpm,這個咱們就放在和nginx.conf同樣的路徑下吧,這個要咱們本身建立
/usr/local/etc/nginx/php-fpm

# 日誌文件放在/usr/local/var/log/nginx中,默認已經有了access.log和error.log文件了
/usr/local/var/log/nginx/

# nginx.pid文件,放在/usr/local/var/run/下面,和php-fpm.pid放一堆
/usr/local/var/run/

# 之後要配置的站點.conf, 咱們就放在/usr/local/etc/nginx/servers/下面,這個servers文件夾自己就存在的
/usr/local/etc/nginx/servers/

# 站點的根目錄,也就用brew給咱們設置的吧
/usr/local/var/www/

我不知道你們是怎麼來定義路徑的,個人智商不咋的,記性很是的差,通常我都用brew安裝軟件,因此用brew默認的路徑,我能方便的找到個人文件。

下面咱們先來修改nginx.conf, 用vim打開,把下面的信息覆蓋nginx.conf, vim /usr/local/etc/nginx/nginx.conf, 若是你不習慣vim, 那就用sublime打開吧subl /usr/local/etc/nginx/nginx.conf

worker_processes  1;

error_log   /usr/local/var/log/nginx/error.log debug;
pid        /usr/local/var/run/nginx.pid;

events {
    worker_connections  256;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /usr/local/var/log/nginx/access.log  main;

    sendfile        on;
    keepalive_timeout  65;
    port_in_redirect off;

    include /usr/local/etc/nginx/servers/*;
}

接下來,將下面的信息放入到php-fpm文件中,vim /usr/local/etc/nginx/php-fpm

location ~ \.php$ {
    try_files                   $uri = 404;
    fastcgi_pass                127.0.0.1:9000;
    fastcgi_index               index.php;
    fastcgi_intercept_errors    on;
    include /usr/local/etc/nginx/fastcgi.conf;
}

ok, 下面就能配置站點了,先到/usr/local/var/www目錄下創建站點根目錄,就叫作default吧,而後在裏面創建個info.php,內容就放phpinfo()函數就行.

mkdir /usr/local/var/www/default
vi /usr/local/var/www/default/info.php #輸入 <?php  phpinfo();

最後,去建立站點.conf文件

/usr/local/etc/nginx/servers/default.conf

輸入下面的內容

server {
    listen       80;
    server_name  localhost;
    root         /usr/local/var/www/default;

    access_log  /usr/local/var/log/nginx/default.access.log  main;

    location / {
        index  index.html index.htm index.php;
        autoindex   on;
        include     /usr/local/etc/nginx/php-fpm;
    }

    error_page  404     /404.html;
    error_page  403     /403.html;
}

測試下配置文件

➜  servers sudo nginx -t
nginx: the configuration file /usr/local/etc/nginx/nginx.conf syntax is ok
nginx: configuration file /usr/local/etc/nginx/nginx.conf test is successful
而後開啓php-fpm,已經開啓的就不用開了
php-fpm -D

開啓nginx服務

sudo nginx # 已經開啓的用sudo nginx -s reload 重啓下

瀏覽器打開http://localhost/info.php測試下
MNMP

別名

上面的php-fpm關閉和重啓的命令有點長,咱們給它作個別名,在你的我的主目錄下先創建一個.aliases文件,而後放入下面的內容

alias fpm.start='php-fpm -D'
alias fpm.stop='kill -INT `cat /usr/local/var/run/php-fpm.pid`'
alias fpm.restart='kill -USR2 `cat /usr/local/var/run/php-fpm.pid`'
alias fpm.status='lsof -Pni4 | grep LISTEN | grep php-fpm'

而後

echo 'source ~/.aliases' >> ~/.zshrc
source ~/.zshrc

本文結束。

以上安裝出現的問題記錄:

1. 使用Curl出現的502錯誤

作有贊接口的時候發現出現了502錯誤,用下面的測試代碼也能夠測試:

$curl = curl_init(); 
        // 抓取支付寶首頁測試 
        curl_setopt($curl, CURLOPT_URL, 'https://www.alipay.com'); 
        // 設置header 
        curl_setopt($curl, CURLOPT_HEADER, 1); 
        // 設置cURL 參數,要求結果保存到字符串中仍是輸出到屏幕上 
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
        // 運行cURL,請求網頁數據 
        $data = curl_exec($curl); 
        // 關閉cURL請求 
        curl_close($curl); 
        // 打印出抓取的測試數據 
        var_dump($data);

產生問題的緣由:brew安裝curl時默認沒有帶上--with-openssl
解決方法:

# 先刪除curl
brew uninstall curl

# 從新安裝curl,帶上--with-openssl
brew install curl --with-openssl

而後重啓下php-fpm

相關文章
相關標籤/搜索