Archive

Archive for January, 2012

How to compile PHP on a Linux Server

January 10th, 2012

Compiling PHP on a Linux server can be a bear of a project. The first time you try doing it, you might just frustrated and just want to install PHP with a RPM package. But you don’t get any flexibility with RPM packages, you just get the default install.

With a custom PHP install (compiling), you can pick which PHP options you want and don’t want. Do you need cURL (now default as of PHP 5.2+)? Using MySQL/phpMyAdmin? Then you will want to compile PHP with mcrypt and mbstring. These are just a couple of examples, but you will have plenty to choose from. We’ll learn about these options very soon…

So let’s start with the install. For my installation, I’m on a Red Hat Enterprise Linux 5 Server (RHEL5). So the paths/configurations I use are specific to that operating system (OS). I’m running MySQL 5+ and Apache 2+. For this installation you will need to have ssh access to your server. If you have a shared hosting account, you might have ssh access, but most likely you will not have the proper privileges to compile PHP. I’ll break this down into steps, to hopefully simplify the process.

  1. ssh to your server (if you don’t have an ssh client, try PuTTY. It’s free.)
  2. Become root on the server (if you have root access), by typing su - and then enter the root password. Or use sudo if that is an option.
  3. cd /usr/local/src
  4. In a browser go to php.net to get the source download URL (this changes depending on PHP version and the mirror location). In this example the download URL is http://us.php.net/get/php-5.3.8.tar.bz2/from/this/mirror
  5. Now download the file by typing wget http://us.php.net/get/php-5.3.8.tar.bz2/from/this/mirror
  6. Extract the PHP source files: tar xjvf php-5.3.8.tar.bz2
  7. cd php-5.3.8
  8. Now that you have the PHP source, and have extracted the files, you can find out what you’re install options are by typing:
    ./configure –h.
    You’ll get a really long list (a bit overwhelming) of compile options. You can dig through each of these options, or just go with what I’m going to do. The nice thing is that once you have compiled PHP once, it’s fairly painless to re-compile PHP with new options should you miss something.
  9. So here’s my configure command (notice that this is specific to 64bit RHEL). Note you should copy this into Notepad or similar to make sure all of the line breaks are ok:
    ./configure –build=x86_64-redhat-linux-gnu –host=x86_64-redhat-linux-gnu –target=x86_64-redhat-linux-gnu –program-prefix= –prefix=/usr –exec-prefix=/usr –bindir=/usr/bin –sbindir=/usr/sbin –sysconfdir=/etc –datadir=/usr/share –includedir=/usr/include –libdir=/usr/lib64 –libexecdir=/usr/libexec –localstatedir=/var –sharedstatedir=/usr/com –mandir=/usr/share/man –infodir=/usr/sh
    are/info –cache-file=../config.cache –with-libdir=lib64 –with-config-file-path=/etc –with-config-file-scan-dir=/etc/php.d –disa
    ble-debug –with-pic –disable-rpath –with-pear –with-bz2 –with-curl –with-exec-dir=/usr/bin –with-freetype-dir=/usr –with-
    png-dir=/usr –enable-gd-native-ttf –without-gdbm –with-gettext –with-gmp –with-iconv –with-jpeg-dir=/usr –with-openssl –with
    -pspell –with-pcre-regex=/usr –with-zlib –with-layout=GNU –enable-exif –enable-ftp –enable-magic-quotes –enable-sockets –ena
    ble-sysvsem –enable-sysvshm –enable-sysvmsg –enable-wddx –with-kerberos –enable-ucd-snmp-hack –with-unixODBC=shared,/usr –enable-shmop –enable-calendar –with-mime-magic=/usr/share/file/magic.mime –without-sqlite –with-libxml-dir=/usr –enable-force-cgi-
    redirect –enable-pcntl –with-imap=shared –with-imap-ssl –enable-mbstring=shared –enable-mbregex –with-ncurses=shared –with-gd
    =shared –enable-bcmath=shared –enable-dba=shared –with-db4=/usr –with-xmlrpc=shared –with-ldap=shared –with-ldap-sasl –with-m
    ysql=shared,/usr –with-mysqli=shared,/usr/lib64/mysql/mysql_config –enable-dom=shared –with-pgsql=shared –with-snmp=shared,/usr
    –enable-soap=shared –with-xsl=shared,/usr –enable-xmlreader=shared –enable-xmlwriter=shared –enable-fastcgi –enable-pdo=shared
    –with-pdo-odbc=shared,unixODBC,/usr –with-pdo-mysql=shared,/usr/lib64/mysql/mysql_config –with-pdo-pgsql=shared,/usr –with-pdo-sqlite=shared,/usr –enable-dbase=shared –enable-mbstring –with-mcrypt=/usr/local/bin/mcrypt –with-apxs2=/usr/sbin/apxs
  10. Note that you will most likely get a lot of errors after executing this command, but no need to panic. In most cases, the packages you are installing are dependent on “devel” packages. For example, you might get this error: Error: configure: error: Cannot find pspell. To fix this you need to install the pspell devel package: yum install aspell-devel. Here are a couple of sites that will help with dependencies: http://www.lifelinux.com/error-compiling-php-on-centos-x64/ and http://www.mickgenie.com/blog/php-compilation-error/
  11. So you’ll need to go back and forth between installing devel packages with yum, and then running your configure command again. Once your configure command has run successfully, move on to the next step.
  12. Now type make
  13. Then make test (really important to do this step)
  14. Finally make install
  15. Optional step: libtool –finish /usr/local/src/php-5.3.8/libs
  16. cp all modules from /usr/local/src/php-5.3.8/modules/*.so to /usr/lib64/php/modules/
  17. cp /usr/local/src/php-5.3.8/php-recommended to /etc/php.ini (this file contains configuration settings, like file upload size limits)
  18. edit /etc/php.ini to set modules path to /usr/lib64/php/modules/
  19. edit /etc/php.ini to set include_path for pear (if you installed pear)
  20. Now restart Apache: /usr/sbin/apachectl restart
  21. To test your php install, you should do two things:
    1. Type php -v (you should list out your new PHP version, in this case PHP 5.3.8)
    2. Go to your document root, and create a php file with this code: <?php phpinfo(); ?>, then load that file in a browser. This will give you a full configuration listing on PHP, Apache and MySQL
So hopefully everything worked out for you. There are many things that can go (temporarily) wrong with PHP compiles. But once you successfully compile PHP once, you’ll have a better handle on how to do it in the future.

php