Install PHP 5 + Apache 2 + MySQL 5 on Mac OS X via MacPorts

Posted by Ben Johnson on 6 February 2011 | 0 Comments

Tags: ,,,,,,,,

At Web2Works when developing websites on a local environment. This guide is specifically for Mac, we use MacPorts to make installing and upgrading packages straight forward via the command promt.

On successfully completing the full install with the following:

Let get started

  1. If you haven’t already done so, make sure you turn off Apple’s "Personal Web Sharing" in the System Preferences so that the default Apache server is not running.
  2. Install MacPorts:
    First make sure you install Xcode, a Apple developer toolkit and library that MacPort uses. Follow the installation guide at by downloading the dmg from (make sure you choose the correct OS X) followed by a simple bummies "Next" Install process.
    sudo port selfupdate
    sudo port upgrade outdated
  3. Install Apache 2
    sudo port install apache2

    This may take a while installing the apache module and the dependencies. Time to make a coffee..... After install has completed ensure that apache has been added to the auto load:
    sudo port load apache2
    To verify Apache is now running, point your browser to  http://localhost/. You should see a page that says "It works!"
  4. Virtual Hosts
    Uncomment Virtual Hosts in Apache settings
    This is an example of what my Virtual Host file looks like. With example
    NameVirtualHost *:80

    <VirtualHost *:80>
        ServerAdmin webmaster@localhost
        ServerAlias localhost
        DocumentRoot /Users/john/Sites
        <Directory />
            Options FollowSymLinks
            AllowOverride None
        <Directory /Users/john/Sites>
            Options Indexes FollowSymLinks MultiViews
            AllowOverride All
            Order allow,deny
            allow from all

        ErrorLog /opt/local/apache2/logs/error.log

        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel warn

        CustomLog /opt/local/apache2/logs/access.log combined


    <VirtualHost *:80>
        DocumentRoot "/Users/ben/sites/website_project"
        ServerName website_project.local
        ServerAlias www.website_project.local
        ErrorLog "/opt/local/apache2/logs/website_project-error_log"
        CustomLog "/opt/local/apache2/logs/website_project-access_log" common
  5. Install MySQL
    sudo port install mysql5-server

    Once this has install set up the database:
    sudo -u mysql mysql_install_db5
    sudo chown -R mysql:mysql /opt/local/var/db/mysql5/
    sudo chown -R mysql:mysql /opt/local/var/run/mysql5/
    sudo chown -R mysql:mysql /opt/local/var/log/mysql5/

    Activate your MySQL server installation so that it autostarts when you boot your machine:
    sudo port load mysql5-server

    You can verify it is running by:
    ps -ax | grep mysql

    Set the MySQL root password, you will be prompted for your existing password ("Enter password:"); since it’s empty, just press Return:
    mysqladmin5 -u root -p password <new-password>

    Test everything by logging in to the server.
    mysql5 -u root -p

    Run the interactiev program to secure the MySQL server.
  6. Install PHP
    Next install the latest PHP and Apache
    sudo port install php5 +apache2 +pear
    sudo port install php5-mysql

    Register PHP with Apache:
    cd /opt/local/apache2/modules
    sudo /opt/local/apache2/bin/apxs -a -e -n "php5"

    Update Apache’s httpd.conf file to enhance the "DirectoryIndex" directive to include additional "index" files. Search for:
    DirectoryIndex index.php index.html

    Also, at the end of the httpd.conf file, add the following lines so that Apache includes the mod_php "AddType" configurations
    # Include PHP configurations
    Include conf/extra/mod_php.conf

    Uncomment Virtual hosts
    Include conf/extra/httpd-vhosts.conf

    Set up your PHP configuration files:
    cd /opt/local/etc/php5
    sudo cp php.ini-development php.ini
  7. Configure PHP.ini
    set mysql.default_socket=/opt/local/var/run/mysql5/mysqld.sock

    date.timezone = Europe/London
    date.default_latitude = 53.826597
    date.default_longitude = -1.592889
  8. Stopping and Starting Apache
    Adding aliases to your shell environment will make it easier to start and stop Apache. Edit your .profile or .bash_profile:
    alias apache2ctl='sudo /opt/local/apache2/bin/apachectl'
    This will mean to restart apache you only have to type: apache2ctl restart
    Run the following to refresh the profile.
    . ~/.profile

    Or try:
    sudo /opt/local/etc/LaunchDaemons/org.macports.apache2/apache2.wrapper start
  9. Install phpMyAdmin
    sudo port install phpmyadmin

    Add the follow line to the apache htpd.conf file
    # Local access to phpmyadmin installation
    Include conf/extra/httpd-phpmyadmin.conf

    Then create this file in /opt/local/apache2/conf/extra/httpd-phpmyadmin.conf containing this text:

    AliasMatch ^/phpmyadmin(?:/)?(/.*)?$ "/opt/local/www/phpmyadmin$1"

    <Directory "/opt/local/www/phpmyadmin">
    Options -Indexes
    AllowOverride None
    Order allow,deny
    Allow from all
    LanguagePriority en de es fr ja ko pt-br ru
    ForceLanguagePriority Prefer Fallback

    Finally, you need to set up the phpMyAdmin configuration to access mySQL.
    cd /opt/local/www/phpmyadmin/
    sudo cp

    $cfg['Servers'][$i]['auth_type'] = 'config'; // Authentication method (config, http or cookie based)?
    $cfg['Servers'][$i]['user'] = 'root'; // MySQL user
    $cfg['Servers'][$i]['password'] = ''; // MySQL password (only needed with 'config' auth_type)

And that is it one final Restart Apache so that your changes take effect,  a painless server install/upgrade.