4.1 动手编译PHP

从一个PHP程序猿,到一个想为PHP开发扩展的程序猿,此间的进化有一步是跳不过去的,那就是你必须熟知如何编译PHP的源码。

*nix Tools

C语言的编译器是我们使用C语言的必备工具,你的系统应该已经自带了一种C语言的编译器,而且它极有可能是大名鼎鼎的GCC。通过检测你本机gcc或者cc程序的版本,可以很方便的知道你机器上是否已经安装的某种C语言的编译器。

  1. walu@walu-ThinkPad-Edge:~$ gcc --version
  2. gcc (Ubuntu/Linaro 4.5.2-8ubuntu4) 4.5.2
  3. Copyright (C) 2010 Free Software Foundation, Inc.
  4. This is free software; see the source for copying conditions. There is NO
  5. warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

如果你还没有安装编译器,那你需要安装一个。最简单的办法便是去下载一个与你系统相符的rpm或者deb包,当然你也可以通过以下命令的一种来方便的安装:yum install gcc, apt-get install gcc, pkg-add -r gcc, 或者 emerge gcc.除了编译器,你还需要以下程序:make, autoconf, automake, 和libtool。说实话,我连autoconf现在是啥还不知道(截至到现在,2011年9月6号),不过除非RP太低,一般系统中都会自备了,而且phpize程序会把这些需要的脚本给生成好的。对于编译需要的程序以及它们的版本我们可以在PHP官网找到最新的答案:

  • autoconf: 2.13 (2.59+ for PHP 5.4+)
  • automake: 1.4+
  • libtool: 1.4.x+ (except 1.4.2)
  • bison: 1.28, 1.35, 1.75, 2.0 or higher
  • flex (PHP 5.2 and earlier): 2.5.4 (not higher)
  • re2c: 0.13.4+
你千万不要被上面的清单给吓着,其实系统应该给装备好了,除非真RP低,那你出门去买张彩票吧… …当然,我们也可以通过SVN从PHP源码库里导出一份源码,需要注意的是,PHP的svn源码库地址是https协议的。官方推荐我们直接签出它的php-src目录:

  1. $ svn checkout https://svn.php.net/repository/php/php-src --depth immediates php-src
  2. $ cd php-src

当然,我们也可以签出特定的版本:

最新的大家可以来这查看:http://php.net/svn.php

Win32 Tools

这里仅代表作者05年的观点,我还没有在win平台下测试过,稍後会把这段修正过来。

  1. The Win32/PHP5 build system is a complete rewrite and represents a significant leap forward from the PHP4 build system. Instructions for compiling PHP4 under Windows are available on php.net, only the PHP5 build systemwhich requires Windows 2000, Windows 2003, or Windows XPwill be discussed here.

  2. First, youll need to grab libraries and development headers used by many of the core PHP extensions. Fortunately, many of these files are redistributed from php.net as a single .zip file located at http://www.php.net/extra/win32build.zip.

  3. Create a new directory named C:\PHPDEV\ and unzip win32build.zip using your favorite zip management program into this location. The folder structure contained in the zip file will create a subdirectory, C:\PHPDEV\win32build, which will contain further subfolders and files. Its not necessary to name your root folder PHPDEV; the only important thing is that win32build and the PHP source tree are both children of the same parent folder.

  4. Next youll need a compiler. If youve already got Visual C++ .NET you have what you need; otherwise, download Visual C++ Express from Microsoft at http://lab.msdn.microsoft.com/express/.

  5. The installer, once youve downloaded and run it, will display the usual welcome, EULA (End-User License Agreement), and identification dialogs. Read through these screens and proceed using the Next buttons after youve agreed to the terms of the EULA and entered any appropriate information.

  6. Installation location is of course up to you, and a typical installation will work just fine. If youd like to create a leaner installation, you may deselect the three optional componentsGUI, MSDN, and SQL Server.

  7. The final package is the Platform SDK, also available for download from Microsoft at http://www.microsoft.com/downloads/details.aspx?FamilyId=A55B6B43-E24F-4EA3-A93E-40C0EC4F68E5. The site currently lists three download options: PSDK-x86.exe, PSDK-ia64.exe, and PSDK-amd64.exe. These options refer to x86 compatible 32bit, Intel64bit, and AMD64bit processors respectively. If you’re not sure which one applies to your processor, select PSDK-x86.exe, which should work cleanly, albeit less efficiently, with both 64 bit variants.

  8. As before, proceed through the first few screens as you would with any other installer package until you are prompted to select between Typical and Custom installation. A Typical installation includes the Core SDK package, which is sufficient for the purposes of building PHP. Other packages can be deselected by choosing a Custom installation, but if you have the hard disk space to spare, you might as well install it all. The other packages may come in handy later on.

  9. So unless youre byte conscious, select Typical and proceed through the next couple of standard issue screens until the installer begins copying and registering files. This process should take a few minutes so grab some popcorn.

  10. Once installation is complete youll have a new item on your Start menuMicrosoft Platform SDK for Windows Server 2003 SP1.

获取PHP源码

其实你有很多办法安装PHP,最简单的一种就是从你系统的库或者源里通过apt-get、yum install之类的命令直接安装PHP5,这样做的好处你的系统可能会自动处理一些php在它上面的工作时的一些bug,而且你还可以方便的升级与卸载。这样做也有缺点,那就是你的PHP版本永远无法是最新的,通常www.php.net发布数周甚至数月后你才能用上相应的版本。第二种方法:也是推荐使用的一种方法,那就是自行下载php-x.y.z.tar.gz的源码包,然后自行编译安装。这种包一般都是经过了海量的测试后才发布的,而且非常接近最新beta或者alpha版本。此外,你还可以snaps.php.net提供的快照包来下载php进行编译安装,这个站点每几个小时便会从源码库里打包出一份新的PHP。不过从这取得的包可能会因为某个未经完整测试的代码提交而使PHP工作不正常。但是如果你想研究下PHP6.0的进展,这里绝对是你方便获取它的地方。最后,你可以直接从版本库中导出此时此刻的源码。作为一个扩展开发者,从版本库或者snaps中获取php看起来并没有多大的作用,但是如果我们要将这个扩展推送到版本库中时,便需要熟练的掌握checkout和checkin的步骤了。签出的地址在上面已经说过了。