Apache 2.2, MySQL 5.0 and PHP 5.1 on FreeBSD 6.1

Way back then, I wrote a tutorial on how to set up a FAMP server. Some things have changed since then, and this tutorial gives a quick run down on how to do it today. I have tried to explain the whys as well as giving the steps. Installing FreeBSD is beyond the scope of this tutorial. So let’s wave our magic wand and say that your server has just rebooted into a fresh minimal installation of FreeBSD 6.1.

There are three ways to install software on a FreeBSD system. You can do everything from ‘first principle’: download source code, compile and install. The second way is to use pkg_add to install pre-built binary packages. pkg_add is able to resolve dependencies and it will automatically download and install these as well.

The third way is to use Ports. The Ports Collection is a set of makefiles, patches and description files placed in /usr/ports. To install a port, you just need to browse to the relevant subdirectory and type ‘make install’ For example:

cd /usr/ports/editors/vim
make install

This will tell the Ports System to download the vim source code, compile and install it on your system. You are given the chance to choose from a set of compile time options if there are any. The Ports System is able to resolve dependencies too.

We will be installing the required software from ports because it gives us both flexibility (we are able to select compile time options) and convenience (it resolves dependencies and fetches the source code for us). Software installed from Ports can also be upgraded easily.

Let’s use Portsnap to make sure that we have the latest snapshot of the Ports Collection. Portsnap is a system for securely distributing the Ports tree. On a brand new installation of FreeBSD 6.1, as root:

portsnap fetch
portsnap extract

You now are the proud owner of an up to date Ports Collection. From now on, if you want to update your Ports tree, just do:

portsnap fetch update

More information on Portsnap can be found in the relevant section of the excellent FreeBSD Handbook.

You can use cvsup as an alternative to Portsnap. However, Portsnap snapshots are cryptographically signed and the system uses less bandwidth. You can use pkg_version to check whether you have any outdated ports on your system:

pkg_version -v

To update all the outdated ports with one command:

portupgrade –a

portupgrade –ai will ask for confirmation before updating each port. If you don’t have portupgrade installed, you can install it by doing:

cd /usr/ports/sysutils/portupgrade
make install

It is also a good idea to do

make clean

This gets rid of the working subdirectory that contains the temporary files during compilation. This can be shortened to ‘cd /usr/ports/sysutils/portupgrade; make install clean’. More information on how you can keep your system up to date can be found here. Now that we have an updated Ports Collection, let’s get down to business.

Install Apache:

cd /usr/ports/www/apache22
make install clean

Install MySQL:

cd /usr/ports/database/mysql50-server
make install clean

Configure the php5 port:

cd /usr/ports/lang/php5
make config

Deselect option CLI. Selecting to build the CLI version rendered php apache module unsuable for me for some reason. A quick googling around told me that I was not alone in having this problem.

Install PHP:

make install clean

Install PHP extensions:

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

Just choose what extensions you want to install. Install the php5-mysql extension:

cd /usr/ports/databases/php5-mysql
make install clean

Let’s start our new daemons:

/usr/local/etc/rc.d/apache22.sh start
/usr/local/etc/rc.d/mysql-server start

You should now be able to browse to your web address and see the cheerful exclamation: ‘It works!’ To make sure that Apache and MySQL start whenever the server is rebooted, add these lines to /etc/rc.conf:


Software installed through ports will have their configuration files in /usr/local/etc. The base system configuration files live in /etc. This makes for a clean, consistent filesystem layout. Let’s configure our newly installed software.

To make Apache PHP-aware, add the following lines to /usr/local/etc/apache22/httpd.conf:

AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps

Go through the configuration file and customise to your needs. The PHP port has created a php.ini-recommended file for us in /usr/local/etc/. Let’s use that as a starting point:

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

Now edit /usr/local/etc/php.ini to suit your needs. Restart Apache to apply the configuration changes:

apachectl graceful

By default, no password is set for the MySQL root user. You should set a password for that user at this point:

mysqladmin -u root password "newpassword"

Replace “newpassword” with your own. MySQL comes with two anonymous accounts set up. These accounts don’t have any passwords assigned by default. It is a good idea to just delete them. Use the MySQL client that came with the database server:

mysql –u root -p

When asked, type in your newly created database root password. At the MySQL prompt, issue the following command:

delete from mysql.user where user = '';
flush privileges;

To exit from the MySQL client, just type ‘exit’.

That’s it! I now suggest that you take a look at the output of phpinfo() to make sure that everything is in order.

12 Comments. Leave a comment or send a Trackback.
  1. #1 • Keith Smith said on December 21 2006:

    EXCELLENT GUIDE! I FINALLY got my bsd/apache/php online! thanks again!

  2. #2 • Dave Moran said on December 29 2006:

    Thank you for this. The last time I tried to set up FAMP I was obscenely unsuccessful. With your guide, it was a snap.

  3. #3 • rob said on January 26 2007:

    Followed your guide exactly through… apache22.sh was not installed, apachectl graceful works, found a work around and copied some apache22.sh.in file to apache22.sh; PHP is not working, i’ve added mime types everywhere, even AddHandler as a .htaccess file…

  4. #4 • David said on April 3 2007:

    I was following your instructions but when i try to install mysql i have gotten this error:

    slave.cc: In function `int fetch_master_table(THD*, const char*, const char*, MA
    STER_INFO*, MYSQL*, bool)’:
    slave.cc:16: internal compiler error: in cp_expand_stmt, at cc1plus/../../../../ contrib/gcc/cp/semantics.c:2761
    Please submit a full bug report,
    with preprocessed source if appropriate.
    See for instructions.
    *** Error code 1
    Stop in /usr/ports/databases/mysql50-server/work/mysql-5.0.27/sql.
    *** Error code 1

    please help

  5. #5 • David said on May 5 2007:

    I’m again, so i got to insall the famp server, that error was because i have some trouble’s while i was updateing my freebsd release, so when i install this release as new installation it work’s at the first

  6. #6 • Shane said on May 6 2007:

    David, glad to hear that you got it working.

  7. #7 • hiutopor said on September 18 2007:


    Very interesting information! Thanks!


  8. #8 • John said on January 27 2008:

    Excellent! This blog gave me just what I needed. It’s clean, concise, and informative. Thank you very much!

  9. #9 • LA said on April 17 2008:

    I install Apache 2.2.8, mysql server 5X, php 5*
    apache php mysql work but I don’t see with phpinfo() that mysql are working (exist only comand at comand line).
    Must modiy httpd.comf or php.ini to see in phpinfo() ?


  10. #10 • Noel said on April 18 2008:

    Just a heads up. I spent AGES (3 days) trying to gete PHP working on freeBSD 7, turns out the problem was just that short tags (php.ini) were set to OFF. So try using instead of and it might work.

  11. #11 • Romano said on June 14 2008:

    Hi all
    this is a very good guide!
    it’s the first time some instructions re ports made sense to me.
    I still got a problem to get my latest FreeBSD box to work.
    Under 7.0 I try to get PHP 5 to work…
    I managed to get portsnap to work find.
    When I run portupgrade -a thing come to a hold.
    I never get PHP 5 to download.
    Anybody any hints?

  12. #12 • mike lypp said on August 7 2008:

    Nicely done, pretty much my hero. Free BSD is great… when it’s configured properly! Thank you very much!