Somacon.com: Articles on web development, software, and hardware
§ Home > Index > Web Development

How to Configure and Make PHP on FreeBSD

These are my notes for how to install a custom build of PHP 5 on FreeBSD.

These notes describe how to compile a custom-built version of PHP 5 from the source, and replace the stock build on a FreeBSD VPS server from Verio. The purpose of the custom-build is to install some extensions that can not be dynamically loaded and must be compiled in, specifically tidy and zip. These notes show how to build the Apache 2 shared module and CLI versions of PHP, but not the CGI version.

The server environment is a FreeBSD Virtual Private Server VPS v3 Hosted at Verio. Here is the server version: FreeBSD 6.2-RELEASE (VKERN_B) #11: Wed Mar 19 22:56:55 MDT 2008. PHP5 is pre-installed on this virtual dedicated server, or it can be installed using vinstall.

The two main files that come out of the build process are "php" and "libphp5.so"; these are called the "binaries". "php" is the CLI executable binary, and "libphp5.so" is an Apache 2 shared module. (The commands below do not build the CGI version of PHP, which may have less performance, but may be more suitable to a shared-server environment.) The new binaries are saved with version number to the appropriate directory. Instructions for creating soft links and updating the ini and apache conf files are provided.

Almost all the steps below must be run as root. Do not run these commands blindly - make sure you understand what they do and if they are appropriate for your server. I hope you find these notes useful.

INFORMATIONAL COMMANDS

#	This command displays existing configure options at the beginning. 
#	Use those options as a starting point for your custom build.
php -r "phpinfo();" | grep configure

# Display PHP loaded modules
php -m

# Display Apache compiled-in modules
httpd -l

SETUP WORKING DIRECTORY

su root
cd ~
mkdir ~/php
cd ~/php

DOWNLOAD TIDY SOURCE FROM CVS AND BUILD IT

Note: The tidy source can no longer be downloaded from the tidy website as an archive. You have to download it via CVS, which is simple.

cvs -d:pserver:anonymous@tidy.cvs.sourceforge.net:/cvsroot/tidy checkout tidy
cd tidy/build/gmake
gmake
gmake install
gmake clean

VERIFYING TIDY INSTALLATION

rehash
tidy --version
ls -la /usr/local/lib | grep libtidy.a

UPGRADE LIBXML

This step is optional.

# To find the latest version currently available in the ports collection
cd /usr/ports
make search name="libxml"
# Upgrade to the latest libxml
cd /usr/ports/textproc/libxml2
make
make install
make deinstall
make reinstall

GET LATEST PHP VERSION

wget http://us3.php.net/get/php-5.2.9.tar.gz/from/this/mirror
tar -xzvf php-5.2.9.tar.gz
rm php-5.2.9.tar.gz 
cd php-5.2.9

BUILD AND INSTALL PHP

# We enable apxs2 to create the Apache 2 module, and disable PHP-CGI.
# The main things we need are tidy and zip
# Since we are compiling many standard modules into the PHP binary, then
#	those extensions need to be disabled from extensions.ini later.
# By compiling many extensions into PHP, it makes the binaries larger.
#	e.g. 2.5 MB for the stock build, and 15 MB for the custom build.
#	However, this does not significantly impact memory usage for us.
# Some extensions can only be enabled by compiling them into the PHP binary.
# Use the command below to display help about the PHP configuration options.
./configure --help

My configure command is below, and it takes a minute or two to execute. You must customize it for your own use.

'./configure' '--disable-static' '--disable-debug' '--prefix=/usr/local/php5' '--with-config-file-scan-dir=/usr/local/php5/etc' '--enable-libxml' '--with-libxml-dir=/usr/local' '--enable-reflection' '--enable-spl' '--enable-zend-multibyte' '--with-regex=system' '--with-tidy' '--enable-zip' '--enable-bcmath' '--with-bz2=shared' '--enable-calendar' '--with-curl=shared' '--enable-dba' '--enable-exif' '--enable-ftp' '--with-gd' '--enable-gd-native-ttf' '--with-jpeg-dir=/usr' '--with-png-dir=/usr' '--with-zlib-dir=/usr' '--with-gettext=shared' '--with-gmp=shared' '--with-imap-ssl' '--with-imap' '--enable-mbstring' '--with-mcrypt=shared' '--with-mhash=shared' '--with-mysql' '--with-mysqli' '--with-openssl-dir' '--with-pdo-mysql' '--enable-sockets' '--with-xsl' '--with-zlib' '--with-apxs2' '--disable-cgi' '--enable-pcntl' '--enable-soap' '--enable-dbase' '--enable-sysvmsg' '--enable-sysvsem' '--enable-sysvshm'

# Takes about 15 minutes - go take a break
make
# This step is optional, if you want ~2500 checks to be run 
#  and you want to send the log to the PHP QA team.
make test
# Does not overwrite existing php.ini; takes < 1 minute
make install
# Clean up
make clean

RENAME NEW FILES TO VERSION-SPECIFIC APACHE MODULE AND BINARY

We rename the output binaries to include their version number. This way, we can keep each version of PHP around, just in case. Then, we use a soft link from "php" to the latest build.

# The build you just created will be named simply as "php"
ls -la /usr/local/php/bin/
mv /usr/local/php/bin/php /usr/local/php/bin/php-5.2.9
ls -la /usr/local/apache2/modules/
# If module exist, back it up
# mv /usr/local/apache2/modules/mod_php5-5.2.9.so /usr/local/apache2/modules/mod_php5-5.2.9-stock.so
mv /usr/local/apache2/modules/libphp5.so /usr/local/apache2/modules/mod_php5-5.2.9.so

UPDATE EXTENSIONS

#	don't load compiled-in extensions; comment them out with semi-colon; 
#		run "php -v" and look for warnings like:
#		"PHP Warning:  Module 'session' already loaded in Unknown on line 0"
pico /usr/local/php/etc/extensions.ini
#	update path to dynamic extensions
#	e.g. extension_dir = "/usr/local/php5/lib/php/extensions/no-debug-non-zts-20060613/"
# Sets the directory name where SOAP extension will put cache files.
# soap.wsdl_cache_dir="/var/tmp" 
pico /usr/local/php/lib/php.ini
#	Make sure LoadModule points to right module; 
#	PHP make install will add a line like:
#		LoadModule php5_module        modules/libphp5.so
#	which needs to be changed to:
#		LoadModule php5_module          modules/mod_php5-5.2.9.so 
pico /www/conf/httpd.conf

TEST TO MAKE SURE NO ERRORS

/usr/local/php/bin/php-5.2.9 -v

UPDATE SYMBOLIC LINK TO PHP CLI

ls -la /usr/local/bin/php
rm /usr/local/bin/php
ln -s /usr/local/php5/bin/php-5.2.9 /usr/local/bin/php 
ls -la /usr/local/bin/php
rehash
# This should show the latest build version
php -v

RESTART AND CLEAN UP

# Restart Apache
restart_apache
# If apache doesn't restart gracefully, then reboot
reboot
# Delete php build directory
rm -rf /root/php

If you see the following error when restarting apache:

Syntax OK
[Thu Feb 19 16:12:51 2009] [warn] module php5_module is already loaded, skipping

Go back and follow the UPDATE EXTENSIONS section. You'll need to edit the httpd.conf to remove the extra LoadModule line. There should be only one for loading the php extension.

Finally, restart any background processes that rely on PHP. Your command will vary from the one I use, and most likely, you do not need to do this step.

php ctl-scheduler.php restart

Links

Old Note: The option '--with-libexpat-dir=/usr/local/lib' was included to avoid the xml_parse entity handling bug in libxml 2.7.x. prior. The bug was fixed in PHP 5.2.9


Created 2007-06-14, Last Modified 2011-07-24, © Shailesh N. Humbad
Disclaimer: This content is provided as-is. The information may be incorrect.