This page was last modified: July 27 2006 16:21:03   
Too Cool for Internet Explorer

Installing PHP v5

Even if you don't know how to do programming in PHP, you will almost certainly need to install it. Hundreds of tools written in PHP is available, and I'm sure that you'll want to install a few of them some time in the near future.

If you choose to install support for MySQL, you should install MySQL first if you want version 5 - If MySQL is not installed, PHP will install the mysql 4.1 client.

If Apache has not been installed yet, it will be included automatically. For that reason I always install Apache first, to be sure that my preferred version of apache is installed.

This means that I install in this order: apache -> mysql -> php

This article assumes that you are using Apache version 2.x

Installing PHP

If you plan on installing PHP 5, you must first be sure that it is supported by any php-based tool you may wish to install like phpadmin or squirrelmail. Also If you already have websites programmed under PHP 4.x, checkout the differences between version 4 and 5. I should mention that I have not encountered any problems with webbased tools, or any of my own pages written in php, when switching to PHP 5.

This guide covers the installation of PHP 5.

cd /usr/ports/lang/php5/
make WITH_APACHE2=yes install clean distclean

......................................................................
.                     Options for php5 5.0.5_1                       .
. .................................................................. .
. .      [X] APACHE2  Use apache 2.x instead of apache 1.3.x       . .
. .      [ ] DEBUG    Enable debug                                 . .
. .      [ ] IPV6     Enable ipv6 support                          . .
. .      [X] OPENSSL  Build static OpenSSL extension               . .
. .                                                                . .
. .                                                                . .
. .                                                                . .
. .                                                                . .
. .                                                                . .
. .                                                                . .
. .                                                                . .
. .                                                                . .
. .                                                                . .
. .                                                                . .
. .                                                                . .
......................................................................
.                       [  OK  ]       Cancel                        .
......................................................................

Remember to select APACHE2 if you have installed version 2 of Apache

You move arround with arrow-up and arrow-down. Use space to X-mark the items you wan't. When finished use tab to goto OK and press enter to finish.

Next thing to do, is choosing which extensions you want for PHP. If you are not sure which to choose, you should at least install those selected by default. The beauty about this is that you can always come back later and install more extentions. The below list shows what I have initially chosen:

cd /usr/ports/lang/php5-extensions
make install clean distclean

[X] CALENDAR  calendar conversion support
[X] CTYPE  ctype functions
[X] FTP  FTP support
[X] GD  GD library support
[X] GETTEXT  gettext library support
[X] MING   ming shockwave flash support
[X] MYSQL  MySQL database support
[X] OPENSSL  OpenSSL support
[X] OVERLOAD  user-space object overloading support
[X] PCRE  Perl Compatible Regular Expression support
[X] PDF  PDFlib support (implies GD)
[X] PGSQL  PostgreSQL database support
[X] POSIX  POSIX-like functions
[X] SESSION  session support
[X] TOKENIZER  tokenizer support
[X] XML   XML support
[X] ZIP  ZIP support
[X] ZLIB  ZLIB support

If you have included GD library support, you will be prompted for GD options:

[X] T1LIB  Include T1lib support
[X] TRUETYPE  Enable TrueType string function
[ ] JIS  Enable JIS-mapped Japanese font support

PHP Configuration file

PHP has a lot of options to play with. The file is located in /usr/local/etc/

cd /usr/local/etc/
cp php.ini-recommended php.ini

Make a copy of the php recommended initialization file as shown above. From now on you'll make any changes in the php.ini file. Chances are that you don't need to change anything right now, but you may want to make changes once you have started programming.

PHP settings in Apache's configuration file
Note that any of the settings in php.ini can also be set in your apache configuration file. This is very handy when you want to have different values for different websites. You do this with the php_flag and php_value directives. The php_flag directive is used when setting boolean values (settings that can only have two values, for example 'On' and 'Off'). The php_value directive is used for all other settings. In the next example you'll see how to do this.

Error logging
From PHP version 5, you can log errors to a file. This is very practical since showing errors directly on the website is ugly and can be a security risk.

Here's an example of how to log php errors to a file:

Create a directory and make www the owner:

mkdir /var/log/php
chown www:www /var/log/php

Open php.ini and set these values:

log_errors = On
display_errors = Off
error_log = /var/log/php/php-error.log

You don't need to create the log file. It will be created automatically the first time php encounters an error.

Remember to restart Apache to make changes in php.ini take effect.

But what if you don't want to log errors from www.example1.tld in the same file as www.example2.tld... or maybe you want to show errors on one site, but not on another...no problem. As mentioned earlier you can add php settings in Apaches configuration file.:

</VirtualHost *:80>
[.. other settings ..]
php_flag log_errors On
php_flag display_errors Off
php_value error_log /path/to/error.log
[.. other settings ..]
</VirtualHost>

How beautiful is that!

PHP Configuration of Apache

For PHP to work, Apache has to know about it.:

Go to usr/local/etc/apache2 and open httpd.conf. Ensure that these two lines exist somewhere in the file:

LoadModule php5_module libexec/apache2/libphp5.so AddType application/x-httpd-php .php

If you want Apache to recognize other file extensions as php, you can seperate them with a space in the AddType line:

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

If you haven't yet made any other changes to httpd.conf, than the above, you can proceed with Configuration of Apache