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

栏目: PHP · 发布时间: 4年前

内容简介:用了快三年的Mac Pro出现问题,苹果公司帮忙换了一台全新的,所以没办法,只能重新装环境,装平时开发所需的各种软件,本着乐于助人的雷锋精神也把这次搭建写出来,希望可以帮助更多人。废话不多说进入正题macOS Sierra 已经帮我们预装了 Ruby、PHP、Perl、Python 等常用的脚本语言,以及 Apache 服务器等等。

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

废话不多说进入正题

macOS Sierra 已经帮我们预装了 RubyPHPPerlPython 等常用的脚本语言,以及 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

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

直接点击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)"
macOS High Sierra 10.13.3全新搭建PHP开发环境

及时按回车键然后输入开机密码,等待安装完成

有时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 "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"

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

哇哦,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/solarized 配色主题

继续修改字体,改成刚安装成功的字体

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

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

macOS High Sierra 10.13.3全新搭建PHP开发环境 命令可以正确高亮显示咯,这样可以避免命令输错的情况,是不是很智能

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 域名访问了

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Python 3面向对象编程

Python 3面向对象编程

[加]Dusty Phillips(达斯帝•菲利普斯) / 肖鹏、常贺、石琳 / 电子工业出版社 / 2015-6 / 79.00元

Python 是一种面向对象的解释型语言,面向对象是其非常重要的特性。《Python 3面向对象编程》通过Python 的数据结构、语法、设计模式,从简单到复杂,从初级到高级,一步步通过例子来展示了Python 中面向对象的概念和原则。 《Python 3面向对象编程》不是Python 的入门书籍,适合具有Python 基础经验的开发人员阅读。如果你拥有其他面向对象语言的经验,你会更容易理解......一起来看看 《Python 3面向对象编程》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具