Compile PHP on Windows

Share this article

Compiling PHP from source code is more commonly done on Unix-type systems. Those working in a Windows environment are more likely to download and install PHP from precompiled packages. And while I don’t disagree it’s easier to use a precompiled solution, even on Unix systems, there are some advantages that can come with compiling a binary from source. In general:

  • You have the ability to fine tune the final product when you compile. Maybe you want to have a particular extension compiled directly into the binary instead of loading it as an external library. Or, perhaps you want to turn off a particular feature which would normally be available by default.
  • You can make tweaks to the compilation process if you’re so inclined which may enhance performance for your particular environment (of course this assumes you already know what you’re doing in which case you wouldn’t be reading this article).
  • Compiling might be the only way to get things to work if the precompiled binaries were built against older versions of supporting software and libraries than you are running on your system.
But be forewarned: compiling can be a frustrating task, especially on Windows! You must ensure your build environment is set up correctly, learn how to use the compiler and other build tools properly, and satisfy any library dependencies. Hopefully this article is your first step in overcoming many of these obstacles.

Setting Up the Build Environment

PHP is written in C and so a C compiler is necessary if you’re going to build PHP from source. C++ is a super set of C, so a good C++ compiler should be able to compile C code as well, though sometimes this isn’t always the case. For Windows, Microsoft’s Visual C++ Express (to which I’ll refer to as VC++ from here after) should suffice and is freely available from Microsoft’s website. I’m using the 2010 edition for this write up. When choosing your compiler version, you should keep in mind how you will be running PHP. If you’ll be running mod_php with an officially precompiled Apache binary then you’ll want to compile PHP using Visual Studio 6 since that’s the version used to compile Apache. The module needs to target the same runtime library as Apache, in this case msvcrt.dll. If you’re building Apache from source as well, or if you’re going to run PHP as CLI or FastCGI, then this isn’t an issue and 2010 will work just fine. You’ll also need to install the Windows Software Development Kit (here after SDK) as well. The SDK supplies us with important header files for the Windows platform which we’ll need for a successful compile. It too is available for free; I’m using version 7.1. Install the compiler first and then the SDK. I won’t discuss the installation since both have a graphical installation wizard to guide you through the process. Once you have a working compiler set up, download the binary tools and deps packages from windows.php.net/downloads/php-sdk. The binary tools package (I’m using the 20110915 archive) contains development tools like re2c, bison, and some additional commands you’ll need to build PHP. The deps package (I’m using the 5.4 archive since that matches the version of PHP I’ll be compiling) contains the minimum header and library dependencies needed, such as zlib.h. It probably goes without saying that you’ll want to download the PHP source as well from windows.php.net/download. At the time of this writing, the current version of PHP is 5.4.6 so that’s the version number you’ll see in my examples. It’s a good idea to create a workspace to which you can unpack the source code to and compile in without mucking up the rest of your system. Create the folder C:PHP-Dev which will serve as the working directory, and then unpack the binary tools archive into it. Next, extract the contents of the PHP source archive to C:PHP-Dev so you have the php5.4 source folder there, and then extract the deps archive into a sibling deps folder. Your directory structure should look similar to this:

Open the Windows SDK Command Prompt that was installed with the SDK (Start > Microsoft Windows SDK > Windows SDK Command Prompt) and execute these commands:
setenv /release /xp /x86
cd C:PHP-Dev
binphpsdk_setvars.bat
Using the SDK Command Prompt console is desirable over the ordinary cmd.exe console as it sets many environment variables specific for compiling source code. The compile commands later should also be executed in this console. The flags to setenv set some build properties for the environment; In this case I’ve set the environment to target a Windows XP 32-bit release build. You can try and build with /x64 if you’re feeling adventurous, but there are still some issues with it. Specifying different versions of Windows such as /vista will most likely yield problems because of some odd defines in the build scripts (PHP still aims to be XP-compatible). Unless you really know what you are doing, it’s probably safest to stick with the recommended values that I used above. phpsdk_setvars.bat
script then goes on to set some additional environment variables so the build process can find the binary tools. Keep in mind that all this variable setting is only temporarily for your console’s session. If you close out of the prompt and go back to compile later, you’ll need to run the commands again. If you don’t, you’ll receive errors like the following when you run configure later in the process and be unable to proceed:
Checking for bison.exe ...  <not found>
ERROR: bison is required
Making sure you have the correct build environment, the required sources, and any dependencies is the hardest part of the process. So now that your environment is set up and the source code and dependencies are in their proper place, it’s time to compile!

Compiling PHP

In the SDK Command Prompt, navigate to the PHP source folder and run buildconf. The command is responsible for generating a configuration file which will create a Makefile to drive the compilation process. After buildconf completes (it should only take a second), run configure --help and examine what functionality you wish to enable/disable, and then re-run configure with any desired options. It’s a good idea to inspect the output before moving on since it will warn you if any of the necessary dependencies are not available. If that happens, you can either install the dependencies and re-run configure again or adjust the invocation to disable the extensions that require them. Finally, run nmake to kick off the compile.
cd C:PHP-Devphp5.4
buildconf
configure
nmake
nmake test
If either configure or nmake fails, there’s a good chance the problem is one of two things: 1) your environment is not set up correctly, or 2) you’ve enabled a feature which depends on an external library and the library is not installed on your system. Double check that you’ve set up the environment according to the instructions above and that any extra libraries which may be necessary based on your configure options have been installed. When the first nmake compile process has completed you’ll find your shiny new PHP binaries in the Release_TS folder. nmake test runs the new binaries through a battery of bug tests to make sure things are working as they should be. The results of nmake test are forwarded to the QA team which depends on them to improve PHP, so even though it may take a few minutes to run, it’s the responsible thing to do. At this point you can also take the extra step of running nmake snap which will create ZIP archives with the binaries which you can copy around.

Compiling Extensions

There are two ways to compile PHP extensions: statically and dynamically. A statically-compiled extension is compiled into the PHP binary itself, while a dynamically-compiled one is a separate DLL which can be loaded later through the php.ini file. Extensions are typically compiled as DLLs, although there are some advantages to static compilation as well; it ultimately it depends on your needs. To compile PHP extensions on Windows, extract the extension’s source code folder into the ext
folder of your PHP source directory. Then, rebuild the configure script by running buildconf --force and re-compile PHP using the appropriate flags to enable the extension. As an example, let’s compile the AOP extension statically. Download the source code from PECL, and extract it’s folder into ext. Then execute the following:
cd C:PHP-Devphp5.4
buildconf --force
configure --enable-aop
nmake
The --force option to buildconf forces it to rebuild the configuration scripts. Afterwards, run configure --help and you should see the option to include the new extension in the output. In this case, it’s --enable-aop. When nmake finishes, you’ll have a newly built PHP binary with the AOP extension baked right in. If you want an extension to be available as a DLL and not baked into PHP, you can follow the same steps as above but specify “shared” as a value to configure’s enable option.
buildconf --force
configure --enable-aop=shared
The resulting DLL will be in the Release_TS folder alongside the PHP binary once compilation has finished, in this case named php_aop.dll. Compiling on Windows is still a bit tricky, especially when it comes to extensions. The windows version of phpize seems to be broken and I have yet been able to compile a DLL after-the-fact, much like how PECL does. There have been tremendous strides made by the PHP team in the past five years or so towards making PHP just as awesome on Windows as it is on Unix, so hopefully the snags and wrinkles will be ironed out in time. In the meantime, I recommend compiling PHP and your shared DLLs at the same time.

Summary

The ability to compile source code is a good skill to have, especially if you later want modify PHP. Perhaps you want to add new functionality, link against a new library, or just be the next great PHP core developer (they can always use the help!). And now that you know how, feel free to hack and build away! Image via Fotolia

Frequently Asked Questions (FAQs) on Compiling PHP from Source on Windows

What are the prerequisites for compiling PHP from source on Windows?

Before you start compiling PHP from source on Windows, you need to have certain software and tools installed on your system. These include Visual Studio, Bison, and Git. Visual Studio is required for its C++ compiler and development environment. Bison is a parser generator that is used in the build process. Git is a version control system that is used to download the PHP source code. You also need to have the PHP source code, which can be downloaded from the official PHP website.

How do I install the necessary software and tools for compiling PHP?

You can download Visual Studio, Bison, and Git from their respective official websites. After downloading, you can install them by following the instructions provided on their websites. For Visual Studio, you need to select the “Desktop development with C++” workload during installation. For Bison, you need to add its bin directory to your system’s PATH environment variable. For Git, you can use the default installation options.

How do I download the PHP source code?

You can download the PHP source code from the official PHP website. You need to select the version of PHP that you want to compile. After selecting the version, you can download the source code as a zip file. After downloading, you need to extract the zip file to a directory on your system.

How do I compile PHP from source?

After installing the necessary software and tools and downloading the PHP source code, you can compile PHP from source by following these steps: Open a command prompt and navigate to the directory where you extracted the PHP source code. Run the buildconf.bat file to generate the configure script. Run the configure script with the desired options. Run the nmake command to start the build process. After the build process completes, you can find the compiled PHP binary in the Release directory.

What are the options for the configure script?

The configure script has many options that you can use to customize the build process. These options allow you to enable or disable certain features of PHP, specify the locations of libraries, and set various other build parameters. You can view a list of all available options by running the configure script with the –help option.

How do I use the compiled PHP binary?

After the build process completes, you can find the compiled PHP binary in the Release directory. You can use this binary to run PHP scripts by specifying the script file as a command-line argument. You can also install the binary on your system by copying it to a directory on your system’s PATH environment variable.

What are the common issues encountered while compiling PHP from source and how can I troubleshoot them?

Some common issues that you might encounter while compiling PHP from source include errors during the build process, missing libraries, and incorrect build options. You can troubleshoot these issues by checking the error messages, ensuring that all necessary software and tools are installed correctly, and verifying the options for the configure script.

Can I compile PHP from source on other operating systems?

Yes, you can compile PHP from source on other operating systems, such as Linux and macOS. The process is similar to compiling on Windows, but there are some differences in the software and tools that you need to install and the commands that you need to run.

What are the benefits of compiling PHP from source?

Compiling PHP from source allows you to customize the build process to suit your specific needs. You can enable or disable certain features of PHP, specify the locations of libraries, and set various other build parameters. This can result in a PHP binary that is optimized for your system and application requirements.

Is compiling PHP from source recommended for beginners?

Compiling PHP from source can be a complex process that requires a good understanding of software development and build systems. Therefore, it is not typically recommended for beginners. However, if you are interested in learning more about how PHP is built and want to customize the build process, compiling from source can be a rewarding experience.

Timothy BoronczykTimothy Boronczyk
View Author

Timothy Boronczyk is a native of Syracuse, New York, where he lives with no wife and no cats. He has a degree in Software Application Programming, is a Zend Certified Engineer, and a Certified Scrum Master. By day, Timothy works as a developer at ShoreGroup, Inc. By night, he freelances as a writer and editor. Timothy enjoys spending what little spare time he has left visiting friends, dabbling with Esperanto, and sleeping with his feet off the end of his bed.

Expert
Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week