Skip to content

Instantly share code, notes, and snippets.

@ihabunek
Created July 6, 2015 07:46
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ihabunek/c74fb2d555fecea37aef to your computer and use it in GitHub Desktop.
Save ihabunek/c74fb2d555fecea37aef to your computer and use it in GitHub Desktop.
Compile PHP On Windows
====== Build your own PHP on Windows ======
===== Before you Begin =====
Building PHP on Windows will require three things
- A properly set up build environment, including a compiler with the right SDK's and some binary tools used by the build system
- Prebuilt libraries and headers for third party libraries that PHP uses in the correct location
- The PHP source
===== Compiler =====
==== Requirements ====
PHP officially supports building with Microsoft's Visual C++ compilers. MinGW and other compilers are NOT supported or even known to work. The free Express and Developer editions can also be used. For more information and how to get the compiler see the [[internals:windows:compiler|supported versions]]. The following VC versions are supported:
* Visual C++ 9.0 (Visual Studio 2008 or Visual C++ 2008) for **PHP 5.3 or 5.4**.
* Visual C++ 11.0 (Visual Studio 2012) for **PHP 5.5 or 5.6**.
* Visual C++ 14.0 (Visual Studio 2015) for **PHP 7.0+**.
If using VC9, you will also need the [[http://www.microsoft.com/en-us/download/details.aspx?id=11310|Windows SDK 6.1]] (see [[internals:windows:windowssdk|this page]] for additional info on the SDK)
==== Setup ====
* If compiling **PHP 5.3 or 5.4**:
* Install Visual Studio 2008
* Install [[http://www.microsoft.com/en-us/download/details.aspx?id=11310|Windows SDK 6.1]]
* If compiling **PHP 5.5 or 5.6**:
* Install Visual Studio 2012. If using the express version, install "[[https://www.microsoft.com/en-us/download/details.aspx?id=34673|Visual Studio 2012 Express for Windows Desktop]]"
* If compiling **PHP 7.0+**:
* Install Visual Studio 2015
==== Command prompt ====
Each version of Visual Studio (or Windows SDK) provides a command prompt environment for compiling (available from the Start Menu group).
* If compiling **PHP 5.3 or 5.4**, open the **"Windows SDK 6.1 shell"** and execute the following command in it: <code bash>setenv /x86 /xp /release</code>
* If compiling **PHP 5.5 or 5.6** open the **"VS2012 x86 Native Tools Command Prompt"**
* If compiling **PHP 7.0+** open the **"Developer Command Prompt for VS2015"**
All commands in the rest of this document should be run in the appropriate command prompt.
===== Download prerequisites =====
- Get the PHP source, there are two alternatives:
* download a source archive ([[http://windows.php.net/download/|Stable releases]], [[http://windows.php.net/qa/|QA releases]])
* clone the [[http://git.php.net/|PHP Git repository]] ([[https://github.com/php/php-src|GitHub mirror]])
- Get the binary tools:
* available from [[http://windows.php.net/downloads/php-sdk/]]
* the binary tools archives are named php-sdk-binary-tools-YYYYMMDD.zip, get the latest one
- Get the libraries on which PHP depends:
* download the packaged deps from [[http://windows.php.net/downloads/php-sdk/]]
* dependency archives are named ''deps-<php version>-<vc version>-<architecture>.7z''
* chose the archive which matches your desired PHP version and architecture, for example ''deps-5.6-vc11-x86.7z''
* **Note:** This should include the libraries needed to build most the core extensions. However, some other extensions may need additional libraries, header files and helper apps. See [[internals/windows/libs]], fetch the version you need and extract the archive into the deps directory (see below).
===== Setup the build directory =====
This should be done only once before building PHP.
- Create the build directory ''c:\php-sdk''
- Unpack the binary tools archive into this directory, it should contain three sub-directories: ''bin'', ''script'' and ''share''
- Open the command prompt and enter the build directory: <code bash>cd c:\php-sdk\</code>
- Run the ''buildtree'' batch script which will create the desired directory structure: <code bash>bin\phpsdk_buildtree.bat phpdev</code>
- The ''buildtree'' script hasn't been updated for newer versions of VC++ so:
* If compiling for **VC11**: copy ''C:\php-sdk\phpdev\**vc9**'' to ''C:\php-sdk\phpdev\**vc11**''
* If compiling for **VC14**: copy ''C:\php-sdk\phpdev\**vc9**'' to ''C:\php-sdk\phpdev\**vc14**''
- Extract the PHP source code to ''C:\php-sdk\phpdev\**vc##**\**x##**'', where:
* ''**vc##**'' is the compiler version you are using (''vc9'', ''vc11'' or ''vc14'')
* ''**x##**'' is your architecture (''x86'' or ''x64'')
* For example: ''C:\php-sdk\phpdev\vc11\x86\php-5.6.4-src''
- In the same directory where you extracted the PHP source there is a ''**deps**'' directory. Here you will need to extract the libraries required to build PHP, which you downloaded in the perevious step (the ''deps-*.7z'' archive).
===== Compile =====
- Open the command prompt and enter the build directory: <code bash>cd c:\php-sdk\</code>
- Set up the build environment variables: <code bash>bin\phpsdk_setvars.bat</code>
- Change directory to the location of your PHP source code, e.g. <code bash>cd C:\php-sdk\phpdev\vc11\x86\php-5.6.4-src</code>
- Run: <code bash>buildconf</code>
- To get an overview of the compiling flags: <code bash>configure --help</code>
- Create your configure command: <code bash>configure --disable-all --enable-cli --enable-$remains</code>
- To build PHP, run: <code bash>nmake</code>
- If you want the resulting PHP builds and extensions to be zipped, after 'nmake' also run: <code bash>nmake snap</code>
- The compiled PHP is now under "C:\php-sdk\phpdev\vcXX\x86\php-source-directory\Release_TS". If you ran 'nmake snap' the zip file will also be here.
* If you compiled with "--disable-zts" the compiled PHP will be under "C:\php-sdk\phpdev\vcXX\x86\php-source-directory\Release"
=== Recompile after you have done some changes ===
- Clean up old compiled binaries <code bash>nmake clean</code>
- If you need to update the 'configure' script <code bash>buildconf --force</code>
- Create your makefile: see [[internals/windows/release]]<code bash>configure --disable-all --enable-cli --enable-$remains</code>
- Compile <code bash>nmake</code>
==== Building PECL extensions ====
Many PECL extensions already have prebuilt DLLs which can be downloaded from the extension page on the PECL web site. For example, on the [[https://pecl.php.net/package/APCu | APCu extension]] page, there are *DLL* links next to the release archive link. They lead you to a page where you can download the compiled extension for your desired PHP version and architecture.
To build a PECL extension (for example APCu):
- Open the extension's page on PECL ([[https://pecl.php.net/package/APCu | APCu]])
- Download the extension source either by:
* downloading a source archive
* fetching the source from the extension's repository (link can be found under *Browse Source*)
- Create a directory named **''pecl''** on the same level as your PHP source directory, e.g. ''C:\php-sdk\phpdev\vc11\x86\pecl''
- Extract or clone the extension source code to the ''pecl'' directory
* if cloning, clone to a subdirectory, e.g. ''pecl/apcu''
* source code archive should already contain a subdirectory named e.g. ''apcu-4.0.7''
- Open a command prompt, run the ''setvars'' script, and enter your PHP source directory
- Rebuild the configure script by running: <code bash>buildconf</code>
- Executing ''configure --help'' should now contain an option to enable APCu <code>--enable-apcu Whether to enable APCu support</code>
- Configure and build: <code bash>configure --disable-all --enable-cli --enable-apcu</code><code>nmake</code>
- Test the binary with a ''php -m'' command, to make sure dbase is loaded
At this point, your compiled extension will be located in the release directory.
Additional Links:
* [[ http://www.ksingla.net/2010/05/adding-a-pecl-extension-to-your-php-build-environment/ | Adding a PECL extension to your PHP build environment ]]
* [[http://php.net/manual/en/install.pecl.windows.php | Installing a PHP extension ]]
=== LDAP Support Notes ===
- Add "--with-ldap" to your configure
- Make sure to package your generated binaries with libsasl.dll, which will not be built through this process - you can find it inside your C:\php-sdk\php53dev\vc9\x86\deps\bin\ folder, or you can download it from one of the PHP [[http://windows.php.net/download/|official binary packages]]
==== Pitfalls ====
**fatal error C1900: [] mismatch between 'P1' version '200801116' and 'P2' version '20070207'**
If you are using Visual Studio 2008 Express and Platform SDK 6.1 make sure you have the latest versions and check for version incompatibilities. There are known issues with old versions of VS 2008 Express and Platform SDK 6.1.
The order in which you install the Visual Studio 2008 Express Edition and Platform SDK 6.1 is important due to the issue reported in [[http://www.microsoft.com/downloads/en/details.aspx?FamilyID=e6e1c3df-a74f-4207-8586-711ebe331cdc&displaylang=en#ctl00_ctl13_Overview|Windows SDK for Windows Server 2008 and .NET Framework 3.5 Overview]], [[http://blogs.msdn.com/b/windowssdk/archive/2009/08/07/windows-sdk-for-server-2008-v6-1-overwrites-vs2008-sp1-crt-files.aspx|Windows SDK for Server 2008 (v6.1) overwrites VS2008 SP1 CRT files]] and [[http://blogs.msdn.com/b/windowssdk/archive/2009/08/07/installing-windows-sdk-for-server-2008-v6-1-after-vs2008-sp1-causes-conflicts-with-security-update-kb971092.aspx|Installing Windows SDK for Server 2008 (v6.1) after VS2008 SP1 causes conflicts with Security Update (KB971092)]]. Install the SDK first, with any of its service packs and security updates before installing Visual Studio 2008 Express Edition.
Make sure that you are running setenv /xp /x86. For example, setenv /xp /x64 or setenv /vista /x64 causes the above error message even if you are using Visual Studio 2008 Express SP1 and Platform SDK 6.1 (60001.18000.367) on a Vista 64bit system.
==== Utilities ====
=== Resource Hacker ===
Utility to show dependency information and other assembly information from a DLL. For instance, it shows which version of the Visual C++ Runtime the DLL was linked against.
Get it here: http://angusj.com/resourcehacker/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment