How to install PHP5 and PHP5 Extensions on FreeBSD?

Note: Article updated for FreeBSD 9

Step 1 – Install FreeBSD

  1. First install FreeBSD. Instructions for installing FreeBSD is contained in one of these article.
    How I install FreeBSD 9?
    How I install FreeBSD? (versions before FreeBSD 9)
  2. Second update FreeBSD and install the ports tree. Instructions for this are in this article.
    What are the first commands I run after installing FreeBSD?

Step 2 – Install PHP

How to install PHP on FreeBSD from Ports

You can install easily from Ports.

  1. Go to the php5 directory in the ports tree.
    # cd /usr/ports/lang/php5
  2. Run ‘make config’ and select the desired options.
    # cd /usr/ports/lang/php5

    Note: If you want to integrate with Apache, you must select the Apache option.

  3. Run ‘make BATCH=yes install’ to install PHP5.
    # make BATCH=yes install

    PHP5 and its dependencies will download, compile, and install automagically for you.

    Note: If you want to script this, including the selecting to use Apache, use this command.

    # make WITH_APACHE=yes BATCH=yes install

How to install PHP on FreeBSD from Packages

  1. You can also install easily as a binary package with this simple command.
    # pkg_add -r php

Step 3 – Installing PHP5 Extensions on FreeBSD

You may now want to install PHP5 extensions for integration with your web server, such as Apache. There is a meta port that allows for installing all PHP5 extensions and we will use this for this example, though later we will demonstrate how to install a single extensions.

How to install php5-extensions from Ports

  1. Change to the php5-extensions ports directory.
    # cd /usr/ports/lang/php5-extensions
  2. Run ‘make config’ and select the php5-extensions you want to install when prompted.
    # make config

    Note 1: If you aren’t sure what to select, use the default, you can always come back and add more extensions later.

    Note 2: It may be important to you to make sure certain extensions are installed, such as extensions for MySQL, MySQLi, or Postgresl, especially if you are installing an Apache, MySQL, PHP server or a variant of such.

    Note 3: The extensions usually link to the release version of MySQL or PostgreSQL unless a new version is already installed. So if you want a later version, you may want to install the desired database port first.

  3. Run ‘make BATCH=yes install’ to install the selected php5-extensions.
    # make BATCH=yes install

    The PHP5-extensions and their dependencies will download, compile, and install automagically for you.

How to install PHP Extensions from Packages

  1. You can also install easily as a binary package with this simple command.
    # pkg_add -r php5-extensions

    Note: The package may not have all the extensions you desire. If not, use ports.

Step 4 – Integrating with Apache 2.x

  1. Change to the apache configuration directory.
    # cd /usr/local/etc/apache22
    
  2. Edit the httpd.conf with your favority edit. I use ‘ee’.
    # ee httpd.conf
  3. Search for “DirectoryIndex” to find the section where the directory index is configured.
  4. Add index.php as the first item as shown:
    DirectoryIndex index.php index.html
  5. Save and close the httpd.conf file.
  6. Change to the “Includes” directory.
    # cd /usr/local/etc/apache22/Includes
  7. Create a file with a name ending in .conf. I used php5.conf.
    # ee php5.conf
  8. Add the following two lines to the file.
    AddType application/x-httpd-php .php
    AddType application/x-httpd-php-source .phps
    
  9. Save and close the php5.conf file.

    Note: You could script this as follows:

    # # echo AddType application/x-httpd-php .php > /usr/local/etc/apache22/Includes/php5.conf echo AddType application/x-httpd-php-source .phps >> /usr/local/etc/apache22/Includes/php5.conf
  10. Restart apache.
    # /usr/local/etc/rc.d/apache22 restart

Step 5 – Test the PHP5 extensions integration with the web server

  1. Go to the web server’s root directory
    # cd /usr/local/www/apache22/data
  2. Create a file called phpinfo.php.
    # ee phpinfo.php
  3. Add the following lines to the file.
    <?php
      phpinfo();
    ?>
    

    Note: the phpinfo() function will automatically create a page full of information about the php configuration and the extensions installed and more.

  4. Save and close the phpinfo.php file.
  5. Now from another machine, browse to the server.

    You can connect using name, fqdn, or IP and see which work.

    • http://servername/phpinfo.php
    • http://www.YourDomain.com/phpinfo.php
    • http://192.168.0.100/phpinfo.php

    You can also try to connect with SSL.

    • https://servername/phpinfo.php
    • https://www.YourDomain.com/phpinfo.php
    • https://192.168.0.100/phpinfo.php
  6. You can now analyze the results of the phpinfo() function on this page.

    Note: Don’t forget to delete this file before putting the server in production. You wouldn’t want such an information disclosure vulnerability available for anyone to see.

Adding or Removing PHP5 extensions

You may find yourself needing to add another PHP5 extension. Maybe you didn’t install the database extensions and you need to add it and you can tell from phpinfo() that it is definitely not installed.

There are two ways to add or remove an extension. First, you could reinstall the meta port (though I wouldn’t recommend it). Second, you can find the specific PHP5 extensions you need and run ‘make install’ or ‘make deinstall’ for that specific port.

Note: Specific PHP5 extensions might take you a moment to locate. The php5-extensions meta port is under /usr/ports/lang but specific ports for extensions are not located there. The MySQLi extension, for example, is found under /usr/ports/databases.

Adding the MySQLi extension

  1. Change to the location of the MySQLi php5 extension.
    # cd /usr/ports/databases/php5-mysqli
  2. Run ‘make BATCH=yes install’ to install the extensions.
    # make BATCH=yes install

    The php5-mysqli extensions downloads and compiles automagically.

Removing the MySQLi extension

  1. Change to the location of the MySQLi php5 extension.
    # cd /usr/ports/databases/php5-mysqli
  2. Run ‘make deinstall’ to uninstall the extensions.
    # make deinstall

    The php5-mysqli extensions is removed.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

  1. Pingback: Installing an Apache + SSL on FreeBSD using the ports tree « Rhyous's 127.0.0.1 or ::1