You are not logged in.
Login or Register

Tip: Build PHP as a static binary

Posted By: apeiro

How to build PHP (from source) as a static binary.

In linkable object parlance, a "static binary" means that the resulting compiled program does not require any external support libraries in order to run.

This is handy if you are distributing a PHP binary with your software, since you won't need to worry about which version of cURL, GD, etc are present on the recipient's server.

This example will build a PHP CGI binary.

After unpacking the source tarball, we'll run this configure command. Yours will vary, but make sure you include the --enable-static option.

$ ./configure --enable-static --enable-cgi \
    --enable-force-cgi-redirect \
    --with-config-file-path=/etc/php5cgi \
    --prefix=/usr/local/php5cgi' \
    --with-gd --with-mysql --with-mcrypt --with-mhash --with-curl \
    --enable-ftp --with-openssl --enable-sockets \
    --with-zlib --with-zlib-dir=/usr/include \
    --with-pear --enable-soap \
    --with-imap --with-imap-ssl --with-kerberos \
    --with-jpeg-dir --with-png-dir --with-pdo-mysql --with-mysqli

Now, before running make, we need to modify the Makefile in the top-level directory.

Open up Makefile in your editor, and look around line 23 (we're using PHP-5.2.5 as an example). You should see a line that looks something like this:

BUILD_CGI = ... $(ZEND_EXTRA_LIBS) -o $(SAPI_CGI_PATH).

You need to add the -all-static flag right before the -o parameter, so it looks more like this:

BUILD_CGI = ... $(ZEND_EXTRA_LIBS) -all-static -o $(SAPI_CGI_PATH).

Now run your make command. When it's done, you can verify the static-ness of your binary like so:

$ file sapi/cgi/php-cgi