In my quest to learn php extension development, I have been told I need to compile PHP from source. Given that it was a bit tricky, I thought I would document it here because chances are I'll need to refer back to this at some point. I did all of this on my Win XP machine. Details may vary on different OS versions. This is based largely on this wiki.

The following steps resulted in a build using Windows SDK 6.1.

1) Download and install Visual C++ Express. I used VC2008 Express:
http://www.microsoft.com/express/downloads/

2) Download and install Windows SDK 6.1.

3) Create a folder to put everything in. I used C:\php-sdk so that's what I'll refer to in the following steps.

4) Download and unpack the PHP Binary Tools Archive into C:\php-sdk. Make sure you see a bin and script folder in there.

5) Open the 6.1 SDK's CMD shell. I did this by selecting Start -> All Programs -> Microsoft Windows SDK v6.1 -> CMD Shell.

6) set some env vars:

setenv /x86 /xp /release

7) CD to this dir. Note I had to use the /d flag for some reason to get the cd command to work. You may or may not need it.

cd /d c:\php-sdk\

8) Set some vars:

bin\phpsdk_setvars.bat

9) build a directory tree:

bin\phpsdk_buildtree.bat php53dev

If this command complains, try creating the directory C:\php-sdk\php53dev[/code] first.

10) Download the PHP source you'd like to compile and extract it in C:\php-sdk\php53dev\vc9\x86. I downloaded my source from the link at the top of this page. DO NOT BOTHER with the binaries. Just get the source. The resulting folder should be something like C:\php-sdk\php53dev\vc9\x86\php-5.3.X

11) You need to fetch some code libraries that PHP uses and put them in the deps folder located at C:\php-sdk\php53dev\vc9\x86\deps. Rather than building them yourself, I recommend you download them from this page. Note that page may list more than one version of a given library. Get the latest one. I downloaded them all and saved the zip archives in my deps folder and unzipped them there. Each zip archive should contain a bin, lib, and maybe an include folder so make sure when you extract them into the deps folder that they should put their contents in the lib/bin/include folder there rather than in some other folder name.

12) In the CMD Shell window, change dirs again:

cd /d C:\php-sdk\php53dev\vc9\x86\php5.3-X

Again, I had to use the /d flag to get this to work. you may be able to leave it out.

13) In the CMD Shell, execute this command:

buildconf

14) To get an idea of what your config options are, type this in the CMD Shell:

configure --help

15) I chose this configuration and it seems to work. CMD Shell:

configure --enable-apache2-2handler --with-curl --enable-fileinfo --enable-mbstring --enable-mbregex --with-mcrypt --with-openssl --with-pgsql --enable-sockets --with-mysql --with-mysqli --enable-pdo --with-pdo-mysql --with-pdo-mssql --with-pdo-pgsql --enable-soap --with-xmlrpc --with-xsl

16) make the project. this should take some time. CMD Shell:

nmake

if you have problems, try this as it will remove old stuff and try to make everything all fresh. CMD Shell:

nmake clean all

17) if you want the resulting php to be zipped for redist, run this too:

nmake snap

18) the compiled php EXEs and DLLs are now under C:\php-sdk\php53dev\vc9\x86\php5.3-X\Release_TS.

You can try running the newly compiled version of PHP. NOTE that if you had php installed previously, you need to type the complete path to the new PHP.exe or you'll just be interacting with your previously installed version. Also, the newly compiled php doesn't find an INI file so you need the -n flag. Try this:

C:\php-sdk\php53dev\vc9\x86\php-5.3.3\Rele
ase_TS\php.exe -n -i | more

This lets you page through the output of phpinfo() by hitting the space bar.

That's it for now. I'm not really sure how to install this just yet. There are some useful tips for setting your path and file assocations here and hereHints and tips are welcome.

    Nice post there, worthy of a sticky.

      Why can I not edit this post? I'd like to make some corrections.

        sneakyimp;10959610 wrote:

        Why can I not edit this post?

        You can only edit your post(s) for a certain number of hours after the initial post was made.

        If you want to make corrections, feel free to post them as a new reply and I'd be glad to merge them into your original post above.

          Thanks BG. Gonna take a bit...i'm trying to sort out debugging setup at the moment. total nightmare.

            19 days later

            this is a very nice and informative web site so i like to visit the City News gathering web site which have the knowledge about cities.

              2 years later

              Hello sneakyimp,
              I want to install php(any version) in my windows-7 system (64 bit). According to you instruction I have installed Windows-SDK v7 because its latest one along with vc++ 2010 because when I read the system requirement of SDK v7 in that they mention " you cannot use the .NET 4 tools with Visual Studio 2008". And I also face some problem during compilation when I used vc++ 2008. Now I am using Windows-7, Win-SDK v7, php-5.4.10-src, deps-5.4-vc9-x86. But after follow your instruction after doing nmake I got Linker error :--

              php5ts.dll.def : error LNK2001: unresolved external symbol xmlXPtrNewLocationSet
              Nodes
              php5ts.dll.def : error LNK2001: unresolved external symbol xmlXPtrNewRange
              php5ts.dll.def : error LNK2001: unresolved external symbol xmlXPtrNewRangeNodeOb
              ject
              php5ts.dll.def : error LNK2001: unresolved external symbol xmlXPtrNewRangeNodePo
              int
              php5ts.dll.def : error LNK2001: unresolved external symbol xmlXPtrNewRangeNodes
              php5ts.dll.def : error LNK2001: unresolved external symbol xmlXPtrNewRangePointN
              ode
              php5ts.dll.def : error LNK2001: unresolved external symbol xmlXPtrNewRangePoints

              php5ts.dll.def : error LNK2001: unresolved external symbol xmlXPtrRangeToFunctio
              n
              php5ts.dll.def : error LNK2001: unresolved external symbol xmlXPtrWrapLocationSe
              t
              php5ts.dll.def : error LNK2001: unresolved external symbol zError
              php5ts.dll.def : error LNK2001: unresolved external symbol zlibCompileFlags
              php5ts.dll.def : error LNK2001: unresolved external symbol zlibVersion
              x64\Release_TS\php5ts.lib : fatal error LNK1120: 1601 unresolved externals
              NMAKE : fatal error U1077: '"C:\Program Files (x86)\Microsoft Visual Studio 10.0
              \VC\Bin\amd64\cl.exe"' : return code '0x2'
              Stop.

              Regards and Thanks,

              Antrikssh...

                I don't think I was ever brave enough to compile PHP on Windows (or even use Windows for any server-related role 😉), but based on those compiler errors, it sounds like you're missing (or VS can't find) the libxml and zlib libraries.

                  I'm not super-duper windows guy, but if you check the original wiki you will see that they don't support VC 10. Also, as far as I can tell, the steps I published above use SetEnv to set /x86 and /xp -- I'm not certain but I believe this will mean that you are trying to compile a 32-bit PHP (sigh) and telling the compiler that windows xp is your target platform.

                  Unfortunately, the wiki has not been updated recently. I'll see if I can contact the PHP-DEV mailing lost or the PHP-DOC mailing lists and ask for a new set of instructions that is more current.

                    sneakyimp wrote:

                    Unfortunately, the wiki has not been updated recently. I'll see if I can contact the PHP-DEV mailing lost or the PHP-DOC mailing lists and ask for a new set of instructions that is more current.

                    If you can get PHP 5.5 built to run on Windows XP it would be a bonus 🙂

                      Hello Everyone,

                      Thanks for giving me your precious time. I found the solution that day only. I want to share my experience with all of you so that my post will help others. Solution of the problem is , we have to complie the code in 32 bit environment. I mean to say that whatever is the cpu architercture(x86, x64) we have to compile the code in x86.

                      setenv /x86 /yourOS(x86/x64) /release

                      follow all the step that define above. On more thing you complie the php source files in x86 env. so you have to use the library code that php use is also for x86. This will remove linking error that I got during my exercise.

                      May this will help some one......

                      Thanks and Regards,
                      Antrikssh...........

                        6 days later

                        Sorry for my belated response. I have been traveling.

                        Thanks for the detail, Antrikssh. What value did you actually use for /yourOS(X86/X64) ?

                        Also, I got some detail from the PHP-DEV mailing list. The lines starting with > were my original questions.

                        Pierre wrote:

                        In particular, there are a couple of aspects to that wiki entry that look a
                        bit old:
                        * setenv flag of /x86 -- what about 64-bit?

                        x64 builds are not yet officially supported. However nothing prevents one to do it. It should build fine.

                        • setenv flag of /xp -- what about win 7?

                        This sets the minimum windows version for PHP 5.3 or 5.4. It obviously creates bins compatible with Windows 7 or even windows 8.

                        5.5 will use windows 7 as a minimum version

                        • setenv flag of /2008 -- what about VC++ 2010?

                        5.3 and 5.4 builds are done using vc9 (vs 2008). It is strongly recommended to use vc9 to build extensions targetting 5.3 or 5.4.

                        There was a response to Pierre's message from Jan Ehrhardt. Pierre's original comments start with >:

                        Jan Ehrhardt wrote:

                        > x64 builds are not yet officially supported.

                        Not yet? That sounds if x64 will be supported sometime. Will it?

                        However nothing prevents one to do it. It should build fine.

                        If you can get all dependencies right. For x86 many of them are
                        available at http://windows.php.net/downloads/php-sdk/
                        But, for instance, libiconv_a.lib is nowhere available as precompiled
                        binary for x64. And MS Visual C/C++ with "nmake" is no longer supported
                        by GNU since iconv 1.11.1. So you will have to cook your own Makefiles
                        or VCproj files. At the moment I am compiling 1.14 for x64 in VC10, but
                        with the v90 Platform Toolset, so you'll end up with VC9 libs after all.

                        About the deps on downloads/php-sdk: I see you are using different deps
                        for PHP 5.3 and PHP 5.4 nowadays. Are the differences great?

                        When I compile PHP for x86 I always use the same deps. Only for the non
                        standard extensions couchdb and eAccelarator I have to use different
                        sources because their PHP 5.4 adjustments are not backwards compatible.

                        5.3 and 5.4 builds are done using vc9 (vs 2008). It is strongly recommended
                        to use vc9 to build extensions targetting 5.3 or 5.4.

                        What are the plans for 5.5? A transition to VC10? I once tried to use my
                        existing php-sdk setup to compile PHP 5.4 with VC10, but not all
                        compilations succeeded. Compilation failed for php_intl.dll failed with
                        these errors:

                        C:\php-sdk\php54dev>nmake php_intl.dll | find "error"

                        Microsoft (R) Program Maintenance Utility Version 10.00.40219.01
                        Copyright (C) Microsoft Corporation. All rights reserved.

                        .\win32/php_stdint.h(101) : error C2371: 'int_fast16_t' : redefinition;
                        different basic types
                        .\win32/php_stdint.h(105) : error C2371: 'uint_fast16_t' : redefinition;
                        different basic types
                        NMAKE : fatal error U1077: '"C:\Program Files (x86)\Microsoft Visual
                        Studio 10.0\VC\BIN\cl.exe"' : return code '0x2'
                        Stop.

                        I did not dig into it to find out how to solve it, because I also got
                        VC++ Runtime errors. VC9 is good enough for the moment.

                        Jan

                        And then a follow-on message from Jan:

                        Jan Ehrhardt wrote:

                        php_intl version PECL-3.0.0b1 compiled fine with VC10. And the VC++
                        Runtime errors were caused by php_couchbase.dll. No big deal.

                        I'm not sure exactly what all this means as I haven't had time to try anything myself. Hope this helps. If you like, you can follow the goings-on in the internals mailing list here.

                        I also got this response:

                        Anatoliy wrote:

                        that wiki page is pretty much up to date. Also, if you have windows
                        specific questions, there is internals-win@lists.php.net .

                        Let me know if you guys want me to ask a specific question over there.

                          Hi,
                          I agree with your research because according to whatever I experienced during this exercise, I felt that working on php extension is not that much easier on x64 bit because lack of support. I don't know about the php-5.5 because I never worked on that but with php-5.4 source code have lot of issue regarding to Apache 2.2. Windows-SDk will build php5apache2_2.dll file but it won't work with the apache 2.2. Even php-5.4 is not working with the apache-2.2 it work with apache 2.4. Not even this issue I faced lot of issue regarding to php-5.4. So if you are working with php-extension, you have to work with x86 (in present scenario). I have used following combination that gave me correct working path for php-extension.

                          Windows-Xp(SP-3 32BIT)
                          php-5.3 Source code.
                          Apache 2.2
                          dep folder for(php-5.3)
                          Windows- SDK(v.7) for 32 bit.
                          VC++ - 2010

                          Thanks and Regards,
                          Antrikssh...

                            Weedpacket;11021093 wrote:

                            If you can get PHP 5.5 built to run on Windows XP it would be a bonus 🙂

                            Weedpacket, can you give me any detail about the problems you are having? I'd be happy to post it to the windows internal list.

                              sneakyimp wrote:

                              Weedpacket, can you give me any detail about the problems you are having? I'd be happy to post it to the windows internal list.

                              Oh, I haven't had any problems - mainly because I haven't got around to trying it yet. It's a reference to Step 6 of the build process. It's not clear whether the policy is "it won't build for XP" or "we don't care if it doesn't build for XP".

                                Weedpacket;11021769 wrote:

                                "we don't care if it doesn't build for XP".

                                ... which isn't a bad policy at all, considering that XP lost support from its creators back in 2009.

                                  bradgrafelman;11021773 wrote:

                                  ... which isn't a bad policy at all, considering that XP lost support from its creators back in 2009.

                                  And yet the instructions all have that -XP flag. Seems to me that these flags could have been named better.

                                    On a related note, here's an excerpt from the NEWS file in the PHP 5.5.0alpha2 release:

                                    PHP 5.5.0alpha2 NEWS wrote:

                                    . Drop Windows XP and 2003 support. (Pierre)

                                      Write a Reply...