macOS Monterey 12.2 搭建 GO+PHP 开发环境

公司配的电脑也已经使用了一年多了,期间也升级了系统,使用了 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
arunfung
《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!