Installation on Windows systems
PHP Manual

Building from source

This chapter teaches how to compile PHP from sources on windows, using Microsoft's tools. To compile PHP with cygwin, please refer to Installation on Unix systems.

This documentation will give you a basic understanding of how to compile PHP on Windows. This guide takes advanges of the many improvements made to the build system available in PHP 5.3.0 and greater.

Getting started

Before we can compile PHP we will need a compiler and some of the many libraries that PHP depends on. The only supported compilers on Windows is MSVC (Microsoft Visual C++). Versions 6 and greater is supported, however for better performance Visual C++ 2008 or greater is recommeded. This documentation will use Visual C++ 2008 and PHP 5.3.x as a base.

Visual C++ exists in two editions, a commercial edition (Visual Studio) and in an "Express" edition, which is freely available from Microsoft.

Installing using Visual Studio

When installing Visual Studio, you will be prompted to choose which components you wish to use, in here you must check the "Visual C++" component. If you plan on compiling x64 versions of PHP, you must select the x64 compilers component below "Visual C++".

If you already installed Visual Studio, then plugin your DVD and select "Add/Remove components", and follow the procedure above for installing the compiler.

Installing using Visual C++ Express

Visual C++ Express is available for free from Microsoft at: » http://www.microsoft.com/express/download/.

Before installing, the installation wizard will ask you to install additional components such as SQL Server, Silverlight and so on, none of these are required for compiling PHP.

Installing the Windows Platform SDK

Before PHP can be compiled, we need the Windows Platform SDK. By default Visual C++ comes with the Windows Platform SDK version 6.0A, this can however not be used due to incompatibility with previous versions of the SDK. So the Windows Platform SDK 2008, which is version 6.1 is required. The SDK is freely available from Microsoft at: » http://www.microsoft.com/downloads/details.aspx?FamilyID=e6e1c3df-a74f-4207-8586-711ebe331cdc.

When downloading the SDK, choose the web install, as you will only need selected components. When the download is done and the setup is prompting you to select which components you require, then select the C++ compilers (remember to tick the x64 compilers, if you want to compile the 64bit version of PHP). You don't need any other tools such as CrystalReports, Mobile Development tools or anything, just the C++ libraries, headers and compilers.

Directory structure

Now we need to set a directory structure for where the PHP sources will be located, aswell as the default required libraries for compiling PHP. Below structure is simple and easy usable, its however not required to have it exactly like this, but its easy to overlook.

Example #1 Source and toolchain directory structure

C:.
+---php
|   +---src                       <- Here will the PHP sources be located
|   +---win32build                <- Toolchain and libraries for x86 (32 bit) versions
|   |   +---bin                   <- Toolchain
|   |   +---include               <- Header files
|   |   +---lib                   <- Library files
|   +---win64build                <- Toolchain and libraries for x64 (64 bit) versions (optional)
|       +---bin                   <- Toolchain
|       +---include               <- Header files
|       +---lib                   <- Library files

Getting the PHP source

There are serveral ways to get the PHP sources, either from the source tarballs or directly from the PHP SVN repository. This documentation will simply assume either option is choosen and the sources either have been exracted/checked out to the "C:\php\src\" directory from the above directory structure.

Getting the PHP toolchain

To compile PHP, we need a toolchain of different programs for compiling and linking default extensions. These are all available in a single zip file from the PHP on Windows repository: » http://pecl2.php.net/downloads/php-windows-builds/php-libs/binary-tools.zip.

Extract this zip file so it complies with the directory structure for the "C:\php\win32build\" directory. If you are planning to compile 64bit versions of PHP, you need to do the same with the "C:\php\win64build\" directory. This is alright as the tools are platform independent.

Getting the libraries required for PHP

PHP is a glue language, and depends on many libraries in order to function. By default PHP requires a set of libraries for compiling the core (Engine, CLI SAPI and the standard library).

These are available from PHP.net, organized in compiler versions and machine architectures. A standard PHP build requires the following mandatory libraries:

If you plan to compile a standard versions of PHP. Meaning compiling all default enabled extension by running configure without any arguments, You will also need the following additional libraries for GD:

All these libraries are packed and published to the following URL: » http://pecl2.php.net/downloads/php-windows-builds/php-libs/.

On the above location, libraries are organized by which compiler that were used to compile them and on which platform architecture. Below explains the different shot name variants for each compiler.

When you have downloaded the mandatory libraries, and/or any additional libraries you may require, then exact them respectedly to the directory structure set in "C:\php\winXXbuild\", so headers are located in the include directory and library files in the libs directory.

Setting up the environment

Before we can compile PHP, we must alter the PATH environment variable so the configure script can pickup the toolchain in the "C:\php\winXXbuild\bin\" directories. To do this, open the Start menu and right click on "Computer" and select "Properties". Select the "Advanced" tab, and hit the button saying "Environment variables". In the global variables section, find the PATH variable and select "Edit". Append the following to its current value: ;C:\php\win32build\bin and for 64bit support: ;C:\php\win32build\bin;C:\php\win64build\bin.

Now you must create the following directory structure on the drive that you are using to compile on:

Example #2 bison.simple directory structure

C:.
+---usr
|   +---local
|       +---share

Now copy the "C:\php\win32build\bin\bison.simple" file into the "C:\usr\local\share" directory.

Compiling

Now PHP is ready to be compiled, open the Start menu, choose "Programs" and find "Microsoft Visual Studio" (or "Microsoft Visual C++"), select the tools folder and open "Visual Studio 2008 Command Prompt". This will open a console window saying "Setting up environment for XXX", where XXX may be either x86 or x64. If you wish to compile the 64bit version of PHP, you must execute the following:

Example #3 Setting up the x64 build environment

cd bin
vcvarsx86_amd64

Now cd to the "C:\php\src\" directory and execute the buildconf script like so:

Example #4 Running buildconf

buildconf
Now run 'configure --help'

This means that the configure script was generated with success. Now we need to run the generated configure script to generate the Makefiles for the compiler. Simply run configure like so:

Example #5 Running configure

configure
...
...
...

As buildconf advertised, then configure --help can be used to see available commands for configure. If you have multiple cores available then enable multi processing, which optimizes and makes the compilation faster. Multi processing can be enabled using the --enable-one-shot option for configure like so:

Example #6 Enabling multi processing in configure

configure --enable-one-shot
...
...
...

If you wish to do a minimal build, then you need to disable all default enabled extensions using the --disable-all option. When disabling all extensions and SAPIs, you must select atleast one before you can compile a PHP binary. Below example will disable all extensions, enable the CLI SAPI and enable multi processing:

If you need to export debugging symbols and doing a debug build, you also need to enable the following two options: --enable-debug and --enable-debug-pack.

Example #7 Configuring a minimal build of PHP with multi processing

configure --disable-all --enable-cli --enable-one-shot
...
...
...

If you need to compile an NTS (NOT Thread Safe) build of PHP, you need to pass the --disable-zts option to configure. By default PHP is compiled with Thread Safety.

Now we are ready actually perform the compile, this is done by executing nmake, like so:

Example #8 Executing nmake

nmake
...
...
...

Testing the build

When the build process is complete, the PHP binary, along with all the compiled extensions will be located in a sub directory to the sources directory. Depending on the build type, it will be located in one of the following directories:

If you are compiling the 64bit version of PHP, these directories will be located inside another directory called x64 from the root of the sources.

To test the compiled binary, then cd to its directory and execute the following:

Example #9 Testing the compiled PHP binary

C:\php\src\Release_TS\> php -v
PHP 5.3.X (cli) (built: XXX XX XXXX XX:XX:XX)
Copyright (c) 1997-2010 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies


Installation on Windows systems
PHP Manual