centos安装扩展源
Sometimes it’s hard to know which PHP extensions you’ll need before you install PHP. In cases where you need to add extensions later on, you might get lucky and the extension could be in the repository of the OS you’re using. It might just be a simple sudo apt-get install php5-intl away. In other cases, however, you might need to install it from source – Phalcon is one such case, but it makes the procedure extremely simple by introducing vendor support, shortcuts and pre-written instructions for your OS to carry out. What if there’s no such thing for other extensions, though?
有时,在安装PHP之前很难知道需要哪些PHP扩展。 如果以后需要添加扩展,可能会很幸运,并且扩展可能在您使用的操作系统的存储库中。 可能只是一个简单的sudo apt-get install php5-intl 。 但是,在其他情况下,您可能需要从源代码安装它-Phalcon就是这样一种情况,但是通过引入供应商支持,快捷方式和用于操作系统的预先编写的说明,它使此过程变得非常简单 。 但是,如果其他扩展程序没有这样的东西怎么办?
In this tutorial, we’ll go through installing some custom extensions on Linux systems (and OS X – the process is nearly identical). The procedure is very similar to what we already did on Nitrous.io, but adapted for local environments – more specifically, Laravel Homestead. You can easily derive installation instructions from this tutorial and apply them to other distros.
在本教程中,我们将逐步在Linux系统(和OS X –过程几乎相同)上安装一些自定义扩展。 该过程与我们在Nitrous.io上已经完成的过程非常相似,但是适用于本地环境,更具体地说,是Laravel Homestead 。 您可以轻松地从本教程中获得安装说明,并将其应用于其他发行版。
If you haven’t already, read the Homestead post linked above and get it up and running. Immediately after running a new Homestead box, you should be able to do this:
如果您还没有阅读过,请阅读上面链接的Homestead帖子,然后开始运行。 运行新的Homestead框后,您应该能够立即执行以下操作:
That’s perfectly fine, this happens because the folder that’s mounted by default actually doesn’t contain any files yet. Now vagrant ssh into the VM, and execute the following commands:
很好,这是因为默认情况下挂载的文件夹实际上还不包含任何文件。 现在将vagrant ssh到虚拟机中,并执行以下命令:
cd Code git clone https://github.com/Swader/publicinfoThis creates a valid PHP Info file in the path that Homestead is set to by default. Refreshing the URL will now produce a PHPInfo screen:
这将在默认情况下将Homestead设置为的路径中创建一个有效PHP Info文件。 现在刷新URL将产生一个PHPInfo屏幕:
To build extensions from source, we need the PHP dev tools installed on our machine, as well as a compiler that can produce the extension file. Here’s how you install these prerequisites on various operating systems:
要从源代码构建扩展,我们需要在计算机上安装PHP开发工具以及可以生成扩展文件的编译器。 这是在各种操作系统上安装这些必备组件的方式:
If you’re using the most recent Homestead, all these tools will already be installed for you. With everything prepared, let’s start installing extensions.
如果您使用的是最新的Homestead,则已经为您安装了所有这些工具。 准备好一切之后,让我们开始安装扩展程序。
There are two types of extensions you can install: bundled with PHP but not installed by default, and third party extensions. Third party extensions like Phalcon usually make the installation process much easier by providing shortcuts, as they don’t have to comply to certain traditions bundled PHP extensions are bound by.
您可以安装两种类型的扩展:与PHP捆绑在一起但默认情况下未安装的扩展,以及第三方扩展。 诸如Phalcon之类的第三方扩展通常通过提供快捷方式使安装过程更加容易,因为它们不必遵守捆绑PHP扩展所绑定的某些传统。
First, let’s move into the home folder on the vm: cd ~. In there, make a downloads folder, and cd into it. When installing a bundled extension, you’ll need the source code of PHP on your machine, preferably one matching your current version. The version Homestead uses is 5.5.12, so I’ll be downloading that one:
首先,让我们进入vm上的home文件夹: cd ~ 。 在该文件夹中,创建一个downloads文件夹,然后将其插入cd 。 安装捆绑的扩展程序时,您将需要计算机上PHP源代码,最好是与当前版本匹配PHP源代码。 Homestead使用的版本是5.5.12,因此我将下载该版本:
wget http://be2.php.net/distributions/php-5.5.12.tar.bz2 tar xvjf php-5.5.12.tar.bz2 cd php-5.5.12I’m using the Belgian mirror above, feel free to use that one or any other from the download archives.
我使用的是上面的比利时镜子,请随时使用下载档案中的那个镜子。
To see the sources of all bundled extensions, go into the ext folder inside the unarchived PHP source code folder and do a list with ls.
要查看所有捆绑扩展的源代码,请进入未归档PHP源代码文件夹内的ext文件夹,并用ls 。
First, we’ll install the PHP-intl extension if you don’t have it installed already. If you do, that’s fine – the installation procedure you’ll see below is identical for every bundled PHP extension. The intl extension is for internationalization – read more here if you’re interested.
首先,如果您尚未安装PHP-intl扩展程序,则将其安装。 如果这样做,那就很好了–对于每个捆绑PHP扩展,您将在下面看到的安装过程是相同的。 国际扩展名是为了国际化-如果您有兴趣,请在此处阅读更多内容。
Seeing as the intl extension needs the ICU library as a prerequisite (as stated in the requirements), let’s install that first.
鉴于intl扩展需要ICU库作为先决条件(如要求中所述),让我们首先安装它。
sudo apt-get install icu-devtools icu-doc libicu-dev libicu52 libicu52-dbgUnder other distributions, the installation instructions may vary. It’s best if you refer to the ICU site or your individual distribution’s docs for this step.
在其他发行版中,安装说明可能会有所不同。 最好在此步骤中参考ICU网站或您个人发行版的文档。
Once ICU is installed, do the following while still in the ext folder:
一旦安装了ICU,请在ext文件夹中进行以下操作:
cd intl phpize ./configure --enable-intl make sudo make installLet’s explain what’s going on.
让我们解释发生了什么。
phpize prepares the extension’s folder for compliation. It allows you to do the subsequent commands by creating a configure file, and basically making the extension’s folder “think” it’s PHP itself. The procedure after phpize is, in fact, identical to what you would do when installing PHP from source – only in this case, just a fragment of PHP is compiled and prepared for use with the already compiled and installed PHP.
phpize为扩展准备扩展的文件夹。 它允许您通过创建configure文件并基本上使扩展的文件夹“认为”它是PHP本身来执行后续命令。 实际上, phpize之后的过程与从源代码安装PHP时的过程相同–仅在这种情况下,仅编译了一部分PHP并准备好与已编译并安装PHP一起使用。
./configure --enable-intl configures the environment for compilation. It prepares everything the compiler will need to craft the intl.so file which we’ll be using. The enable-intl flag is necessary even though we’re in the intl folder because the folder, effectively, thinks it is PHP, and we need to help it live that illusion. This command tells it: “Ok, you’re PHP’s source code. Now compile and install with the intl extension.”, when in fact, it’s the only part that can be installed from this folder.
./configure --enable-intl配置编译环境。 它准备了编译器制作intl.so文件所需的一切,我们将使用它们。 即使我们位于intl文件夹中, enable-intl标志也是必需的,因为该文件夹实际上认为它是 PHP,并且我们需要帮助它实现这种错觉。 该命令告诉它:“好吧,您是PHP的源代码。 现在,使用intl扩展名进行编译和安装。”,实际上,这是可以从此文件夹安装的唯一部分。
make will compile the sources into intl.so, placing the file into the very folder you’re currently in, under the modules subfolder.
make会将源代码编译为intl.so ,并将文件放置在modules子文件夹下的当前文件夹中。
sudo make install will move this file into the current PHP installation’s extensions folder.
sudo make install会将文件移动到当前PHP安装的扩展文件夹中。
All we need to do now is enable the extension by having php.ini consume it. We’ll do that later, let’s compile a third party extension first.
现在我们需要做的就是通过让php.ini使用扩展来启用它。 我们稍后再做,让我们先编译第三方扩展。
We’ll be installing Mongo as a third party extension. There are binary distributions available for Mongo which make installation simpler, but let’s do it manually for the sake of education. We’ll assume you already have the actual Mongo installed, and are thus focusing only on the PHP extension. If you don’t have Mongo installed, refer to their installation docs.
我们将安装Mongo作为第三方扩展。 有适用于Mongo的二进制发行版,这些发行版使安装更简单,但是为了便于学习,让我们手动进行安装。 我们假设您已经安装了实际的Mongo,因此仅关注PHP扩展。 如果您尚未安装Mongo,请参阅其安装文档 。
git clone https://github.com/mongodb/mongo-php-driver cd mongo-php-driver phpize ./configure make sudo make installThis has built our mongo.so file, and placed it into the extensions folder of our PHP installation. We’ll enable it in the next section.
这已经构建了我们的mongo.so文件,并将其放入我们PHP安装的extensions文件夹中。 我们将在下一部分中启用它。
To see if the compiled .so files are indeed in our PHP extensions folder, list out its contents:
要查看编译后的.so文件是否确实在我们PHP扩展文件夹中,请列出其内容:
As you can see, there they are, highlighted in bright green.
如您所见,它们以亮绿色突出显示。
To enable them, we need to tell php.ini about them. There are several ways to do this:
要启用它们,我们需要告诉php.ini有关它们的信息。 做这件事有很多种方法:
You can put the lines extension=mongo.so and extension=intl.so directly into php.ini. This works, and is a perfectly valid approach in most cases.
您可以将extension=mongo.so和extension=intl.so直接放入php.ini 。 这是可行的,并且在大多数情况下是一种完全有效的方法。
You can create a separate ini file for each of these, put them in a folder that gets auto-included after php.ini is loaded, and maintain separation and isolation of php.ini that way. This approach is healthier, though harder to accomplish.
您可以为每个文件创建一个单独的ini文件,将它们放在php.ini加载后自动包含的文件夹中,并以此方式保持php.ini分离和隔离。 尽管更难实现,但这种方法更健康。
You can combine 2) and the default tools for enabling PHP mods. Homestead uses this approach, and so will we. 您可以结合使用2)和默认工具来启用PHP mod。 Homestead使用这种方法,我们也会这样做。 cd `/etc/php5/mods-available`This folder is the repository of all such individual ini files. Create two new files here:
该文件夹是所有此类单个ini文件的存储库。 在此处创建两个新文件:
sudo touch mongo.ini echo "extension=mongo.so" | sudo tee -a mongo.ini sudo touch intl.ini echo "extension=intl.so" | sudo tee -a intl.iniThese commands created two new ini files, each for one of the extensions we’ve previously built. As they are now in the mods-available folder, we can use the already available php5enmod (short for PHP enable mod) command line tool.
这些命令创建了两个新的ini文件,每个文件都用于我们先前构建的扩展之一。 由于它们现在位于mods-available文件夹中,因此我们可以使用已经可用的php5enmod (PHP enable mod的缩写)命令行工具。
sudo php5enmod mongo sudo php5enmod intlNote: If you don’t have the php5enmod tool, symlinking the ini files into the conf.d folder of various php runtimes will do the trick:
注意:如果您没有php5enmod工具,则将ini文件符号链接到各种php运行时的conf.d文件夹中将达到目的:
ln -s /etc/php5/mods-available/mongo.ini /etc/php5/cli/conf.d/mongo.ini ln -s /etc/php5/mods-available/intl.ini /etc/php5/cli/conf.d/intl.ini ln -s /etc/php5/mods-available/mongo.ini /etc/php5/fpm/conf.d/mongo.ini ln -s /etc/php5/mods-available/intl.ini /etc/php5/fpm/conf.d/intl.iniThe reason why there’s four entries is that we have the command line version of PHP and the FPM version of PHP. Each uses its own php.ini file, and each loads its own conf.d folder for extensions – hence, we need to add both extensions to both PHP versions if we want the extensions available all-around. Use this approach only if you don’t have the php5enmod tool.
之所以有四个条目,是因为我们有PHP的命令行版本和PHP的FPM版本。 每个扩展程序都使用自己的php.ini文件,并为扩展程序加载其自己的conf.d文件夹-因此,如果要使扩展程序全面可用,则需要将两个扩展程序都添加到两个PHP版本中。 仅当您没有php5enmod工具时才使用此方法。
Finally, let’s restart nginx and php-fpm to load these changes.
最后,让我们重新启动nginx和php-fpm以加载这些更改。
sudo service nginx restart sudo service php5-fpm restartTo see if we’ve got them installed, refresh the PHPinfo screen from before and search for mongo and intl respectively.
要查看是否已安装它们,请从之前刷新PHPinfo屏幕,然后分别搜索mongo和intl。
Success!
成功!
To remove extensions, there’s no need to delete any actual files unless you’re really low on space. You can do it in three ways:
要删除扩展名,除非您的空间非常有限,否则无需删除任何实际文件。 您可以通过三种方式做到这一点:
Run php5dismod if you have the tool available. It’s the opposite of the php5enmod tool mentioned above. The .so files will stay in place, and the ini files will remain in mods-available, they just won’t load because their symlinks will be removed from the fpm and cli conf.d folders.
如果有可用的工具,请运行php5dismod 。 这与上面提到的php5enmod工具相反。 .so文件将保留在原位,并且ini文件将保留在mods-available ,因为它们的符号链接将从fpm和cli conf.d文件夹中删除,因此它们将不会加载。
Remove the symlinks manually. E.g. sudo rm /etc/php5/cli/conf.d/mongo.ini
手动删除符号链接。 例如sudo rm /etc/php5/cli/conf.d/mongo.ini
If you enabled the extensions by putting them directly into the php.ini files, remove those lines from the php.ini files, or better yet, comment them so that they remain accessible for further use should you ever change your mind.
如果您通过将它们直接进入启用扩展php.ini文件,从删除这些线路php.ini文件,或者更好的是,他们发表意见,使它们保持进一步使用,你应该改变主意访问。
As you can see, installing extensions from source is extremely simple, even if there are no precise instructions and even if the extension isn’t supported by an OS’s official repository. The next time you need to add an extension into your PHP installation on a *nix system (this tutorial applies to OS X as well), refer back to this post for a refresher.
如您所见,即使没有确切的说明,并且操作系统的正式存储库不支持该扩展,从源代码安装扩展也非常简单。 下次您需要在* nix系统上PHP安装中添加扩展名(本教程也适用于OS X)时,请参考本文以复习。
Please leave your feedback below, and let me know if you’re confused by a specific extension and would like help with installing it.
请在下面留下您的反馈意见,如果您对特定扩展名感到困惑,并希望获得安装方面的帮助,请告诉我们。
翻译自: https://www.sitepoint.com/install-php-extensions-source/
centos安装扩展源
相关资源:jdk-8u281-windows-x64.exe