PHP的多版本环境管理
大多数语言编程的时候,都会碰到在不同版本下的代码兼容性问题,测试代码兼容性,或者调试老旧代码,需要不同的语言版本环境. ruby有了rbenv,rvm,nodejs也有nvm,当然php也应该有个类似的 phpenv , php-version , phpbrew 来管理PHP版本,简单查看了下这三个工具,
大多数语言编程的时候,都会碰到在不同版本下的代码兼容性问题,测试代码兼容性,或者调试老旧代码,需要不同的语言版本环境.
ruby有了rbenv,rvm,nodejs也有nvm,当然php也应该有个类似的 phpenv , php-version , phpbrew 来管理PHP版本,简单查看了下这三个工具,我更偏向于phpbrew一些,其实我更愿意叫它PHPbrew.
如果你确定需要使用phpbrew,请认真读完本文,再去实践,请注意!前方有坑(其实我更推荐 vagrant ).
Brew & manage PHP versions in pure PHP at HOME
1.安装PHPbrew
下载:
<code>curl -L -O https://github.com/phpbrew/phpbrew/raw/master/phpbrew chmod +x phpbrew sudo mv phpbrew /usr/bin/phpbrew </code>
2.简单使用
为你的shell环境运行初始化脚本:
<code>phpbrew init </code>
设置环境变量:
<code>sudo vi ~/.bashrc #文件最后,插入下面这行代码 source ~/.phpbrew/bashrc #保存退出 :wq </code>
显示已知版本:
<code>phpbrew known ===> Fetching release list... [==================================================================] 29.67/29.67KB 100% 5.6: 5.6.3, 5.6.2, 5.6.1, 5.6.0 ... 5.5: 5.5.19, 5.5.18, 5.5.17, 5.5.16, 5.5.15, 5.5.14, 5.5.13, 5.5.12 ... 5.4: 5.4.35, 5.4.34, 5.4.33, 5.4.32, 5.4.31, 5.4.30, 5.4.29, 5.4.28 ... 5.3: 5.3.29, 5.3.28, 5.3.27, 5.3.26, 5.3.25, 5.3.24, 5.3.23, 5.3.22 ... You can run `phpbrew update` to get a newer release list. </code>
显示旧版本:
<code>phpbrew known --old </code>
更新版本列表:
<code>phpbrew known --update </code>
3.编译安装PHP
默认配置安装PHP:
<code>phpbrew install 5.4.0 +default </code>
测试安装:
<code>phpbrew install --test 5.4.0 </code>
显示debug信息:
<code>phpbrew -d install --test 5.4.0 </code>
安装旧版本:
<code>phpbrew install --old 5.2.13 </code>
清理编译目录:
<code>phpbrew clean </code>
4.PHP编译参数
显示可用参数:
<code>phpbrew variants Variants: all, apxs2, bcmath, bz2, calendar, cgi, cli, ctype, curl, dba, debug, dom, embed, exif, fileinfo, filter, fpm, ftp, gcov, gd, gettext, gmp, hash, iconv, icu, imap, inifile, inline, intl, ipc, ipv6, json, kerberos, libgcc, mbregex, mbstring, mcrypt, mhash, mysql, opcache, openssl, pcntl, pcre, pdo, pgsql, phar, phpdbg, posix, readline, session, soap, sockets, sqlite, static, tidy, tokenizer, wddx, xml, xml_all, xmlrpc, zip, zlib, zts Virtual variants: dbs: sqlite, mysql, pgsql, pdo mb: mbstring, mbregex neutral: default: bcmath, bz2, calendar, cli, ctype, dom, fileinfo, filter, ipc, json, mbregex, mbstring, mhash, mcrypt, pcntl, pcre, pdo, phar, posix, readline, sockets, tokenizer, xml, curl, openssl, zi Using variants to build PHP: phpbrew install php-5.3.10 +default phpbrew install php-5.3.10 +mysql +pdo phpbrew install php-5.3.10 +mysql +pdo +apxs2 phpbrew install php-5.3.10 +mysql +pdo +apxs2=/usr/bin/apxs2 </code>
5.其他配置选项
<code>phpbrew install 5.3.10 +mysql +sqlite -- \ --enable-ftp --apxs2=/opt/local/apache2/bin/apxs </code>
6.使用和切换
临时使用:
<code>phpbrew use 5.4.22 </code>
切换版本(设置默认版本):
<code>phpbrew switch 5.4.18 </code>
关闭:
<code>phpbrew off </code>
7.显示已经安装过的PHP版本
<code>phpbrew list </code>
8.管理FPM
NGINX需要配合php-fpm使用,因此,如果是使用 LNMP
或者自己安装的NGINX
+PHP
的运行环境,则需要在phpbrew安装PHP的时候加上+fpm
模块,才能使用phpbrew
的模块管理.
<code>phpbrew install 5.6.3 +fpm </code>
启动FPM:
<code>phpbrew fpm start </code>
停止FPM
<code>phpbrew fpm stop </code>
显示php-fpm的模块:
<code>phpbrew fpm module </code>
测试php-fpm的配置
<code>phpbrew fpm test </code>
配置php-fpm
<code>phpbrew fpm config </code>
更多 扩展安装 启用扩展 配置PHP 管理FPM 其他组件(composer,phpunit).
#实践笔记
OS X笔记:
安装准备:
<code>brew install automake autoconf curl pcre re2c mhash libtool icu4c gettext jpeg libxml2 mcrypt gmp libevent libpng freetype brew link icu4c ln -s /usr/local/include/freetype2 /usr/local/include/freetype2/freetype </code>
安装phpbrew:
<code>curl -L -O https://github.com/phpbrew/phpbrew/raw/master/phpbrew chmod +x phpbrew sudo mv phpbrew /usr/bin/phpbrew phpbrew init source ~/.phpbrew/bashrc phpbrew lookup-prefix homebrew </code>
安装php版本:
<code>#同样的在其他系统平台也会出现类似库找不到的问题,需要手动指定路径 phpbrew install 5.3.29 +default +fpm +pdo +mysql +sqlite +gd -- --with-gd=shared --with-jpeg-dir=/usr/local/Cellar --with-png-dir=/usr/local/Cellar --enable-gd-native-ttf --with-freetype-dir=/usr/local/Cellar #print_r(gd_info()); </code>
注意:如果配置或编译的时候出现报错,请查看此页面 Requirement
安装PHP版本
测试默认config
<code>phpbrew install 5.6.3 --test +default tail -f /home/rming/.phpbrew/build/php-5.6.3/build.log </code>
默认:
<code>phpbrew install 5.6.3 +default +fpm +pdo +mysql +sqlite +gd tail -f /home/rming/.phpbrew/build/php-5.6.3/build.log </code>
精简:
<code>phpbrew install 5.6.3 +fpm +json +dbs +mb +curl +mcrypt +filter tail -f /home/rming/.phpbrew/build/php-5.6.3/build.log </code>
-
+json
是因为phpbrew本身是个phar包,依赖于php的json函数 -
+fpm
是为了nginx
,phpbrew安装的php-fpm默认listen=127.0.0.1:9000
,因此需要把之前lnmp的配置文件fastcgi_pass
选项更改为127.0.0.1:9000
; -
+dbs
是因为没有安装mysqli扩展,想用个phpmyadmin都困难,囧; -
+mb
是多种语言编码字节数问题,为了中文字符截断和匹配使用 -
+curl
curl扩展 , 很常用的,你懂得 -
+mcrypt
phpmyadmin需要 -
+filter
filter_var过滤函数
写个脚本切换php-fpm:
<code>function nginxenv(){ phpbrew fpm stop &>/dev/null; phpbrew switch ${1}; phpbrew fpm start &>/dev/null; } </code>
保存文件到~/.phpbrew/nginxenv
, 然后在~/.bashrc
最后加上 source ~/.phpbrew/nginxenv
, 然后命令行里就可以 使用nginxenv 5.3.29
这样切换版本了.
参考:
- PHPbrew Cookbook
- manual
- Requirement
原文地址:PHP的多版本环境管理, 感谢原作者分享。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Validator can be created by adding the following two lines in the controller.

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c
