Recompiling PHP5 With Bundled Support For GD On Ubuntu

Want to support HowtoForge? Become a subscriber!
 
Submitted by patusovniak (Contact Author) (Forums) on Tue, 2009-07-28 11:36. :: Ubuntu | PHP

Recompiling PHP5 With Bundled Support For GD On Ubuntu

I’ve been installing the LAMP stack on an Ubuntu based server for a client. They rely on the version of LibGD that’s bundled with PHP5. This version includes certain functions including imagerotate() and others.

It seems that the people who build Ubuntu (and Debian) distribution do not want to compile PHP5 with the version of GD that comes bundled with PHP5. As far as I can tell the guys at Ubuntu (and Debian) consider this to be a fork of the original GD library and think that this could be a security risk. Their stance on it is that if PHP want to use this version then they should contribute the changes that have been made to the original GD library back to the core project. This seems fair enough, but unfortunately I can’t immediately change the direction of an open source project this big – or probably any project, come to think about it!

After a bit of rummaging around on the Internet I found nothing to do with actually compiling in this support, so I tried to work out how to do this… In theory! I found a link on Ubuntu forums about including the GMP library I roughly followed this through.

To install the required packages and download the source code for PHP5:

Install build tools, debian helpers and fakeroot:

apt-get install build-essential debhelper fakeroot

Source code should reside in /usr/src:

cd /usr/src

Download PHP source:

apt-get source php5

Install all packages required to build PHP5:

apt-get build-dep php5
cd php5-5.2.3

Now a package is compiled is set within the files contained in the debian directory of a package. Rules for configuring the compile process can be found in debian/rules. In this file there is a line that reads --with-gd=shared,/usr --enable-gd-native-ttf \. This links to the Ubuntu distributed version of LibGD as a shared library. It is part of the autoconf script that customises the compilation of PHP. I replaced this line with --with-gd=shared --enable-gd-native-ttf \. This causes the compilation process to use the bundled version of GD and make a shared library.

Once the package has been reconfigured it can be compiled and installed by:

Build the php5-* packages:

dpkg-buildpackage -rfakeroot
cd ..

Install the new php5-gd package

dpkg -i php5-gd_5.2.3-1ubuntu6.3_i386.deb

A quick

/etc/init.d/apache2 restart

and hey presto, you should be using the bundled version of LibGD for PHP. Hurrah!


Please do not use the comment function to ask for help! If you need help, please use our forum.
Comments will be published after administrator approval.
Submitted by Gia Phu LIU (not registered) on Sun, 2011-04-03 23:45.

Hi,

 I got an error on Ubuntu/Hardy by using the method above.

 cat /usr/src/php5-5.2.4/test-results.txt 

Warning: putenv(): Safe Mode warning: Cannot set environment variable 'SSH_CLIENT' - it's not in the allowed list in /usr/src/php5-5.2.4/run-tests.php on line 70

Warning: putenv(): Safe Mode warning: Cannot set environment variable 'SSH_AUTH_SOCK' - it's not in the allowed list in /usr/src/php5-5.2.4/run-tests.php on line 71

Warning: putenv(): Safe Mode warning: Cannot set environment variable 'SSH_TTY' - it's not in the allowed list in /usr/src/php5-5.2.4/run-tests.php on line 72

Warning: putenv(): Safe Mode warning: Cannot set environment variable 'SSH_CONNECTION' - it's not in the allowed list in /usr/src/php5-5.2.4/run-tests.php on line 73

Warning: set_time_limit(): Cannot set time limit in safe mode in /usr/src/php5-5.2.4/run-tests.php on line 76

+-----------------------------------------------------------+

|                       ! WARNING !                         |

| You are running the test-suite with "safe_mode" ENABLED ! |

|                                                           |

| Chances are high that no test will work at all,           |

| depending on how you configured "safe_mode" !             |

+-----------------------------------------------------------+

ERROR: invalid PHP executable specified by TEST_PHP_EXECUTABLE  = ./apache2-build/sapi/cli/php

 

So I solved it this way:

I've downloaded the newest PHP5 Version: 5.2.17 (because I got an error on 5.3.6) to /usr/src/

tar -xzvf filename

 cd /usr/src/php-5.2.17

 ./configure --with-apxs2=/usr/bin/apxs2 --with-mysql=/usr --with-mysqli=/usr/bin/mysql_config --with-pgsql=/usr --with-tidy=/usr --with-curl=/usr/bin --with-curlwrappers --with-openssl-dir=/usr --with-zlib-dir=/usr --with-xpm-dir=/usr --with-pdo-pgsql=/usr --with-pdo-mysql=/usr --with-xsl=/usr --with-ldap --with-xmlrpc --with-iconv-dir=/usr --with-snmp=/usr --enable-exif --enable-calendar --with-bz2=/usr --with-gd --with-jpeg-dir=/usr --with-png-dir=/usr --with-zlib-dir=/usr --with-freetype-dir=/usr --enable-zip --with-pear

(you can add soap or openssl if you want to or other modules) 

 make

 make -i install

 cd /etc/apache2/mods-available/

make sure that "php5.load" look similar like this:

LoadModule php5_module /usr/lib/apache2/modules/libphp5.so

 

make sure that "php5.conf" look like this:

cat php5.conf 

<IfModule mod_php5.c>

  AddType application/x-httpd-php .php .phtml .php3

  AddType application/x-httpd-php-source .phps

</IfModule> 

 

if not, enable php5 module by using this command:

a2enmod php5

 restart apache:

/etc/init.d/apache2 restart

 

then check the phpinfo() 

I hope I could help some people out there.

cheers,

 Phurx

Submitted by petemaxi (not registered) on Fri, 2011-01-28 18:28.

Hi, I noticed the 64-bit .deb and thank you for that, but what I really needed was a 32-bit .deb, couldn't find one, so I made my own.
Here's the link:-
http://www.maxiwebs.co.uk/gd-bundled/

Submitted by petemaxi (not registered) on Sun, 2011-01-30 20:18.
If you've been struggling to follow the link, it's because the URL has a space at the end for some reason. Get rid of the offending space and your page should load. Hopefully someone on this site can edit this mistake in my previous post and get rid of this one. http://www.maxiwebs.co.uk/gd-bundled/
Submitted by petemaxi (not registered) on Mon, 2011-02-07 11:56.

I have updated http://www.maxiwebs.co.uk/gd-bundled/ . It now has 32 and 64 bit versions of the bundled gd library for three different versions of php, the latest php versions for Ubuntu 9.10, 10.04 and 10.10.

Compiling is such a faff, so I've done it for you, enjoy!

Submitted by Chad R. Smith (not registered) on Thu, 2010-09-09 13:52.

Hey everyone,

I thought I would make this much simpler for people and provide my .deb that I created using this method. Since it takes 1 1/2 hours to get configured and it’s just easier to grab the .deb and go.

Ubuntu version 10.04 (64-bit)
http://2shared.com/file/R0PqWXeC/php5-gd_532_1ubuntu42_amd64.html

Please note that it’s the 64-bit version, so if you are on a 32 bit system it wouldn’t work. We also tested in our office with 9.04, 9.10, 10.04, and 10.10 (beta)

Now, you install this and it works perfectly, but your update manager will tell you that there’s an update. DO NOT install the update it will revert the changes. How do you get the updater to hold the package? By following the steps:

HOW TO GET THE PACKAGE UPDATER TO NOT UPDATE THIS:

1.) Open a terminal
2.) Run the following command: sudo apt-get install wajig
3.) Say Yes to the install
4.) After install do the following: sudo wajig hold php5-gd

There you go now you will have GD bundled 2.0.34, and the package updater will hold the package so it's never updated again.

Take care,
Chad R. Smith

Submitted by Anonymous (not registered) on Fri, 2010-11-12 08:22.

I tried a recompile after forgetting to change the class on a 64-bit ubuntu 9.10 for php5.2 on virtualbox with this manual and the process choked on a call trace ending in child_rip+0x0/0x20

I ran make clean from within the source directory to clean up before recompile but it complained that There is no rule to target 'clean' it.

Is it necessary to symlink to the correct libraries for jpeg like described at http://php.net/manual/en/image.installation.php by Larry Kluger? They seem to be in place already. If so, how do I go about things in light of the above tutorial.

Also @Chad, thanks for the precompile but unfortunately the link is buggered. Do you have a correct address?

I figure that i'm missing some very obvious requirements that are not mentioned  in this tutorial.I welcom suggestions.

Submitted by Anonymous (not registered) on Wed, 2010-05-26 10:31.

Hi, thanks for this great post. I followed the instructions as described however when it came to installing the compiled package, it could not be located. It might be because I am using the latest version of ubuntu and do  not know the correct name for the package. I also went ahead to list the available packages but did not see any under a similar naming style to the one that you have in your post.

 Any pointers in the right direction would be appreciated.

Thanks

Submitted by Anonymous (not registered) on Sun, 2013-11-03 03:32.

 Had the same thing happen. Noticed in the messages it said to use -d.

sudo dpkg-buildpackage -d

This worked for me. It took 1 hour to compile. It made several files including:

 php5-gd_5.3.10-1ubuntu3.8_i386.deb

Followed the rest of the instructions fine. Now have bundled and imagerotate() and imagettftext()

You can check for this with function_exists('imagerotate') and function_exists('imagettftext') in php. 

Submitted by Anonymous (not registered) on Fri, 2010-04-23 05:50.
You will need to "cd .." before you execute "dpkg-buildpackage -rfakeroot" you will get an error otherwise.
Submitted by Vertigo (not registered) on Wed, 2010-02-24 01:39.

Worked perfectly, thanks!

 A few notes to anyone else that comes across this. You can use sudo instead of -rfakeroot just fine (it gave me problems), and expect the packages to take a LOOONG time to build - took me upwards of 40 minutes, I could be off there, wasnt timing.

Once done, youll have a bunch of deb files, most of which you wont need, but the install of the new gd package will take seconds, and youre done.

Test it before and after by trying to use imagerotate()

Submitted by Liberty Dandira (not registered) on Mon, 2010-02-22 19:59.
You are a star..thanks a lot...those with AMD64 servers, I have the deb which I think you can install that I have compiled. Contact me so that I can send you the deb.
Submitted by Paul Stenning (not registered) on Sun, 2010-01-10 15:52.

I have just completed this on a fully up to date Ubuntu 8.04 LTS server.  Excellent post, works really well! 

Two minor changes were needed because it had a slightly later version of PHP installed.

  • The directory name is now php5-5.2.4
  • The compiled package name is now php5-gd_5.2.4-2ubuntu5.9_i386.deb
Submitted by Leszek (not registered) on Wed, 2009-08-05 13:58.
In case of dependency problems, apt-get -f install should sort things out.
Submitted by Anonymous (not registered) on Fri, 2010-06-11 16:50.

Hi,

this tutorial works fine. But how can i hold the php5-gd library? Every time when i run apt-get update / apt-get upgrade my system wants to override the new compiled one. Any ideas?

Thank you

Submitted by Gordon (not registered) on Tue, 2010-11-16 17:23.

Yes, I am experiencing the same issue. I am using Turnkey Linux, and the default cron jobs run apt and revert to the original php-gd with separate ubuntu compiled library.

 I have been looking through the cron files to see what I can comment out! Any advice or direction much appreciated.

 Thanks,

 G

Submitted by Anonymous (not registered) on Sat, 2011-05-21 14:23.

Edit /etc/apt/apt.conf.d/50unattended-upgrades to modify the Unattended-Upgrade::Package-Blacklist section to include the packages you do not want automatically upgraded.

 Example:

 // List of packages to not update
Unattended-Upgrade::Package-Blacklist {
        "php5";
        "php5-gd";
        "libapache2-mod-php5";
};

Submitted by Andriy (not registered) on Fri, 2011-02-04 20:29.
Thank you from Ukraine!