macOS Monterey 12.2 搭建 GO+PHP 開發環境

arunfung發表於2022-01-28

公司配的電腦也已經使用了一年多了,期間也升級了系統,使用了 beta 版的MacOS 12,體驗也還是不錯的,但由於使用的時間也比較久了,系統快取佔了一半,並且還刪不掉,以及電腦時不時卡頓後自動重啟。所以就趁著春節把系統重灌一下,提升下使用體驗。

1、安裝 Xcode、Xcode Command Line Tools

Xcode 是蘋果出品的包含一系列工具及庫的開發軟體。目前最新版本是 13.2.1 可以通過App Store 安裝,其主要目的是避免安裝其他軟體提示更新 Xcode,軟體有 12G 之大,還是找個網好點的地方安裝吧。

Xcode Command Line Tools 作為 Xcode 的一部分,包含了 GCC 編譯器。在命令列中執行以下命令即可安裝:

xcode-select --install

直接點選安裝,然後等待安裝完成

xcode-select -p

返回以下表示安裝成功

/Applications/Xcode.app/Contents/Developer

2.安裝 Homebrew

HomeBrew 是 macOS 軟體包管理器,用來安裝、升級以及解除安裝常用的軟體

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

或者使用國內源

/bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)"

3. 安裝 iTerm2

iTerm2 是 MAC 下最好的終端工具(沒有之一)以及配合oh-my-zsh 及其外掛,將是強大的神器

下載 iTerm2,開啟會提示移動到 Application,或者在 Finder 中,將 iTerm 拖拽進入 Application 資料夾中。這樣,你可以在 Launchpad 中啟動 iTerm2。

3.1 安裝 oh-my-zsh

oh-my-zsh Mac 自帶 zsh 版本較低,可以安裝最新版 brew install zsh

下面安裝 oh-my-zsh


sh -c "$(wget https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh -O -)"

iTerm2 的介面瞬間變清爽了

換個主題,我比較喜歡下面這個主題

agnoster.zsh-theme

還有各種主題自行選擇

各種主題


sudo vim ~/.zshrc //開啟這個配置檔案

ZSH_THEME="robbyrussell" 找到這行主題配置

ZSH_THEME="agnoster" 更換成agnoster的主題

這個主題需要安裝一個字型,才能正常顯示 Powerline fonts


git clone https://github.com/powerline/fonts.git --depth=1

cd fonts

./install.sh

cd ..

rm -rf fonts

preferences > profiles > colors 修改配色

preferences > profiles > text 選擇 fria mono for powerline 字型


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" 改成這樣就可以了

這樣做的原因是避免升級有衝突

3.2 外掛

oh my zsh 自帶外掛

Oh My Zsh 本身自帶了很多外掛,比如 git,外掛目錄: ~/.oh-my-zsh/plugins

我主要使用的兩個是:

1、命令高亮顯示 [zsh-syntax-highlighting]github.com/zsh-users/zsh-syntax-hi...)

2、自動補全命令 zsh-autosuggestions

git clone 到本地


git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions

git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting
  • 配置

sudo vim ~/.zshrc

plugins=(git zsh-autosuggestions zsh-syntax-highlighting)

Source ~/.zshrc

3.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. 安裝 PHP、Composer、GO、Nginx

macOS 12 系統不會預設安裝 PHP 和 GO,所以我們需要自行安裝。


brew install php composer go nginx // 使用 brew 安裝非常簡單

# composer 設定本地倉庫路徑

composer config repositories.arunfung path ../shop

4.1. 安裝 Laravel Valet

valetlaravel 官方提供的一個開發環境,主要優勢就是簡單方便,開發 PHP 專案就足夠使用。(安裝 valet 需提前安裝 composer 和 php)。


composer global require laravel/valet //先獲取專案

valet install //然後安裝

新增環境變數:安裝好了 valet 之後,我們需要將 valet 新增環境變數中才能使用對應指令


sudo vim ~/.zshrc

# 新增到檔案最後

export PATH="$PATH:$HOME/.composer/vendor/bin"

source ~/.zshrc

在家目錄建立Sites資料夾,之後所有專案都放在這個資料夾中,訪問的話也是資料夾名+.test就可以了


mkdir ~/Sites

cd ~/Sites

valet park //將這個目錄設定為專案倉庫

valet path //檢視valet 倉庫路徑

這樣就可以訪問類似 http://blog.test 的域名

# 如需切換不同 PHP 版本你可以這樣切換

valet use php@7.2

valet use php

9. 安裝 Redis、Etcd、MySQL、Nsq等基礎元件


brew install redis etcd nsq mysql

現在預設版本的 MySQL 已經是8了,如果5.7版本就安裝制定版本即可。

加入開機自啟

ln -sfv /usr/local/opt/mysql/*.plist ~/Library/LaunchAgents

MySQL 配置密碼

//執行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!
本作品採用《CC 協議》,轉載必須註明作者和本文連結
著作權歸作者所有。商業轉載請聯絡作者獲得授權,非商業轉載請註明出處。 文章來源blog.arunfung.com

相關文章