原文地址: http://www.zhoujiping.com/notes/mnmp.html
2011年的MacBook Pro(機械硬盤,8G內(nèi)存),之前升級到Mac 10.11,但會經(jīng)常的卡頓,所以一直使用的是Mac 10.10系統(tǒng),最近不知道怎么了,瀏覽器在后臺會自動播放廣告聲音,現(xiàn)在mac 10.12出來了,該系統(tǒng)除了添加Siri,基本上就是10.11的一個升級,嘗試下載安裝,很好,竟然跑的很流暢,在mac 10.11上出現(xiàn)的卡頓現(xiàn)象也沒有了,嗯,還是不錯的。
我是在App Store上下載系統(tǒng)后,然后做成U盤啟動盤后,電腦格盤重新安裝的,制作U盤啟動盤很簡單,首先要使用Mac磁盤工具
把你的U盤或者移動硬盤進行格式化,最好用移動硬盤,速度會快點:
然后打開終端,執(zhí)行下面的命令就可以了:
sudo /Applications/Install\ macOS\ Sierra.app/Contents/Resources/createinstallmedia --volume /Volumes/Sierra --applicationpath /Applications/Install\ macOS\ Sierra.app
上面這個代碼應該是能看懂它的意思的,如果看不懂的話,那也沒有關系,那么你在格式化U盤的時候,一定要注意取名叫
Sierra
,然后拷貝上面的代碼到終端執(zhí)行就可以了。就是那么的簡單,你的U盤啟動盤就做好了。然后備份你電腦的資料(應該也沒啥資料,代碼都在github或者coding,別的東西放網(wǎng)盤,這年頭電腦上誰還放東西呢),重啟電腦,按住option
選擇U盤的盤符安裝就行了。
安裝HomeBrew
Brew
是Mac
下面的包管理工具,就像centos下面的yum一樣。HomeBrew
可以通過ruby
來安裝,mac系統(tǒng)是自帶ruby
的,所以只要在終端運行下面的代碼即可安裝HomeBrew
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
在安裝的時候會有下面的提示,你應該仔細看下,要清楚知道HomeBrew
給你安裝了哪些腳本,給你創(chuàng)建了哪些文件夾,路徑是在哪里的。仔細的看下安裝時出現(xiàn)的說明信息,這樣即使沒有用過的軟件,你根本不用看文檔,都是能夠使用它的最基礎功能的。如果你一定要研究下細致的,可以看HomeBrew
的官網(wǎng):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
現(xiàn)在我們已經(jīng)安裝完HomeBrew
了,其實你只要運行brew help
就能知道該怎么用這個軟件了。
自檢Brew
安裝完成之后,建議執(zhí)行一下自檢,brew doctor
如果看到Your system is ready to brew.
那么你的brew
已經(jīng)可以開始使用。
美化下終端
brew
我們已經(jīng)安裝好了,現(xiàn)在已經(jīng)可以使用了,但是看上面的圖,我們的終端太他媽的丑了,mac
默認使用的shell是bash
,如果把它換成zsh
,并安裝zsh
的插件oh-my-zsh
,那就會好很多了,mac系統(tǒng)已經(jīng)自帶了zsh
,我們就不用去安裝了,執(zhí)行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)"
終端執(zhí)行上面的代碼即可,程序安裝完oh-my-zsh
后會自動幫我們把shell切換為zsh
,現(xiàn)在我們的終端就變成這樣了,注:oh-my-zsh
還可以更換主題和裝插件的,具體訪問:http://ohmyz.sh
安裝FiraCode
字體
嗯,上面的差不多了,我們再安裝個比較流行的編程字體FireCode
,可以進入:https://github.com/tonsky/FiraCode點擊下載,上面的地址可能會下載失敗,我也把這個字體放在百度網(wǎng)盤上了,你也可以點擊這里下載, 下載后解壓然后雙擊字體就可以安裝上了。
現(xiàn)在我們打開終端,點擊終端->偏好設置
,我們在這里按自己的喜歡設置一下背景,字體,顏色,行間距等即可,我設置完就成為下面這樣了,嗯,這樣就好看多了
安裝終端的命令自動提示
在終端下操作會經(jīng)常需要輸入一些常用的命令,要不出錯的輸入這些命令也不是件容易的事,zsh
還有一個很好用的補全命令的插件zsh-autosuggestions
, 我們通過下面命令安裝下:
brew install zsh-autosuggestions
我記得以前只要這么安裝完就可以的,但是這次我安裝完還是沒有出現(xiàn)自動提示,不去想這個原因了,我們自己設置下就行了,先找到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
好了,然后你可以執(zhí)行source ~/.zshrc
或者重新打開終端,看下效果:
如上圖,我輸入一個
b
就會自動提示我之前輸入過的命令,然后按一下右方向鍵
就能自動補全了。
使用homebrew-cask安裝sublime
先安裝cask, 使用cask可以安裝一些應用程序,如QQ,迅雷,谷歌瀏覽器等,功能和App stroe差不多,個人覺得這個更好管理
# 網(wǎng)上大部分方法都是用brew tap添加源再安裝的,其實不用那么麻煩,只要輸入brew cask即可,brew會自動幫我們完成cask安裝。
brew cask # 沒有安裝cask會自動安裝,已經(jīng)安裝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如下:
總體來說,字體和樣式都挺不錯了,不過大多數(shù)人都會換一個自己喜歡的主題的。
給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
# 第二步:執(zhí)行mysql_secure_installation
mysql_secure_installation # 執(zhí)行后按照提示信息進行設置,慢慢看下英文,都能看懂的
好了,進入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
現(xiàn)在brew search php
就可以了,出來一堆版本的php,我們就安裝php7.0版本吧,我用php7.1版本在跑laravel的時候,出現(xiàn)過錯誤,我那時候沒有解決掉,裝回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,因此需要添加系統(tǒng)環(huán)境變量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之后,確保它正常運行監(jiān)聽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
到這里為止,我們已經(jīng)安裝完了mysql php nginx, 在安裝php的時候添加了--with-mysql, 所以php操作mysql是沒有問題的,現(xiàn)在我們就要配置nginx,讓它監(jiān)聽php-fpm的進程,這樣當用戶打開瀏覽器訪問的時候,身為反向代理的nignx就能把東西讓php去執(zhí)行了。
接下來,我們要配置nginx.conf
文件,創(chuàng)建一個php-fpm
文件(監(jiān)聽php-fpm), 還要約定下將nginx.pid
文件,log日志,以及以后我們要配置的站點.conf的路徑,我們的路徑約定還是按照brew默認的目錄來設置,如下:
# nginx.conf,已經(jīng)被創(chuàng)建好了,我們一會要更改下
/usr/local/etc/nginx/nginx.conf
# php-fpm,這個我們就放在和nginx.conf一樣的路徑下吧,這個要我們自己創(chuàng)建
/usr/local/etc/nginx/php-fpm
# 日志文件放在/usr/local/var/log/nginx中,默認已經(jīng)有了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
ok, 下面就能配置站點了,先到```/usr/local/var/www```目錄下建立站點根目錄,就叫做default吧,然后在里面建立個info.php,內(nèi)容就放phpinfo()函數(shù)就行.
```bash
mkdir /usr/local/var/www/default
vi /usr/local/var/www/default/info.php #輸入 <?php phpinfo();
最后,去創(chuàng)建站點.conf文件
/usr/local/etc/nginx/servers/default.conf
輸入下面的內(nèi)容
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;
}
測試下配置文件
```bash
? 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,已經(jīng)開啟的就不用開了
php-fpm -D
開啟nginx服務
sudo nginx # 已經(jīng)開啟的用sudo nginx -s reload 重啟下
瀏覽器打開http://localhost/info.php測試下
別名
上面的php-fpm關閉和重啟的命令有點長,我們給它做個別名,在你的個人主目錄下先建立一個.aliases
文件,然后放入下面的內(nèi)容
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
本文結束。