Avenging Angel

Apache/MySQL/PHP/phpMyAdmin on FreeBSD

  1. Introduction:

  2. This article describes how to setup Apache, MySQL, PHP and phpMyAdmin on a server running FreeBSD. The article was written for the software versions below but is likely to work on newer versions without too much difficulty.

  3. Software:

  4. Operating System:FreeBSD 7.0 for i386Download
    Apache:2.2.8Installed from Ports Collection
    MySQL Server:5.0.51aInstalled from Ports Collection
    PHP & Extensions:5.2.5Installed from Ports Collection
    phpMyAdmin:2.11.5Installed from Ports Collection

  5. Before you begin:

    1. This article assumes you have a working install of FreeBSD 6.2 for i386 logged in as root (or another user in the wheel group and you have used "su") with the ports collection installed. You can use sysinstall, cvsup or portsnap to install the ports distribution if you have not already done so. See point 2 below.
    2. Update your ports collection (portsnap fetch, portsnap extract.) (See the FreeBSD Handbook Section 4.5.1)
  6. Installing MySQL:

    1. Go to the mysql50-server port directory by typing the command:
      cd /usr/ports/databases/mysql50-server
      
    2. Build the port by typing: (This takes AGES - good time for some food.)
      make BUILD_OPTIMIZED=yes BUILD_STATIC=yes
      
    3. Install by typing:
      make install clean
      
    4. Open /etc/rc.conf with your favourite text editor and add the line shown below. This will ensure mysql is enabled and starts on boot.
      mysql_enable="YES"
      
    5. Start mysql manually to avoid having to reboot now by typing:
      /usr/local/etc/rc.d/mysql-server start
      
    6. Set a password for the MySQL root user by executing the command, subtituting your own password in place of new-password:
      /usr/local/bin/mysqladmin -uroot password 'new-password'
      
      And you're done! MySQL is installed.
  7. Installing Apache

    1. Go to the apache22 port directory by typing the command:
      cd /usr/ports/www/apache22
      
    2. Build and install the port by typing: (This takes a while, coffee time!)
      make install clean 
      
      Note: You may want to disable the two DAV options if you don't need them when prompted

    3. Open /etc/rc.conf with your favourite text editor and add the line shown below. This will ensure apache is enabled and starts on boot.
      apache22_enable="YES"
      
  8. Installing PHP

    1. Go to the php5 port directory by typing the command:
      cd /usr/ports/lang/php5
      
    2. Build and install the port by typing: (Just accept the default options, this takes a while, more coffee.)
      make install clean
      
      Make sure the APACHE (Build Apache module) option is ticked when configuring the build, leaving all other options as default, before selecting OK.

    3. Go to the php5-extentions meta port directory by typing the command:
      cd /usr/ports/lang/php5-extentions
      
    4. Build and install the port by typing: (Just accept the defaults here, phpMyAdmin will install any other extensions required itself)
      make install clean
      
    5. Install the php.ini file:
      cp /usr/local/etc/php.ini-dist /usr/local/etc/php.ini
      

    6. Edit your Apache configuration file (/usr/local/etc/apache22/httpd.conf) and add the following lines to the end of the file:
      AddType application/x-httpd-php .php
      AddType application/x-httpd-php-source .phps
      
      Note: You can add additional extenions other than .php (eg .phtml) seperated by spaces.
      Note: The second line is optional, it will show colour highlighted PHP source for .phps files.

    7. You should also search for the line that reads:
      DirectoryIndex index.html
      
      and change it to read:
      DirectoryIndex index.php index.html
      
      Note: You may also add index.phtml or any other default page if you added additional extentions in the previous step.

    8. Enable language settings by searching for the line:
      #Include etc/apache22/extra/httpd-languages.conf
      
      and removing the # comment mark so it reads:
      Include etc/apache22/extra/httpd-languages.conf
      
    9. Edit the language settings file (/usr/local/etc/apache22/extra/httpd-languages.conf) and add the following line at the end of the file:
      AddDefaultCharset On
      
    10. Start Apache using the startup script:
      /usr/local/etc/rc.d/apache22 start
      
      And you're done! Apache with PHP is installed.
  9. Installing phpMyAdmin

    1. Got to the phpmyadmin port directory by typing the command:
      cd /usr/ports/databases/phpmyadmin
      
    2. Build and install the port by typing: (Just accept the default options)
      make install clean
      
    3. phpMyAdmin is now installed in /usr/local/www/phpMyAdmin. To use it we need to create Alias and Directory entries in /usr/local/etc/apache22/httpd.conf. To do this, add the following lines to the <IfModule alias_module> section (just search for where all the other Alias commands are.)
      Alias /phpmyadmin /usr/local/www/phpMyAdmin
      
    4. As /usr/local/www/phpMyadmin is outside of the Apache <DocumentRoot> you will have to make a <Directory> entry for it too. Add the following lines to the end of the <Directory> section: (just search for </Directory>)
      <Directory "/usr/local/www/phpMyAdmin">
              Order allow,deny
              Allow from all
      </Directory>
      
      Note:You may prefer to put the Alias entry inside a <VirtualHost> entry if you are hosting multiple sites using name based virtual hosting and do not wish to enable phpMyAdmin on all the sites.

    5. Create a config directory for phpMyAdmin and make it globally read/write/executable by typing the commands:
      cd /usr/local/www/phpMyAdmin
      mkdir config
      chmod 777 config
      
    6. Restart Apache so that the Alias and Directory entries take effect by typing:
      /usr/local/etc/rc.d/apache22 restart
      
    7. Configure phpMyAdmin by going to http://hostname/phpmyadmin/scripts/setup.php in your browser and set at least the following:
      • Add Server
        • Change the "Authentication type" dropdown to http to have phpMyAdmin prompt you for a username and password.
        • Delete root from the "User for config" auth textbox so it is blank.
        • You can leave all other settings as they are (even if they are blank.)
        • Click the "Add" button to add the new server
      Save the configuration using the Save button in the Configuration section.

    8. For the changes to take effect you must copy the generated config file from the phpMyAdmin/config directory to the phpMyAdmin directory by typing the following command: (Note the space dot at the end of the command)
      cp config/config.inc.php .
      
    9. You can now delete the config directory you created earlier and reset the permissions on the config.inc.php file to read only typing the commands:
      rm -rf config
      chmod 444 config.inc.php
      
    10. That's it! You can place your web site in /usr/local/www/data/ and access phpMyAdmin at http://hostname/phpmyadmin/ in your web browser and logging in using username root and the MySQL password you set earlier.
  10. Addendum

    1. Document Version: 2.7.0
    2. Changes:
      • 2.7.0
        • Updated for FreeBSD 7.0, MySQL 5.0.51a and phpMyAdmin 2.11.5.
      • 2.6.0
        • Updated for Apache 2.2.8, MySQL 5.0.51, PHP 5.2.5 and phpMyAdmin 2.11.4.
        • Removed register_globals reminder, nobody should be using this anymore.
        • Updated Apache rc.d script name.
        • Updated phpMyAdmin config procedure.
      • 2.5.0
        • Updated for Apache 2.2.6, PHP 5.2.4 & phpMyadmin 2.11.1.
        • Some formatting updates.
      • 2.4.0
        • Updated for FreeBSD 6.2, Apache 2.2.4, MySQL 5.0.45 PHP 5.2.3 & phpMyAdmin 2.10.3
      • 2.3.0:
        • Clarified "Build Apache Module" option for php build
        • Updated for phpMyAdmin 2.8.2
      • 2.2.0:
        • Updated for FreeBSD 6.1, Apache 2.2.2, MySQL 5.0.22, PHP 5.1.4, phpMyAdmin 2.8.1
      • 2.1.0:
        • Updated for Apache 2.2.0, MySQL 5.0.19, PHP 5.1.2, phpMyAdmin 2.8.0.2
        • Changed install procedure for phpMyAdmin to install from Ports Collection rather than source tarball.
        • Included link to appropriate section in the FreeBSD Handbook for obtaining/updating the Ports Collection.
      • 2.0.0:
        • Updated for FreeBSD 6.0, Apache 2.0.55, MySQL 5.0.16, PHP 5.1.1 and phpMyAdmin 2.7.0-pl1.
        • Changed procedure to install from Ports Collection rather than build from source tarballs.
      • 1.1.0:
        • Removed instructions for installing the FreeBSD 4.x compatibility package as this is no longer required for MySQL.
        • Updated for FreeBSD 5.4, MySQL 4.1.13 and phpMyAdmin 2.6.3-pl1.
        • Fixed a couple of grammatical errors.
        • Fixed mistake in command line to make symbolic link for MySQL install.
    3. References: