macOS High Sierra 10.13.3 全新搭建 PHP 开发环境

用了快三年的 Mac Pro 出现问题,苹果公司帮忙换了一台全新的,所以没办法,只能重新装环境,装平时开发所需的各种软件,本着乐于助人的雷锋精神也把这次搭建写出来,希望可以帮助更多人。

废话不多说进入正题

macOS Sierra 已经帮我们预装了 Ruby、PHP、Perl、Python 等常用的脚本语言,以及 Apache 服务器等等。

1.安装 Xcode

Xcode 是苹果出品的包含一系列工具及库的开发软件。
通过 App Store 安装最新版本的 Xcode。(如果你的Xcode是8.2之前的版本的话,在你编译PHP7.0+的时候会提示你“更新Xcode”版本)
我们一般不会用 Xcode 来开发 PHP 项目。但这一步也是必需的,因为 Xcode 会帮你附带安装一些如 Git 等必要的软件。当然你也可以通过源码包安装 Git
N多软件包都基于Xcode,既然必须要安装,所以我就把这个安装放在第一步。

安装成功,哎公司网络比较差,只能晚上回家安装,大概等了2小时吧,size 5G左右,所以网络不好就提前安装一下吧。

2.安装 Xcode Command Line Tools

这一步会帮你安装许多常见的基于 Unix 的工具。Xcode 命令行工具作为 Xcode 的一部分,包含了 GCC 编译器。在命令行中执行以下命令即可安装:

xcode-select --install

直接点击install,然后等待安装完成就好,哎等待总是漫长的。
Verify that you’ve successfully installed Xcode Command Line Tools:

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

3.安装 Homebrew

HomeBrew 是macOS 软件包管理器,用来安装、升级以及卸载常用的软件

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

及时按回车键然后输入开机密码,等待安装完成
有时homebrew核心仓库没有我们所要的安装包,但在其他仓库中有,那我们可以自己添加

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 "$(wget https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh -O -)"

哇哦,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 配色设置选择一个(我比较喜欢这种类型的颜色)
ethanschoonover.com/solarized 配色主题
继续修改字体,改成刚安装成功的字体

然后重开一个终端,发现会带上自己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

命令可以正确高亮显示咯,这样可以避免命令输错的情况,是不是很智能

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

查看权重
j --stat

然后新开一个终端就已经可以使用啦

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

如需切换PHP版本,可以使用 brew-php-switcher

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一下是否通

添加环境变量:
安装好了 valet 之后,我们需要将 valet 添加环境变量中才能使用对应指令:我的 Mac 电脑变量配置文件通常是: ~/.zshrc 一般还会有~/.bashrc ~/.bash_profile 如果你电脑没有这些文件,那么就手动创建;

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

在家目录创建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开发环境已经搭建完成,可以创造世界了!哈哈,如果有其他安装需求或者不懂的,可以在评论区评论,如有问题或者错误还请各位大佬明示。

  • 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
  • 文章来源https://blog.arunfung.com
本作品采用《CC 协议》,转载必须注明作者和本文链接
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。 文章来源blog.arunfung.com
本帖由 Summer 于 5年前 加精
arunfung
《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 34
wanghan

我更关心的是为什么苹果公司给换了新的

5年前 评论
arunfung

@wanghan 哈哈你关心的点很不一样啊

5年前 评论

Valet 确实挺喜欢的,只可惜,切换PHP版本比较麻烦。

5年前 评论
arunfung

@Ali 如果你是使用Mac os 我觉得这个会适合你 brew-php-switcher

5年前 评论

插眼码住 萌新买不起苹果本 以后买了用

5年前 评论
arunfung

@王某人的用户名 哈哈,加油,好好学习,努力赚钱

5年前 评论

版本更新,还得重来。。。麻烦,还是 homestead 好,还能统一大家的配置。

5年前 评论
oyghan 4年前

不过还是要赞下你的教程,对于不随便升级的同学还是很有用的。

5年前 评论
arunfung

@Hatcher 哈哈,需求对应才能体现出价值,我是跳过homestead,使用docker了,跟线上环境也统一的。

5年前 评论

@arunfung 你可以整一篇 docker 的安装教程造福群众呀 :grinning:

5年前 评论

赞哇,😄😄,但是 brew 现在已经不支持 php 的老版本了,若需要安装 php5.6 , php7.0 的版本,可以参考这篇 文章 ,不同项目不同 php 版本的话,可以参考社区 @Destiny 大佬的 文章

4年前 评论
Destiny
# php7.0
brew tap exolnet/homebrew-deprecated
brew install php@7.0

:kissing_heart:

4年前 评论
Destiny

@Ali

最新版本 valet 支持切换 php 版本,不过是所有站点版本都切换

valet use php@7.0
4年前 评论

@Destiny 文章的灵魂就是这俩命令

4年前 评论
arunfung

@Destiny @sayhe110 O(∩_∩)O哈哈~,我这篇文章还是去年这个时候写的,看来是时候来一版2019的了。多谢各位大神指出 :+1: :+1: :+1:

4年前 评论

@Destiny 啊啊!真是呢刚看了文档,只可惜没有早一点支持。。

4年前 评论
Destiny

@Ali valet 切换 php 吗?如果想达到多个站点使用不同版本可以看看我之前写的文章。 :smiley:

4年前 评论

@Destiny 我看了文档没说支持 多版本的PHP,就放弃了,因为我记得 homestead 支持,我就重新安装了 homestead。。现在找你的文章出来看看。

4年前 评论
Destiny

@Ali 因为 homestead 本身虚拟了一个 Ubuntu 虚拟环境,可以任意修改。

4年前 评论

想再本地也搭个环境,不太熟悉mac ,和目录,可以推荐一下吗 ;如:node.js ,nginx, git 安装那个文件夹下, 项目 统一在那个目录下,萌新 一枚 ,谢谢

4年前 评论
arunfung

@qinplain 使用homebrew安装的所有软件都在 /usr/local/Cellar 目录下,配置文件在 /usr/local/etc 目录下,基本所有的软件都可以通过 brew 安装,而且十分方便,所有的依赖及环境变量都会帮我们处理好。

4年前 评论

@arunfung 谢谢, 以前一直都是window10 下开发,现在换了 系统,很不习惯,不能按自己在window10 开发那样,先模仿你们的开发方式,向 linux 方向发张

4年前 评论

我的本本配置基本一毛一样,brew+valet+item2+ohmyzsh,
然后本地装了mysql、redis、swoole
然后docker也装了,本地不够用就docker加,但是基本上本地都足够

4年前 评论

@qinplain 完全不用管,傻瓜式操作,软件下载下来,拖到app目录,然后直接开终端敲就ok了

4年前 评论
arunfung

@aen233 :grinning:真的一毛一样么? 目前我就docker起了mysql8.0,自己研究使用

4年前 评论

@arunfung 除了ohmyzsh的主题吧,我用的是粉色的,叫 gianu
我也是docker装了mysql8.0,不过去年8月份折腾的时候,因为mysql8.0的加密解密方式变了,如果要连sequelPro或navicat都需要折腾一下,还有mailhog这类的

4年前 评论
arunfung

@aen233 pink,小姐姐专用款, :smiley:你只需要命令行对MySQL的密码修改一次,就可以连接了

4年前 评论

为啥我安装valet之后连不上外网了,安装了两次,同样的效果,17款macbookpro,系统版本忘了

4年前 评论
arunfung

@小刀客 什么外网?可以再具体一些么?

4年前 评论

@arunfung 就是浏览器打开显示没有连接网络。

4年前 评论

redis php 扩展没有。建议添加一下哦

2年前 评论

本地valet配置的项目如何让局域网内电脑通过本地ip访问呢。还请指导一下

2年前 评论
arunfung

@Undefined_fish 编辑 /usr/local/etc/nginx/valet/valet.conf 配置文件的 listen ,将 127.0.0.1: 去掉,只监听80端口,然后其他同学本地host配置 192.168.0.0(你的ip) laravel.test(你的虚拟域名) ,然后通过这个域名就可以访问了,记得改了nginx要重启一下 valet restart

2年前 评论

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