Installing PHP and PHP extensions on FreeBSD

This article gives short explanations of how to install PHP and PHP extensions on FreeBSD.
For reasons of optimization, PHP will be installed from ports. Optimization may be done by make.conf.
Before you start the installation you must select options of php5 port. The following commands move you to php5 port directory and start configuring the current port options.
cd /usr/ports/lang/php5 make config
The menu with options will appear:
You will probably need to know some of the options to decide which one to select:
- CLI - compiles PHP with a command-line version which allows you to run PHP scripts from the command line;
- CGI - compiles PHP as CGI version, so the command php-cgi will be available in the system;
- APACHE - compiles PHP module for Apache.
After selecting options, the following command will start the installation:
make install clean
When PHP is installed you will probably need additional extensions for PHP. You can install them from port - php5-extensions. The commands below will move you to the port directory and start configuring the current port options.
cd /usr/ports/lang/php5-extensions make config
The menu with options will appear:
Now you have to select the needed options and start the installation using command:
make install clean
All PHP extensions can be also installed manually even if you initially decided not to install them. You can install them from separate ports. Each PHP extension has its own port for installation.
The following command will show a list of paths to each PHP extension port:
find /usr/ports -type d -iname "php5-*"
The result of running the command will be the following:
/usr/ports/archivers/php5-bz2 /usr/ports/archivers/php5-phar /usr/ports/archivers/php5-zip /usr/ports/archivers/php5-zlib /usr/ports/chinese/php5-cconv /usr/ports/converters/php5-bsdconv /usr/ports/converters/php5-iconv /usr/ports/converters/php5-mbstring /usr/ports/converters/php5-recode /usr/ports/databases/php5-creole /usr/ports/databases/php5-dba /usr/ports/databases/php5-interbase /usr/ports/databases/php5-mssql /usr/ports/databases/php5-mysql /usr/ports/databases/php5-mysqli /usr/ports/databases/php5-odbc /usr/ports/databases/php5-pdo /usr/ports/databases/php5-pdo_dblib /usr/ports/databases/php5-pdo_firebird /usr/ports/databases/php5-pdo_mysql /usr/ports/databases/php5-pdo_odbc /usr/ports/databases/php5-pdo_pgsql /usr/ports/databases/php5-pdo_sqlite /usr/ports/databases/php5-pgsql /usr/ports/databases/php5-propel /usr/ports/databases/php5-redis /usr/ports/databases/php5-rrdtool /usr/ports/databases/php5-sqlite /usr/ports/databases/php5-sqlite3 /usr/ports/databases/php5-sybase_ct /usr/ports/devel/php5-blitz /usr/ports/devel/php5-blitz-devel /usr/ports/devel/php5-dav /usr/ports/devel/php5-geshi /usr/ports/devel/php5-gettext /usr/ports/devel/php5-ice /usr/ports/devel/php5-json /usr/ports/devel/php5-pcntl /usr/ports/devel/php5-phing /usr/ports/devel/php5-pinba /usr/ports/devel/php5-readline /usr/ports/devel/php5-shmop /usr/ports/devel/php5-sysvmsg /usr/ports/devel/php5-sysvsem /usr/ports/devel/php5-sysvshm /usr/ports/devel/php5-thrift /usr/ports/devel/php5-tokenizer /usr/ports/ftp/php5-curl /usr/ports/ftp/php5-ftp /usr/ports/graphics/php5-chartdirector /usr/ports/graphics/php5-exif /usr/ports/graphics/php5-ffmpeg /usr/ports/graphics/php5-gd /usr/ports/graphics/php5-swfed /usr/ports/japanese/php5-mecab /usr/ports/lang/php5-extensions /usr/ports/mail/php5-imap /usr/ports/math/php5-bcmath /usr/ports/math/php5-gmp /usr/ports/misc/php5-calendar /usr/ports/net/php5-ldap /usr/ports/net/php5-soap /usr/ports/net/php5-sockets /usr/ports/net/php5-xmlrpc /usr/ports/net-mgmt/php5-snmp /usr/ports/security/php5-filter /usr/ports/security/php5-hash /usr/ports/security/php5-mcrypt /usr/ports/security/php5-openssl /usr/ports/sysutils/php5-fileinfo /usr/ports/sysutils/php5-posix /usr/ports/textproc/php5-ctype /usr/ports/textproc/php5-dom /usr/ports/textproc/php5-pspell /usr/ports/textproc/php5-simplexml /usr/ports/textproc/php5-wddx /usr/ports/textproc/php5-xml /usr/ports/textproc/php5-xmlreader /usr/ports/textproc/php5-xmlwriter /usr/ports/textproc/php5-xsl /usr/ports/www/php5-session /usr/ports/www/php5-tidy
To install the needed PHP extension you will need to go to the port directory and run the installation. For example if you need to install port php5-gd you will need to do the following:
cd /usr/ports/graphics/php5-gd make install clean