天天看點

MAC OS brew安裝MNMP

安裝HomeBrew

Brew

Mac

下面的包管理工具,就像centos下面的yum一樣。

HomeBrew

可以通過

ruby

來安裝,mac系統是自帶

ruby

的,是以隻要在終端運作下面的代碼即可安裝

HomeBrew

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

在安裝的時候會有下面的提示,你應該仔細看下,要清楚知道

HomeBrew

給你安裝了哪些腳本,給你建立了哪些檔案夾,路徑是在哪裡的。仔細的看下安裝時出現的說明資訊,這樣即使沒有用過的軟體,你根本不用看文檔,都是能夠使用它的最基礎功能的。如果你一定要研究下細緻的,可以看

HomeBrew

的官網:http://brew.sh

==> 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

就能知道該怎麼用這個軟體了。

自檢Brew

安裝完成之後,建議執行一下自檢,

brew doctor

如果看到

Your system is ready to brew.

那麼你的

brew

已經可以開始使用。

美化下終端

brew

我們已經安裝好了,現在已經可以使用了,但是看上面的圖,我們的終端太他媽的醜了,

mac

預設使用的shell是

bash

,如果把它換成

zsh

,并安裝

zsh

的插件

oh-my-zsh

,那就會好很多了,mac系統已經自帶了

zsh

,我們就不用去安裝了,執行

zsh --version

可以檢視zsh的版本,我們需要安裝個

zsh

的插件

oh-my-zsh

安裝

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

安裝

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

或者重新打開終端。

使用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換主題

先給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

安裝

好了,然後滑鼠移到菜單欄

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,

}           

安裝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           

到這裡為止,我們已經安裝完了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 重新開機下

/*引用自 segmentfault-周繼平*/