醉丶春风的Blog

千里之行, 始于足下



Mac实现多版本php共存,根据nginx server自动切换的方法


Mac实现多版本php共存,根据nginx server自动切换

本文目标

在这里我会为大家演示在mac上安装php5.6,php7.1,php7.3这三个版本的php并通过配置, 实现访问不同的虚拟域名,自动切换php版本

我的Mac版本是 10.15.1 Catalina

1. 安装Homebrew

已安装过的直接跳过

官网:https://brew.sh/index_zh-cn

安装方法, 执行下面的命令即可, 由于国内网的问题,安装可能会很慢,最好全程都挂代理

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

2. 安装各个版本的PHP

由于最新版的brew废弃了`php7.2以下的版本,如果想要安装7.2以下的版本,需要手动添加软件源, 在这之前, 可以使用下面的命令搜索一下php有哪些版本

brew search php

正常应该只有php@7.2, php@7.3这两个版本

2.1 添加软件源,安装低版本php

如果不需要低版本php, 这一步可以略过

brew tap exolnet/homebrew-deprecated

使用brew tap命令,添加一个第三方软件源(third-party-repositories)

此时再次使用brew search php命令,会发现多出另外几个版本的php,如图

alt

2.2 安装php7.3

brew install php@7.3

2.3 安装php7.1

brew install php@7.1

2.4 安装php5.6

brew install php@5.6

2.5 安装其他版本的php

还剩下 php@7.2, php@7.0, 各位可以自行安装

3. 创建自己的php命令

3.1 查看当前的php版本

使用下面的命令

ll /usr/local/bin/php

3.2 解决php5.6 php-v报错

在上一步中, 如果查看当前的php环境变量,指向的是php5.6的话, 此时使用php-v, 会有如下报错

dyld: Library not loaded: /usr/local/lib/libcrypto.1.0.0.dylib
  Referenced from: /usr/local/Cellar/php56/5.6.11_1/bin/php
  Reason: image not found

这是因为php5.6使用openssl依赖是1.0的,而新版的homebrew已经淘汰了1.0版本的openssl, 使用的是openssl@1.1

解决方法:

方法1:参考 https://github.com/Homebrew/homebrew-php/issues/1901

使用brew link openssl --force, 可能需要重新安装php5.6

重新安装方法 brew reinstall php@5.6

方法2:brew install https://github.com/tebelorg/Tump/releases/download/v1.0.0/openssl.rb

安装1.0版本的openssl

3.3 创建各个版本的php软链接到环境变量

为了方便管理,我在宿主目录下新建了一个文件夹存放所有我配置的命令

mkdir -p ~/program/bin # 创建存放命令的文件夹
cd ~/program/bin # 进入到该文件夹

brew安装的php命令所在的文件夹是/usr/local/opt/php@7.1/bin

其中php@7.1为对应版本的目录, 还有php@5.6, php@7.3两个目录,如果你安装了另外版本的php, 还有对应该版本的文件夹

alt

下面开始创建软链接,此时我们在 ~/program/bin目录下

ln -s /usr/local/opt/php@5.6/bin/php ./php56
ln -s /usr/local/opt/php@7.1/bin/php ./php71
ln -s /usr/local/opt/php@7.3/bin/php ./php73

将该目录添加到环境变量

如果你用的是zsh

echo 'export PATH="/Users/xushantong/program/bin:$PATH"' >> ~/.zshrc
source ~/.zshrc

如果你用的是bash

echo 'export PATH="/Users/xushantong/program/bin:$PATH"' >> ~/.bashrc
source ~/.bashrc

注意把用户名替换成你自己的用户名

此时可以使用php56, php71, php73来代指对应版本的php命令了

php56 -v
php71 -v
php73 -v

如果此时php56 -v出错了, 可以参考3.2步骤

4. 更改各个版本php-fpm监听的端口号

php-fpm所在的目录为 /usr/local/etc/php, 这个文件夹下存放了各个版本的php-fpm配置文件以及php.ini

4.1 修改php5.6 php-fpm监听端口

cd /usr/local/etc/php/5.6
sudo vim php-fpm.conf

找到listen = 127.0.0.1:9000, 修改成 listen = 127.0.0.1:9056

4.2 修改php7.1. php-fpm监听端口

cd /usr/local/etc/php/7.1
sudo vi php-fpm.d/www.conf

找到listen = 127.0.0.1:9000, 修改成 listen = 127.0.0.1:9071

注意:php7.1和php7.3的监听端口在 php-fpm.d/www.conf 这个文件中,原因是,在php-fpm.conf中没有这个配置 ,同时该文件最后一行引入了php-fpm.d文件夹下面的所有文件

alt

4.3 修改php7.3 php-fpm监听端口

cd /usr/local/etc/php/7.3
sudo vi php-fpm.d/www.conf

找到listen = 127.0.0.1:9000, 修改成 listen = 127.0.0.1:9073

5. 安装配置nginx

5.1 安装nginx

brew install nginx

5.2 配置nginx server

nginx的配置文件所在目录为/usr/local/etc/nginx

该目录下的nginx.conf引入了./servers下的所有文件, 我们只要在这个目录添加配置文件即可

cd /usr/local/etc/nginx/servers
touch test1.conf # 创建两个测试server
touch test2.conf

test1.conf中添加如下内容

sudo vim test1.conf
server {
    listen       80;
    server_name  test71.com;

         # 将此处的用户名修改为你自己的用户名
    root /Users/xushantong/vhosts/test71;
    index index.html index.php;

    #charset koi8-r;

    #access_log  logs/host.access.log  main;


    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }

    location ~ \.php$ {
            # 将php请求转发到9071端口,也就是php7.1进行处理
        fastcgi_pass   127.0.0.1:9071;
        fastcgi_index  index.php;
        #fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
}

test2.conf中添加如下内容

sudo vim test2.conf
server {
    listen       80;
    server_name  test73.com;

        # 将此处的用户名修改为你自己的用户名
    root /Users/xushantong/vhosts/test73;
    index index.html index.php;

    #charset koi8-r;

    #access_log  logs/host.access.log  main;


    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }


    location ~ \.php$ {
            # 将php请求转发到9073端口,也就是php7.3进行处理
        fastcgi_pass   127.0.0.1:9073;
        fastcgi_index  index.php;
        #fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
}

使用nginx -t 测试配置文件是否正确

alt

如图所示,是正确的

6. 添加hosts, 添加测试代码

添加hosts

sudo vim /etc/hosts

粘贴下面两行

127.0.0.1 test71.com
127.0.0.1 test73.com

添加测试代码

mkdir -p ~/vhosts/test71
mkdir -p ~/vhosts/test73
echo '<?php echo phpinfo();' > ~/vhosts/test71/index.php
echo '<?php echo phpinfo();' > ~/vhosts/test73/index.php

7. 验证

先启动所有用到的php-fpm, 因为是自动切换, 所以要先启动

brew services start php@7.1
brew services start php@7.3

启动nginx

brew services start nginx
# 如果之前已经启动过nginx了, 修改了配置文件需要重新加载
brew services reload nginx

8. 查看

分别访问下面两个域名, 来看效果吧

访问 http://test71.com

访问 http://test73.com

题外话

在使用过程中,我发现每次都使用 brew services,每次都要手打一遍,程序员怎么能容忍这种麻烦呢,所以我为这个命令设置了一个别名 bs

echo "alias bs='brew services'" >> ~/.zshrc # 如果用的是bash, 修改为 .bashrc

以后只要使用 bs start php@7.1, bs stop nginx, bs list, 很爽有没有


作者: 徐善通
地址: https://www.xstnet.com/article-146.html
声明: 除非本文有注明出处,否则转载请注明本文地址


我有话说



最新回复


正在加载中....

Copyrights © 2016-2019 醉丶春风 , All rights reserved. 皖ICP备15015582号-1