How to Install Apache, MySQL, PHP, and phpMyAdmin on FreeBSD

Introduction

I like having a good – stable – system so FreeBSD is really great for hosting purposes. Recently I moved from Ubuntu Server to BSD and had to build an entire new setup on FreeBSD. Here a small tutorial on how to achieve this.

Requirements

  • FreeBSD distribution – I’m using 9.1 (server/desktop/VPS)
  • Basic Unix knowledge

Installation

If you have no FreeBSD ports collection installed please install this first

1
2
portsnap fetch
portsnap extract

If you have the ports collection just make sure to update:

1
portsnap fetch update

Apache

Let’s start with Apache (you can also use nginx but I’m familiair with Apache so decide it yourself. Navigate to the Apache port and build it:

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

Add the following to make sure Apache will be started when the system will boot up:

1
echo 'apache22_enable="YES"' >> /etc/rc.conf

Start Apache:

1
/usr/local/etc/rc.d/apache22 start

Test Apache by pointing your web browser to the IP-address/hostname of your FreeBSD-system. It should display:

1
"It works"

MySQL

Now we will install MySQL server:

1
2
cd /usr/ports/databases/mysql55-server
make install clean

Add the following line to /etc/rc.conf:

1
echo 'mysql_enable="YES"' >> /etc/rc.conf

Start the MySQL server:

1
/usr/local/etc/rc.d/mysql-server start

Set a password for the MySQL root user:

1
/usr/local/bin/mysqladmin -u root password 'enter-your-password'

PHP

Now we will build PHP, but before we can continue building we have to config:

1
2
cd /usr/ports/lang/php5
make config

A screen will pop-up, there you can choose the “Build Apache module” by hitting the spacebar and continue building:

1
make install clean

Just PHP however is not enough – there will be some extensions needed too – so again we have to config:

1
2
cd /usr/ports/lang/php5-extensions/
make config

Choose the “MySQL database support” and “MySQLi database support” and continue building:

1
make install clean

phpMyAdmin

Be sure to consider whether you’re sure you want to use phpMyAdmin or not – also consider reading the security recommendations: http://php.net/manual/en/security.php. It’s really NOT necessary but will make this easy for you.

Let’s continue:

1
cp /usr/local/etc/php.ini-development /usr/local/etc/php.ini

Open and edit /usr/local/etc/apache22/httpd.conf (I’m using gedit). Look for the following line and change it into:

1
2
DirectoryIndex index.html ->
DirectoryIndex index.html index.htm index.php

Now add these lines to the end of the /usr/local/etc/apache22/httpd.conf file:

1
2
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
1
Alias /phpmyadmin "/usr/local/www/phpMyAdmin"
1
2
3
4
5
6
<Directory "/usr/local/www/phpMyAdmin">
Options None
AllowOverride None
Order allow,deny
Allow from all
</Directory>

Please note, if you don’t want to use phpMyAdmin then make sure you only add line 1 & 2 to the end of the file (AddType…. & AddType….).

If you would like multilanguage support to Apache you can uncomment the following line:

1
Include etc/apache22/extra/httpd-languages.conf

Then open the language settings file /usr/local/etc/apache22/extra/httpd-languages.conf and add the following line to the end of the file:

1
AddDefaultCharset On

Now restart Apache:

1
/usr/local/etc/rc.d/apache22 restart

We have to configure the phpMyAdmin part a bit further now. Let’s create a directory and make it writable with the correct permissions:

1
mkdir /usr/local/www/phpMyAdmin/config && chmod o+w /usr/local/www/phpMyAdmin/config

Open a web browser again and enter: http://IP-address/phpmyadmin/setup (or hostname of course) – there will be a config page for phpMyAdmin.

Select “New server” and then select the “Authentication” tab. Under the “Authentication type” choose “http” from the drop-down list (this will avoid saving login/password credentials in config.inc.php) and remove “root” from the “User for config auth”.

Now select “Save” and you will be returned you to the Overview page where you should see a new server listed. Select “Save” again in the Overview page to save your configuration as /usr/local/www/phpMyAdmin/config/config.inc.php. Now let’s move that file to /usr/local/www/phpMyAdmin where phpMyAdmin can use of it to run:

1
mv /usr/local/www/phpMyAdmin/config/config.inc.php /usr/local/www/phpMyAdmin

Test if the environment is configured correctly and point your browser again to http://IP-address/phpmyadmin (or hostname) and use “root” as username with the password you’ve set before. Does it work? Great – let’s clear some things:

1
2
3
rm -r /usr/local/www/phpMyAdmin/config
/usr/local/etc/rc.d/apache22 restart
/usr/local/etc/rc.d/mysql-server restart

Wordpress

WordPress is a blogging-platform that makes (heavy) use of Apache, MySQL and PHP. We will install it on our newly created implementation to ensure we have these packages installed and working correctly.

Let’s start by downloading the latest WordPress directly from the developers site to your home directory and unpacking the package:

1
2
fetch http://wordpress.org/latest.tar.gz
tar -zxvf latest.tar.gz

You should now see the new directory wordpress in your home directory. Next we need to create the file ~/wordpress/wp-config.php and make some changes to it so WordPress can access the MySQL server. First, let’s copy the file ~/wordpress/wp-config-sample.php to be used as the template:

1
cp ~/wordpress/wp-config-sample.php ~/wordpress/wp-config.php

Open ~/wordpress/wp-config.php in gedit and enter the database name as well as your MySQL login (root) and password in the appropriate lines. When complete, it should look like the following:

1
2
3
4
5
6
7
// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define('DB_NAME', 'wordpress');
/** MySQL database username */
define('DB_USER', 'root');
/** MySQL database password */
define('DB_PASSWORD', 'password');

Now move the wordpress directory to Apache’s documentroot:

1
mv ~/wordpress/ /usr/local/www/apache22/data/

Let’s create a MySQL-DB for our WordPress installation. Open phpMyAdmin in your browser and create a database by selecting the “Databases” tab at the top. Enter a name for it in the “Create a new database” field. Enter “wordpress” as the database name then select “Create.”

Now let’s run the WordPress script that will populate its database with the requisite tables. Open your web browser and navigate to http://IP-address/wordpress/wp-admin/install.php (or hostname once again).

If everything is configured correctly you should see the WordPress installation wizard page. Enter a title for your site, username, password, and an e-mail, then select “Install WordPress.” You should see this:

Comments