如何使用PHPbrew和VirtPHP

tech2022-09-22  67

We’ve all been in the situation where we have one version installed. Maybe that version is whatever came installed on our operating system. Maybe it is a version bundled into MAMP/WAMP/XAMPP.

我们所有人都已经安装了一个版本。 也许那个版本就是我们操作系统上安装的版本。 也许它是捆绑到MAMP / WAMP / XAMPP中的版本。

How do you go about switching that PHP version?

您如何切换该PHP版本?

How do you switch to one version, then switch back again?

如何切换到一个版本,然后再切换回?

How do you go about switching that version of PHP, but only for one single application on your computer?

您如何切换该版本PHP,但仅对计算机上的一个应用程序进行切换?

The Ruby and Python communities have had tools for dealing with this for years. PHP has them now too, but there was nowhere near enough fanfare.

多年来,Ruby和Python社区已经拥有处理此问题的工具。 PHP现在也有它们,但是没有那么夸张的地方了。

PHPbrew (PHPbrew)

PHPbrew is an amazing little tool which has been out since 2012.

自2012年以来, PHPbrew是一个了不起的小工具。

It builds and installs PHP versions, and places them into a folder in your home directory. This means you can manage your own versions of PHP. PHPbrew will build various versions, place them in the home folder, and let you switch between them whenever you want.

它生成并安装PHP版本,并将其放入您的主目录中的文件夹中。 这意味着您可以管理自己PHP版本。 PHPbrew将构建各种版本,将它们放置在home文件夹中,并让您在需要时在它们之间进行切换。

安装PHPbrew (Installing PHPbrew)

It should be worth noting that PHPbrew has a fair few requirements, but they are not tough to install. I did not have to install anything, as after using this Macbook for over two years I had all the requirements anyway.

值得注意的是,PHPbrew的需求很少,但安装起来并不困难。 我不需要安装任何东西,因为使用此Macbook超过两年后,我仍然满足所有要求。

If you are a Mac OS X user – and I will continue to assume that you are – then you can use Homebrew (no relation) to install dependencies.

如果您是Mac OS X用户,并且我将继续假设您是,那么您可以使用Homebrew(无关系)安装依赖项。

brew install automake autoconf curl pcre re2c mhash libtool icu4c gettext jpeg libxml2 mcrypt gmp libevent brew link icu4c

Then you will need to install PHPbrew itself:

然后,您将需要安装PHPbrew本身:

curl -L -O https://github.com/phpbrew/phpbrew/raw/master/phpbrew chmod +x phpbrew sudo mv phpbrew /usr/bin/phpbrew

This downloads PHPbrew, adds the “executable” permission and moves it to /usr/bin directory.

这将下载PHPbrew,添加“可执行”权限并将其移至/usr/bin目录。

Hop over to their basic usage instructions to see how to get things initialized in more detail, but the basics should just be:

跳到他们的基本用法说明,以了解如何更详细地初始化事物,但是基础应该只是:

phpbrew init

With phpbrew initialised you will need to add these lines to your .bashrc:

随着phpbrew的初始化,您将需要将这些行添加到.bashrc :

echo "source $HOME/.phpbrew/bashrc" >> ~/.bashrc

If you are using a non-default shell like ZSH then you will need to edit your .zshrc file instead.

如果使用非默认的shell,比如ZSH,那么你将需要编辑您的.zshrc文件来代替。

使用PHPbrew安装PHP (Installing PHP using PHPbrew)

Before we can install a version of PHP, we need to see which versions are available to PHPbrew. We can do this with a simple command:

在安装PHP版本之前,我们需要查看PHPbrew可以使用哪些版本。 我们可以用一个简单的命令来做到这一点:

phpbrew known Available stable versions: 5.6+ 5.6.0 5.5+ 5.5.17, 5.5.16, 5.5.15, 5.5.14, 5.5.13, 5.5.12, 5.5.11, 5.5.10 5.4+ 5.4.33, 5.4.32, 5.4.31, 5.4.30, 5.4.29, 5.4.28, 5.4.27, 5.4.26 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

At the time of writing, PHP 5.6.0 is the latest version, and versions of PHP before 5.3 are not supported.

在撰写本文时,PHP 5.6.0是最新版本,并且不支持5.3之前PHP版本。

We want to install PHP 5.6.0, so we can use all the great new features, so lets ask phpbrew to do that:

我们想要安装PHP 5.6.0,因此我们可以使用所有出色的新功能,因此让phpbrew做到这一点:

phpbrew install 5.6.0

Note that if you are using PHPbrew 1.14 or earlier then this would fail on some systems with an error about not having XML enabled. When XML is missing, PHPbrew will fail to install something called PEAR and the build will break. We can get around that using the +xml_all option:

请注意,如果您使用的是PHPbrew 1.14或更早版本,则在某些系统上此操作将失败,并显示有关未启用XML的错误。 如果缺少XML,PHPbrew将无法安装称为PEAR的内容,并且构建将中断。 我们可以使用+xml_all选项解决该问题:

phpbrew install 5.6.0 +xml_all

This +xml_all option is what PHPbrew calls “Variants”, and there are a lot more available.

这个+xml_all选项被PHPbrew称为“变量”,并且还有很多可用的方法。

变体 (Variants)

When installing PHP yourself, there are lots of options to enable or disable features. PHPbrew simplifies this and abstracts it, using a feature called Variants.

自己安装PHP时,有很多选项可以启用或禁用功能。 PHPbrew使用一个称为Variants的功能简化并抽象了它。

Things like database drivers, curl, the GD image library and JSON are all available as optional variants.

数据库驱动程序,curl,GD图像库和JSON之类的东西都可以作为可选变体使用。

PHPbrew has one variant called “default”, which – contrary to expectation – is not used by default. Instead it acts as a shortcut for enabling the following variants:

PHPbrew有一个称为“默认”的变体,与预期相反,默认情况下不使用。 相反,它充当启用以下变体的快捷方式:

bcmath

bcmath bz2

bz2 calendar

日历 cli

cli ctype

类型 dom

dom fileinfo

文件信息 filter

过滤 ipc

IPC json

json mbregex

mbregex mbstring

mbstring mhash

马什 pcntl

pcntl pdo

do posix

posix readline,

阅读专线 sockets

插座 xml_all

xml_all zip

压缩

The default may contain more than you need, so a more granular approach may be more to your liking.

default可能包含比您需要的更多的内容,因此更细粒度的方法可能更适合您。

Lets say we just want to install PHP 5.6.0 to build a CLI application, that uses PDO to talk to a SQLite database. For that, we can do the following:

可以说,我们只想安装PHP 5.6.0来构建一个CLI应用程序,该应用程序使用PDO与SQLite数据库进行通信。 为此,我们可以执行以下操作:

phpbrew install 5.6.0 +cli +pdo +sqlite +xml_all

This command will enable the PDO extension itself, and sqlite enables the SQLite drivers. The cli variant will install the command-line interface, and xml_all will stop PHPbrew complaining about PEAR.

此命令将启用PDO扩展本身,而sqlite启用SQLite驱动程序 。 cli变体将安装命令行界面,而xml_all将停止PHPbrew抱怨PEAR。

If you have any trouble installing a version of PHP, try running the same command but add the -d option. This will send debug information to the console, instead of sending it to a log file.

如果在安装PHP版本时遇到任何问题,请尝试运行相同的命令,但添加-d选项。 这会将调试信息发送到控制台,而不是将其发送到日志文件。

phpbrew install -d 5.6.0 +default +sqlite

切换PHP版本 (Switching PHP versions)

So, at this point we should have a version of PHP installed.

因此,此时我们应该已经安装了PHP版本。

If our installation was a success then PHPbrew will output a message like this:

如果我们的安装成功,则PHPbrew将输出如下消息:

Congratulations! Now you have PHP with php-5.6.0. To use the newly built PHP, try the line(s) below: $ phpbrew use php-5.6.0 Or you can use switch command to switch your default php version to php-5.6.0: $ phpbrew switch php-5.6.0

The first command listed use will let you use PHP 5.6.0 while you’re in that console session. If you close the tab/window or restart your computer then you’ll be back to whichever version of PHP is the default.

列出的第一个命令use将使您处于控制台会话时,可以使用PHP 5.6.0。 如果关闭标签页/窗口或重新启动计算机,则将返回默认PHP版本。

The second command switch will switch the default version of PHP that PHPbrew will go to on a new session.

第二个命令switch将切换PHPbrew将在新会话上使用的默认PHP版本。

Lets try setting the default version to be PHP 5.6.0, and see if it works.

让我们尝试将默认版本设置为PHP 5.6.0,然后查看它是否有效。

$ phpbrew switch php-5.6.0 $ php -v PHP 5.6.0 (cli) (built: Sep 30 2014 15:30:22) Copyright (c) 1997-2014 The PHP Group Zend Engine v2.6.0, Copyright (c) 1998-2014 Zend Technologies

The output above shows us exactly what we want to see: PHP 5.6.0.

上面的输出显示了我们想要看到的内容: PHP 5.6.0 。

If we now try installing the older PHP 5.5, we can once again use $ phpbrew known to see which versions are available. Pick a version, and try to install it:

如果现在尝试安装较旧PHP 5.5,则可以再次使用$ phpbrew known查看可用的版本。 选择一个版本,然后尝试安装它:

phpbrew install 5.5.17 +default +sqlite

This will install PHP 5.5.17 with the default and sqlite variants. To then use PHP 5.5.17, we have to run another command:

这将安装具有default和sqlite变体PHP 5.5.17。 要使用PHP 5.5.17,我们必须运行另一个命令:

$ phpbrew use php-5.5.17 $ php -v PHP 5.5.17 (cli) (built: Sep 30 2014 17:41:05) Copyright (c) 1997-2014 The PHP Group Zend Engine v2.5.0, Copyright (c) 1998-2014 Zend Technologies

Now we can use PHP 5.5.17 in this session, and use PHP 5.6.0 again. As we used the use command not switch, when we open another tab or window, PHP 5.6.0 will once again be there.

现在,我们可以在此会话中使用PHP 5.5.17,然后再次使用PHP 5.6.0。 当我们使用use命令not switch ,当我们打开另一个选项卡或窗口时,PHP 5.6.0将再次出现。

PHPbrew vrs。 系统 (PHPbrew vrs. System)

When we are using a PHPbrew version of PHP, our bash session will be using a special path for the PHP version. We can find out which version is in use with the which command:

当我们使用PHPbrew版本PHP时,我们的bash会话将使用PHP版本的特殊路径。 我们可以使用which命令找出正在使用的版本:

$ which php /Users/phil/.phpbrew/php/php-5.6.0/bin/php

If we would like to stop using this phpbrew version of PHP and go back to the system version, we can use the off command.

如果我们想停止使用此phpbrew版本PHP并返回到系统版本,则可以使用off命令。

$ phpbrew off phpbrew is turned off. $ which php /usr/bin/php $ php -v PHP 5.4.24 (cli) (built: Jan 19 2014 21:32:15) Copyright (c) 1997-2013 The PHP Group Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies

Once again we’re using the default version. You may not need this, but it is handy to know how to get rid of phpbrew. The which command will also help you out with debugging.

再次,我们使用默认版本。 您可能不需要此功能,但是知道如何摆脱phpbrew很方便。 which命令还将帮助您进行调试。

PHPbrew by itself is a useful tool, and you might find this is all you need. That said, these versions of PHP can get complicated when you find that various projects need more extensions. PHPBrew can add PECL extensions, but not a project-by-project basis.

PHPbrew本身就是一个有用的工具,您可能会发现这就是您所需要的。 也就是说,当您发现各种项目需要更多扩展时,这些版本PHP可能会变得复杂。 PHPBrew 可以添加PECL扩展,但不能逐个项目添加。

It also assumes you can remember which version of PHP an application should be using. It might not be the default, and running it under another version could cause problems.

它还假定您可以记住应用程序应使用哪个PHP版本。 它可能不是默认值,并且在其他版本下运行可能会导致问题。

To do this, we need to look at using another tool on top of PHPbrew.

为此,我们需要研究使用PHPbrew之上的另一个工具。

VirtPHP (VirtPHP)

VirtPHP lets you make isolated PHP environments on a single machine, like Python’s virtualenv. That might sound a little complicated but the idea is simple.

VirtPHP使您可以在一台机器上创建隔离PHP环境,例如Python的virtualenv。 听起来可能有些复杂,但想法很简单。

To start, you mark one directory which contains an application or component, and give it a name. Imagine we were working on “airpair-api” and its a PHP app, we’d want to make that its own environment.

首先,标记一个包含应用程序或组件的目录,并为其命名。 想象一下,我们正在开发“ airpair-api”及其PHP应用程序,我们想使其成为自己的环境。

Then, we could install PECL extensions that “airpair-api” needs, without affecting other applications.

然后,我们可以安装“ airpair-api”所需的PECL扩展,而不会影响其他应用程序。

That’s the theory, so let’s take a look at how we do that.

这就是理论,所以让我们看看我们如何做到这一点。

安装virtPHP (Installing virtPHP)

Go to the virtPHP releases page and find the latest release. It will have a link saying “virtphp.phar”, and you’ll want to right click and copy that URL.

转到virtPHP版本页面 ,找到最新版本。 它将有一个链接说“ virtphp.phar”,您将要右键单击并复制该URL。

$ wget https://github.com/virtphp/virtphp/releases/download/v0.5.1-alpha/virtphp.phar chmod +x virtphp.phar sudo mv virtphp.phar /usr/bin/virtphp

Now we can check to see if it is working:

现在我们可以检查它是否正常工作:

$ virtphp -V virtPHP version v0.5.1-alpha 2014-08-13 16:05:47

创造环境 (Creating Environments)

VirtPHP maintains a relaxed approach to which version of PHP is in use. When you go to create an environment, it will take whichever version of PHP you have in your console session and reference that.

VirtPHP维护使用宽松版本PHP的宽松方法。 当您创建环境时,它将使用您在控制台会话中拥有的任何版本PHP并引用该版本。

So, before we try to make a new environment, we need to be certain we are using the right version.

因此,在尝试创建新环境之前,需要确定我们使用的是正确的版本。

$ which php /Users/phil/.phpbrew/php/php-5.6.0/bin/php

Oops, it’s still using the default version and I want to make sure my codebase is working on PHP 5.5.

糟糕,它仍在使用默认版本,我想确保我的代码库在PHP 5.5上正常工作。

$ phpbrew use 5.5.17 $ which php /Users/phil/.phpbrew/php/php-5.5.17/bin/php

Perfect, the currently enabled version of PHP is 5.5.17, which in this example is the one we want.

完美,当前启用PHP版本是5.5.17,在此示例中,这是我们想要的版本。

Now we can make an environment.

现在我们可以创造一个环境。

virtphp create airpair-api

You will see a lot of output, and if goes well then you should see the following:

您将看到很多输出,如果运行顺利,则应该看到以下内容:

Your virtual php environment (airpair-api) has been created! You can activate your new environment using: ~$ source /Users/phil/.virtphp/envs/airpair-api/bin/activate

At this point the new environment is ready to use, but not enabled. Copy the command it gives you and run it, or run this shorter version:

此时,新环境可以使用了,但尚未启用。 复制它给您的命令并运行它,或运行此较短的版本:

$ source ~/.virtphp/envs/airpair-api/bin/activate

Now you should see the name of the environment (airpair-api) in the console prompt, before the $ character. This lets you know that you are in an environment, so you can deactivate it or act accordingly.

现在,您应该在控制台提示符中$字符之前看到环境名称(airpair-api) 。 这样可以使您知道自己处在环境中,因此可以将其停用或采取相应措施。

在沙盒中玩 (Playing in the Sandbox)

Now we have this environment, we can install and configure things without affecting other PHP installations.

现在我们有了这个环境,我们可以安装和配置东西而不会影响其他PHP安装。

One great use-case for using environments is being able to install PECL extensions. Not only can you test how an app works with or without the extension, but you can try different versions too.

使用环境的一个很好的用例是能够安装PECL扩展。 您不仅可以测试带有或不带有扩展名的应用程序的工作方式,而且还可以尝试其他版本。

(airpair-api) $ pecl install xdebug (airpair-api) $ pecl install memcached-1.0.2

This helps us install the great debugging tool Xdebug, and install the [memcached] extension.

这有助于我们安装出色的调试工具Xdebug ,并安装[memcached]扩展名。

At the time of writing, the PECL command installs packages via the PEAR-based system. In future versions of virtPHP, PECL extensions will install via the new and improved Pickle system. This will remove a few issues that OS X seems to have with supporting PEAR.

在编写本文时,PECL命令通过基于PEAR的系统安装软件包。 在virtPHP的将来版本中,将通过新的经过改进的Pickle系统安装PECL扩展。 这将消除OS X似乎在支持PEAR时遇到的一些问题。

退出环境 (Exiting an Environment)

To check if you’re still using an environment, two things will help. The first clue is to see the environment name in brackets in your command prompt. The second is to use which php and see if its pointing to a virtPHP environment.

要检查您是否仍在使用环境,有两件事会有所帮助。 第一条线索是在命令提示符下的括号中查看环境名称。 第二个是使用which php ,看看它是否指向virtPHP环境。

(airpair-api) $ which php /Users/phil/.virtphp/envs/airpair-api/bin/php (airpair-api) $ deactivate $ which php /Users/phil/.phpbrew/php/php-5.6.0/bin/php

There you can see we were using the airpair-api environment. Then after deactivating it, the console fell back to using 5.6.0 installed from PHPbrew, as that is default.

在那里您可以看到我们正在使用airpair-api环境。 然后,将其停用后,控制台恢复为使用从PHPbrew安装的5.6.0,因为这是默认设置。

结论 (Conclusion)

Playing with many installed versions like this can at first seem a little confusing. In reality, there is much less to learn here than trying to teach a beginner developer all about a full stack.

首先,使用许多已安装的版本进行游戏似乎有些混乱。 实际上,在这里要学习的东西要比试图教给初学者的开发人员更多有关完整堆栈的知识要少得多。

If a new developer was to try and build a basic PHP app, traditionally they would go through the following steps to get started:

如果新开发人员尝试构建基本PHP应用程序,则传统上他们将通过以下步骤开始:

Apache/nginx

阿帕奇/ nginx MySQL

MySQL Virtual Hosts and /etc/hosts

虚拟主机和/ etc / hosts Hack the core OS PHP version

破解核心OS PHP版本 Maybe install XAMPP/WAMP/MAMP instead

也许改为安装XAMPP / WAMP / MAMP Try to upgrade the core versions or *AMP versions

尝试升级核心版本或* AMP版本 Get confused that system PHP and MAMP PHP is different

混淆系统PHP和MAMP PHP是不同的 Try to install PECL extensions to MAMP version, but install them to system PHP instead

尝试将PECL扩展安装到MAMP版本,但将其安装到系统PHP

You can avoid a lot of that pain with a tool like Vagrant and a provision script, but that assumes that this beginner is in a team. If going solo, the beginner would have a much harder time getting started.

您可以使用Vagrant之类的工具和配置脚本来避免很多麻烦,但前提是该初学者在团队中。 如果要独奏,初学者会很难上手。

This is how the Ruby on Rails community has done things for years. Teach beginners a framework, abstract away a lot of the hard stuff, get them building and let them learn more about it all as they grow.

这就是Ruby on Rails社区多年来所做的事情。 教初学者一个框架,抽象出很多困难的东西,让他们建立,并让他们随着成长而了解更多。

Grab your PHP version, install what you need, run the PHP development server with php -S and only beef up your stack when (or if) you need to.

获取您PHP版本,安装所需的组件,使用php -S运行PHP开发服务器,仅在需要时(或如果需要)增强您的堆栈。

Dev/Prod parity is important, but sometimes you can get away with not caring too much if its just a simple little HTTP service. If you already have CI testing in place then this is even more true.

Dev / Prod奇偶校验很重要,但是有时只要它只是一个简单的小HTTP服务,您就可以不必太在乎。 如果您已经进行了CI测试,那就更是如此。

Finaly, even if you don’t want to run the code through the development server, having PHPbrew and virtPHP are still useful. You can install new versions as soon as they come out to play with the new syntax, and not break all your apps.

最后,即使您不想通过开发服务器运行代码,使用PHPbrew和virtPHP仍然很有用。 您可以立即安装新版本以使用新语法,而不会破坏所有应用程序。

翻译自: https://www.sitepoint.com/use-phpbrew-virtphp/

相关资源:25个经典网站源代码
最新回复(0)