多版本PHP部署及composer配置

mac

#安装php5.6
brew install php56
#安装php7.1
brew install php71
#安装composer
brew install composer

此时composer对应的php版本是5.6

现在我们有一个项目A是php5.6的,有一个项目B是php7.1的

访问项目A要求用的是php5.6版本,访问项目B时要求用的是php7.1

#方案一
#查看当前php版本
php -v
#假设结果是5.6
#访问项目B的时候
brew unlink php56
brew link php71
brew services start php71
#此时再查看php版本已切换至php7.1
php -v
#如果要切回php5.6
brew unlink php71
brew link php56
brew services start php56
#方案二
#配置项目A
#nginx配置
location ~ \.php$ {
    ...
    fastcgi_pass 127.0.0.1:9000
}
#配置项目B
#修改php7.1的php-fpm.conf监听端口
listen = 127.0.0.1:9001
#nginx配置
location ~ \.php$ {
    ...
    fastcgi_pass 127.0.0.1:9001
}
#重启nginx和php

推荐使用方案二

多版本PHP的composer配置

用phpstorm打开项目A和项目B

在项目A中执行composer命令是正常的

在项目B中执行composer报错,因为项目B在phpstorm打开时配置的php7.1的版本

那该如何配置多版本PHP的composer呢?

通过上文brew安装的composer对应的是php5.6,因此全局的composer对应的是php5.6

故而我们需要安装的对应php7.1的composer

1,下载composer,https://getcomposer.org/download/,将下载的composer.phar移动到~/composer7目录下

#创建composer7目录
mkdir ~/composer7
#下载composer.phar
cd ~/composer7
wget https://getcomposer.org/download/1.6.5/composer.phar

2,配置composer7

#编辑~/.bash_profile,创建php7和composer7的别名
vi ~/.bash_profile
alias php7='/usr/local/Cellar/php71/7.1.11_22/bin/php'
alias composer7='/usr/local/Cellar/php71/7.1.11_22/bin/php /Users/my/composer7/composer.phar'
#'usr/local/Cellar/php71/7.1.11_22'即php7.1安装的目录
#'/Users/my/composer7'即下载的所在路径composer.phar

3,使~/.bash_profile生效

#使~/.bash_profile生效
source ~/.bash_profile
#由于是mac,用的是zsh,重启后~/.bash_profile会失效,故需要编辑~/.zshrc
vi ~/.zshrc
source /User/my/.bash_profile
#/User/my是当前用户的根目录
#编辑~/.zshrc后,mac重启是会执行./zshrc,故而重启后./bash_profile会被载入,其中的命令别名会生效

4,查看php7和composer7

#查看php7命令对应的php版本
php7 -v
#查看composer7
composer7 -v


你可能感兴趣的:(PHP,laravel,linux)