This post will describe how to install and configure Apache, MySQL, PHP and phpMyAdmin on FreeBSD for basic local web development. Once set up, you’ll be able to use your “AMP” server to do web development, code testing, maintain local copies of your web sites, etc.
The software discussed in this post are available as free and open source under various licensing structures. The versions of software discussed in this post are as follows:
The following steps discussed in this post assume you have the FreeBSD Ports Collection installed. If not, you can install it by using the following commands:
portsnap fetch portsnap extract
If the Ports Collection is already installed, make sure to update it:
portsnap fetch update
Okay, let’s get started. All commands are issued as the root user or by simulating the root user by using the command su. While building the various ports you should accept all default configuration options unless otherwise instructed.
Navigate to the Apache server port and build it:
cd /usr/ports/www/apache22 make install clean
Once Apache has been successfully installed, add the following line to /etc/rc.conf so that the Apache server will start automatically at system boot.
echo 'apache22_enable="YES"' >> /etc/rc.conf
Now let’s start Apache to make sure it works:
Point your web browser to the host name or IP address of the FreeBSD host you’ve installed Apache on and you should see the venerable “It Works.”
Now let’s build the MySQL server:
cd /usr/ports/databases/mysql55-server make install clean
Add the following line to /etc/rc.conf:
echo 'mysql_enable="YES"' >> /etc/rc.conf [/sourcecode language="bash"] And start the mysql server: /usr/local/etc/rc.d/mysql-server start [/sourcecode language="bash"] Then set a password for the MySQL root user: /usr/local/bin/mysqladmin -u root password 'your-password'
Next, we’ll build PHP. Before we proceed, however, we need to add a configuration option so that PHP build includes support for the Apache server. Start with the following commands:
cd /usr/ports/lang/php5 make config
A menu should come up allowing you to select/deselect various build options. You should select “Build Apache module” by highlighting the option with the arrow keys and hitting the space bar, then hit Enter, which should bring you back to the command prompt. Now proceed with building the port:
make install clean
Now let’s add the requisite extensions to PHP to round out its capabilities. Before we build this port though we’ll want to add support for both MySQL and MySQLi (an improved interface to MySQL) in order to communicate with the MySQL server.
cd /usr/ports/lang/php5-extensions/ make config
In the corresponding menu you should select “MySQL database support” and “MySQLi database support,” then proceed with building the port:
make install clean
phpMyAdmin is a free software tool written in PHP intended to handle the administration of MySQL from your web browser. phpMyAdmin supports a wide range of operations with MySQL, including managing databases, tables, fields, relations, indexes, users, permissions, etc., from an easy-to-use web page, while you still have the ability to directly execute any SQL statement from the command line if you prefer. Installing phpMyAdmin is optional but it’s nice tool to have. Here again we’ll want to add support for MySQL and MySQLi before building the port:
cd /usr/ports/database/phpmyadmin/ make config
Here you should ensure that both “MYSQL M(DB_connect): PHP MySQL support via mysql client” and “MYSQLI M(DB_connect) PHP Improved MySQL client support” are selected, then proceed with building the port:
make install clean
Now that we have the requisite ports built and installed it’s time to configure them. First, let’s create the file /usr/local/etc/php.ini to hold our PHP options. The simpliest way to do this is to copy the file /usr/local/etc/php.ini-development which will add the default settings for new PHP installations. This configuration is suitable for development purposes, but NOT necessarily for production purposes. If your plans include a production server, then among other things, and before going online with your site, you should consider copying /usr/local/etc/php.ini-production instead and consult the recommendations at http://php.net/manual/en/security.php.
cp /usr/local/etc/php.ini-development /usr/local/etc/php.ini
Now let’s configure Apache. Open the file /usr/local/etc/apache22/httpd.conf in your favorite editor and look for the following line:
And change it so it reads as follows:
DirectoryIndex index.html index.htm index.php
Then append the following lines to the end of the file in order to support PHP files as well as phpMyAdmin, which normally lives outside of the Apache document root. Note: if you elected not to install phpMyAdmin, then you need only add the two AddType lines:
AddType application/x-httpd-php .php AddType application/x-httpd-php-source .phps Alias /phpmyadmin "/usr/local/www/phpMyAdmin" Directory "/usr/local/www/phpMyAdmin"&; Options None AllowOverride None Order allow,deny Allow from all
As an optional step, if you’d like to add multilanguage support to Apache, uncomment the following line:
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:
Now restart Apache:
That’s it for our Apache configuration. Now let’s configure phpMyAdmin. We’ll do this by creating the file /usr/local/www/phpMyAdmin/config.inc.php, the basic configuration file for phpMyAdmin. Traditionally, users have manually created config.inc.php, but now phpMyAdmin includes a nice setup script, making it much easier to create this file with the settings you want. Start by creating the directory /usr/local/www/phpMyAdmin/config and make it writable by phpMyAdmin:
mkdir /usr/local/www/phpMyAdmin/config && chmod o+w /usr/local/www/phpMyAdmin/config
Then open your web browser and navigate to http://your-hostname-or-IP-address/phpmyadmin/setup where you will see the phpMyAdmin setup Overview page. Select “New server” and then select the “Authentication” tab. Under the “Authentication type” choose “http” from the drop-down list (using HTTP-Auth to sign-in into phpMyAdmin will avoid storing login/password credentials directly in config.inc.php) and remove “root” from the “User for config auth”(See Figure 1).
Screenshot of the phpMyAdmin setup page
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 make use of it.
mv /usr/local/www/phpMyAdmin/config/config.inc.php /usr/local/www/phpMyAdmin
Now let’s try out phpMyAdmin to make sure it works. Point your web browser to http://your-hostname-or-IP-address/phpmyadmin where you will be presented with a pop-up box requesting you to log in. Use “root” and the MySQL password you set up previously, then you should be directed to the phpMyAdmin administration page. We no longer need the /usr/local/www/phpMyAdmin/config directory so let’s remove it and wrap up by restarting the Apache and MySQL servers:
rm -r /usr/local/www/phpMyAdmin/config /usr/local/etc/rc.d/apache22 restart /usr/local/etc/rc.d/mysql-server restart