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.

comments powered by Disqus