macOS High Sierra 10.13.3全新搭建PHP開發環境

clipboard.png

用了快三年的Mac Pro出現問題,蘋果公司幫忙換了一臺全新的,因此沒辦法,只能從新裝環境,裝平時開發所需的各類軟件,本着樂於助人的雷鋒精神也把此次搭建寫出來,但願能夠幫助更多人。php

廢話很少說進入正題mysql

macOS Sierra 已經幫咱們預裝了 Ruby、PHP、Perl、Python 等經常使用的腳本語言,以及 Apache 服務器等等。linux


1.安裝 Xcode

Xcode 是蘋果出品的包含一系列工具及庫的開發軟件。
經過 App Store 安裝最新版本的 Xcode。(若是你的Xcode是8.2以前的版本的話,在你編譯PHP7.0+的時候會提示你「更新Xcode」版本)
咱們通常不會用 Xcode 來開發 PHP 項目。但這一步也是必需的,由於 Xcode 會幫你附帶安裝一些如 Git 等必要的軟件。固然你也能夠經過源碼包安裝 Git
N多軟件包都基於Xcode,既然必需要安裝,因此我就把這個安裝放在第一步。nginx

安裝成功,哎公司網絡比較差,只能晚上回家安裝,大概等了2小時吧,size 5G左右,因此網絡很差就提早安裝一下吧。laravel

2.安裝 Xcode Command Line Tools

這一步會幫你安裝許多常見的基於 Unix 的工具。Xcode 命令行工具做爲 Xcode 的一部分,包含了 GCC 編譯器。在命令行中執行如下命令便可安裝:git

xcode-select --install

<img src="https://leanote.com/api/file/...; height="200px" width="400px">github

直接點擊install,而後等待安裝完成就好,哎等待老是漫長的。
Verify that you’ve successfully installed Xcode Command Line Tools:redis

xcode-select -p
返回如下表示安裝成功
/Applications/Xcode.app/Contents/Developer

3.安裝 Homebrew

HomeBrew 是macOS 軟件包管理器,用來安裝、升級以及卸載經常使用的軟件sql

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


及時按回車鍵而後輸入開機密碼,等待安裝完成
有時homebrew核心倉庫沒有咱們所要的安裝包,但在其餘倉庫中有,那咱們能夠本身添加shell

homebrew擴展倉庫
* 列出已有倉庫:` brew tap`
* 添加倉庫:` brew tap 倉庫名 `
* 刪除倉庫:` brew untap 倉庫名`

安裝完成後,咱們能夠考慮更改下 Homebrew 源,由於國外源一直不是很給力,這裏咱們將 Homebrew 源改成中國科學技術大學開源軟件鏡像:

cd "$(brew --repo)"
git remote set-url origin https://mirrors.ustc.edu.cn/brew.git

4. 安裝iTerm2

iTerm2 是 MAC 下最好的終端工具(沒有之一)以及配合oh-my-zsh 及其插件,將是強大的神器
下載iTerm2,打開會提示移動到application,或者在 Finder 中,將 iTerm 拖拽進入 Application 文件夾中。這樣,你能夠在 Launchpad 中啓動 iTerm2。

4.1 安裝 oh-my-zsh

接下來安裝配合iTerm2使用的oh-my-zsh
首先查看系統支持的shell列表,Mac 系統自帶了 zsh ,Linux上得安裝

cat /etc/shells
zsh --version   //查看版本

雖然Mac自帶了zsh,若是你想要最新版的zsh,那麼你用 brew install zsh 安裝一個最新的吧(問題不大)

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

哇哦,iTerm2的界面瞬間舒服多了,有沒有、有沒有、有沒有?(哈哈哈哈哈哈)

不過默認的theme主題」robbyrussell」用起來感受仍是差了那麼一點點,大多數coder都比較喜歡 agnoster.zsh-theme 這個主題

各類主題

vim ~/.zshrc       //打開這個配置文件
ZSH_THEME="robbyrussell"    找到這行主題配置
更換成如下的主題名就好
ZSH_THEME="agnoster" # (this is one of the fancy ones)
# see https://github.com/robbyrussell/oh-my-zsh/wiki/Themes#agnoster

打開一個新終端看看目前是什麼樣的一個主題,不過貌似有一點不對,有一些字符不能正常顯示,so咱們來安裝一個字體 Powerline fonts

# clone
git clone https://github.com/powerline/fonts.git --depth=1
# install
cd fonts
./install.sh
# clean-up a bit
cd ..
rm -rf fonts

而後到iterm2的preferences > profiles > colors 配色設置選擇一個(我比較喜歡這種類型的顏色)
http://ethanschoonover.com/so... 配色主題
繼續修改字體,改爲剛安裝成功的字體
<img src="https://leanote.com/api/file/...; width="50%" height="50%"><img src="https://leanote.com/api/file/...; width="40%" height="50%">

而後重開一個終端,發現會帶上本身mac的用戶名什麼的,我的感受挺多餘的,so我本身來改變一下
首先進到存放主題的文件夾》複製agnoster.zsh-theme > myagnoster.zsh-theme

cd ~/.oh-my-zsh/themes  //進入主題文件夾
cp agnoster.zsh-theme myagnoster.zsh-theme //複製一份
vim myagnoster.zsh-theme  打開
 
## Main prompt
build_prompt() {
  RETVAL=$?
  prompt_status
  prompt_virtualenv
 #prompt_context
  prompt_dir
  prompt_git
  prompt_hg
  prompt_end
}
只需把prompt_context用#註釋掉便可
 
而後打開.zshrc  
ZSH_THEME="agnoster"  》 ZSH_THEME="myagnoster" 改爲這樣就能夠了
 
這樣作的緣由是避免升級有衝突

4.2 插件

oh my zsh 自帶插件
Oh My Zsh 自己自帶了不少插件,好比說: git, autojump osx, 不過基本都沒有啓用,插件目錄: ~/.oh-my-zsh/plugins

4.2.1 安裝 zsh-syntax-highlighting

這個自動高亮效果的插件也是配合oh-my-zsh使用

Oh-my-zsh插件形式的安裝,還有更多安裝方式你們能夠去探索一下

  • Clone this repository in oh-my-zsh's plugins directory:
git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting
  • Activate the plugin in ~/.zshrc:
plugins=( [plugins...] zsh-syntax-highlighting)
  • Source ~/.zshrc to take changes into account:
source ~/.zshrc

<img src="https://leanote.com/api/file/...;>
命令能夠正確高亮顯示咯,這樣能夠避免命令輸錯的狀況,是否是很智能

4.2.2 安裝 zsh-autosuggestions​​

Oh My Zsh 插件形式安裝

  • Clone this repository into $ZSH_CUSTOM/plugins (by default ~/.oh-my-zsh/custom/plugins)
git clone https://github.com/zsh-users/zsh-autosuggestions $ZSH_CUSTOM/plugins/zsh-autosuggestions
  • Add the plugin to the list of plugins for Oh My Zsh to load:
plugins=(zsh-autosuggestions)
  • Source ~/.zshrc
source ~/.zshrc

4.2.3 安裝 autojump

autojump是一個命令行工具,它可使用快捷命令,直接跳轉到配置好的目錄,而不用管如今身在何處,依賴zsh。
OS X
Homebrew is the recommended installation method for Mac OS X:(以前已經安裝homebrew)

brew install autojump

vim ~/.zshrc
plugins=( [plugins...] autojump) 添加autojump到.zshrc的plugins

新添加一行
[[ -s $(brew --prefix)/etc/profile.d/autojump.sh ]] && . $(brew --prefix)/etc/profile.d/autojump.sh

而後新開一個終端就已經可使用啦

4.3 iterm快捷鍵

open . 在當前目錄下打開finder
⌘ + return 全屏
⌘ + f 所查找的內容會被自動複製
⌘ + d 橫着分屏 / ⌘ + shift + d 豎着分屏令
⌘ + / 光標位置
⌘ + r 只是換到新一屏,不會像 clear 同樣建立一個空屏
ctrl + u 清除當前行
ctrl + a 到行首
ctrl + e 到行尾
ctrl + w 刪除光標以前的單詞
ctrl + k 刪除到文本末尾
⌘ + alt + 方向鍵 切換屏幕(用於hotkey window)
⌘ + 方向鍵 切換tab
ctrl + _ Undo
ctrl + y Paste the last thing to be cut

4.4 oh-my-zsh git插件別名

alias ga='git add'
alias gb='git branch'
alias gba='git branch -a'
alias gbd='git branch -d'
alias gcam='git commit -a -m'
alias gcb='git checkout -b'
alias gco='git checkout'
alias gcm='git checkout master'
alias gcp='git cherry-pick'
alias gd='git diff'
alias gfo='git fetch origin'
alias ggpush='git push origin $(git_current_branch)'
alias ggsup='git branch --set-upstream-to=origin/$(git_current_branch)'
alias glgp='git log --stat -p'
alias gm='git merge'
alias gp='git push'
alias gst='git status'
alias gsta='git stash save'
alias gstp='git stash pop'
alias gl='git pull'
alias glg='git log --stat'
alias glgp='git log --stat -p'

5. 安裝 PHP

Mac最新的系統已經自帶php,我的比較喜歡安裝最新的,因此這邊升級一下

brew search php   // 先搜索一下

brew install php   // 安裝,等待安裝完成

新開一個tab輸入php -v就能夠看到最新版本了

加入開機自啓
ln -sfv /usr/local/opt/php/*.plist ~/Library/LaunchAgents

6. 安裝 Composer

Dependency Manager for PHP , PHP 的一個依賴管理工具

brew search composer

brew install composer

安裝成功啦,以後咱們就能夠經過composer來安裝PHP的一些擴展包

7. 安裝 Mysql

brew install mysql  //安裝

安裝成功以下:
We've installed your MySQL database without a root password. To secure it run:
    mysql_secure_installation
 
MySQL is configured to only allow connections from localhost by default
 
To connect run:
    mysql -uroot
 
To have launchd start mysql now and restart at login:
  brew services start mysql
Or, if you don't want/need a background service you can just run:
  mysql.server start

啓動:
mysql.server start

加入開機自啓
ln -sfv /usr/local/opt/mysql/*.plist ~/Library/LaunchAgents

運行密碼配置
//運行mysql_secure_installation

mysql_secure_installation
 
Securing the MySQL server deployment.
 
Connecting to MySQL using a blank password.
 
VALIDATE PASSWORD PLUGIN can be used to test passwords  //密碼驗證插件,爲了提升安全性,須要驗證密碼
and improve security. It checks the strength of password     // 它會檢查密碼的強度
and allows the users to set only those passwords which are      //只容許用戶設置足夠安全的密碼
secure enough. Would you like to setup VALIDATE PASSWORD plugin?    //你肯定要安裝驗證密碼插件嗎?
 
Press y|Y for Yes, any other key for No: y      //肯定安裝
 
There are three levels of password validation policy:   //三個等級的驗證策略
 
LOW    Length >= 8   //最小長度大於等於8個字符
MEDIUM Length >= 8, numeric, mixed case, and special characters     //數字,字母,特殊字符 混合,具體的應該是至少1個數字,1個字母,1個特殊字符,長度不超過32個字符
STRONG Length >= 8, numeric, mixed case, special characters and dictionary file     //  最嚴格,加上了,字典文件
 
Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 0       //這裏我選擇0最簡單的,
Please set the password for root here.
 
New password:       //輸入密碼
 
Re-enter new password:      //重複輸入密碼
 
Estimated strength of the password: 50      //密碼強度的評級
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y    //是否使用剛輸入的密碼?
By default, a MySQL installation has an anonymous user,     //默認狀況下,MySQL有一個匿名用戶,
allowing anyone to log into MySQL without having to have     //這個匿名用戶,沒必要有一個用戶爲他們建立,匿名用戶容許任何人登陸到MySQL,
a user account created for them. This is intended only for   //這只是爲了方便測試使用
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production  //在正式環境使用的時候,建議你移除它
environment.
 
Remove anonymous users? (Press y|Y for Yes, any other key for No) : y    //提示移除匿名用戶
Success.
 
 
Normally, root should only be allowed to connect from    //通常狀況下,root用戶只容許使用"localhost"方式登陸,
'localhost'. This ensures that someone cannot guess at  
the root password from the network.  // 以此確保,不能被某些人經過網絡的方式訪問
 
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : no    //不容許root遠程登錄?
 
 ... skipping.
By default, MySQL comes with a database named 'test' that     //默認狀況下,MySQL數據庫中
anyone can access. This is also intended only for testing,        //這也僅僅是爲了測試
and should be removed before moving into a production             // 在正式環境下,應該移除掉
environment.
 
 
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y  //確認刪除test數據庫?
 - Dropping test database...
Success.
 
 - Removing privileges on test database...
Success.
 
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.         //刷新權限表,以確保全部的修改能夠馬上生效
 
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y      //確認刷新
Success.
 
All done!

8. 安裝 Laravel Valet

這邊我說明一下,本該安裝 NGINX,由於我所接觸的項目都是 laravel 或者 lumen,因此我這邊安裝這個 laravel 官方支持的 valet 開發環境,這個其實自己就是運用的 nginx,只是使用這個開發環境,開發 laravel 的不少項目就很是方便,我我的也比較建議使用,目前支持 Mac 和 linux 系統。
前面已經安裝php和composer,這邊就能夠直接安裝

composer global require laravel/valet  //先獲取項目
valet install       //而後安裝
ping foobar.test    //ping一下是否通

在家目錄建立Sites文件夾,以後全部項目都放在這個文件夾中,訪問的話也是文件夾名+.test就能夠了

mkdir ~/Sites
cd ~/Sites
valet park  //將這個目錄設置爲項目倉庫
valet path  //查看valet 倉庫路徑

這樣就能夠以這樣的  http://blog.test 域名訪問了

若是有切換PHP版本需求的用戶,我以爲這個 brew-php-switcher 可能會比較適合

9. 安裝 Redis

brew search redis
brew install redis

默認配置在/usr/local/etc/redis.conf

redis-server

設置開機啓動
ln -sfv /usr/local/opt/redis/*.plist ~/Library/LaunchAgents 

使用launchctl啓動redis server
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.redis.plist

最基本的PHP開發環境已經搭建完成,能夠創造世界了!哈哈

相關文章
相關標籤/搜索