Table of Contents
1.安装PHPbrew
2.简单使用
3.编译安装PHP
4.PHP编译参数
5.其他配置选项
6.使用和切换
7.显示已经安装过的PHP版本
8.管理FPM
#实践笔记
Home php教程 php手册 PHP的多版本环境管理

PHP的多版本环境管理

Jun 06, 2016 pm 08:14 PM
php Version environmental management

大多数语言编程的时候,都会碰到在不同版本下的代码兼容性问题,测试代码兼容性,或者调试老旧代码,需要不同的语言版本环境. 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>
Copy after login

2.简单使用

为你的shell环境运行初始化脚本:

<code>phpbrew init
</code>
Copy after login

设置环境变量:

<code>sudo vi ~/.bashrc
#文件最后,插入下面这行代码
source ~/.phpbrew/bashrc
#保存退出
:wq
</code>
Copy after login

显示已知版本:

<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>
Copy after login

显示旧版本:

<code>phpbrew known --old
</code>
Copy after login

更新版本列表:

<code>phpbrew known --update
</code>
Copy after login

3.编译安装PHP

默认配置安装PHP:

<code>phpbrew install 5.4.0 +default
</code>
Copy after login

测试安装:

<code>phpbrew install --test 5.4.0
</code>
Copy after login

显示debug信息:

<code>phpbrew -d install --test 5.4.0
</code>
Copy after login

安装旧版本:

<code>phpbrew install --old 5.2.13
</code>
Copy after login

清理编译目录:

<code>phpbrew clean
</code>
Copy after login

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>
Copy after login

5.其他配置选项

<code>phpbrew install 5.3.10 +mysql +sqlite -- \
    --enable-ftp --apxs2=/opt/local/apache2/bin/apxs
</code>
Copy after login

6.使用和切换

临时使用:

<code>phpbrew use 5.4.22
</code>
Copy after login

切换版本(设置默认版本):

<code>phpbrew switch 5.4.18
</code>
Copy after login

关闭:

<code>phpbrew off
</code>
Copy after login

7.显示已经安装过的PHP版本

<code>phpbrew list
</code>
Copy after login

8.管理FPM

  NGINX需要配合php-fpm使用,因此,如果是使用 LNMP 或者自己安装的NGINX+PHP的运行环境,则需要在phpbrew安装PHP的时候加上+fpm 模块,才能使用phpbrew的模块管理.

<code>phpbrew install 5.6.3 +fpm
</code>
Copy after login

启动FPM:

<code>phpbrew fpm start
</code>
Copy after login

停止FPM

<code>phpbrew fpm stop
</code>
Copy after login

显示php-fpm的模块:

<code>phpbrew fpm module
</code>
Copy after login

测试php-fpm的配置

<code>phpbrew fpm test
</code>
Copy after login

配置php-fpm

<code>phpbrew fpm config
</code>
Copy after login

更多  扩展安装   启用扩展  配置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>
Copy after login

安装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>
Copy after login

安装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>
Copy after login

注意如果配置或编译的时候出现报错,请查看此页面 Requirement

安装PHP版本

测试默认config

<code>phpbrew install 5.6.3 --test +default
tail -f /home/rming/.phpbrew/build/php-5.6.3/build.log
</code>
Copy after login

默认:

<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>
Copy after login

精简:

<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>
Copy after login
  • +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扩展 , 很常用的,你懂得
  • +mcryptphpmyadmin需要
  • +filter filter_var过滤函数

写个脚本切换php-fpm:

<code>function nginxenv(){
        phpbrew fpm stop &>/dev/null;
        phpbrew switch ${1};
        phpbrew fpm start &>/dev/null;
}
</code>
Copy after login

  保存文件到~/.phpbrew/nginxenv , 然后在~/.bashrc最后加上 source ~/.phpbrew/nginxenv , 然后命令行里就可以 使用nginxenv 5.3.29这样切换版本了.

参考:

  • PHPbrew Cookbook
  • manual
  • Requirement
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

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

CakePHP Working with Database CakePHP Working with Database Sep 10, 2024 pm 05:25 PM

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

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

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

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

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

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

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

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

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

CakePHP Logging CakePHP Logging Sep 10, 2024 pm 05:26 PM

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

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

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

See all articles