Version 2 (modified by jaguarcy+macports@…, 14 years ago) (diff)


<- Back to the HOWTO section

Getting Apache, MySQL, and PHP Running under MacPorts

  • Audience: Users who don't like the default OS X installation
  • Requires: MacPorts


Some networks and proxies prevent or break the use of rsync or svn via http. For people on these networks, getting an updated port tree can be difficult. The rsync server makes a tarball daily and stores it on

These instructions are written for the following versions:

  • Apache 2.2.*
  • MySQL 5.0.*
  • PHP 5.2.*


Step 1: Turn off Apple's "Personal Web Sharing"

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. While you can set things up so that both servers run on different ports (e.g., port 80 and port 8080), it's not worth the hassle of maintaining two web server daemons. Just use the latest Apache from MacPorts and be done with it.

Step 2: Install Apache

sudo port install apache2

Create an initial httpd.conf configuration file:

cd /opt/local/apache2/conf
sudo cp httpd.conf.sample httpd.conf

Activate your Apache installation so that it autostarts when you boot your machine:

sudo launchctl load -w /Library/LaunchDaemons/org.macports.apache2.plist

Start Apache (the previous step only activates the autostart; it didn't start it for the current boot cycle):

sudo /opt/local/apache2/bin/apachectl -k start

Point your browser to http://localhost/ and verify that Apache is running. (For Apache 2.2.8, you should see a giant "It works!" displayed.)

Reboot your machine, point your browser again to http://localhost/, and verify that Apache has started and is running.

You may want to enable local access to the "Apache HTTP Server Manual" and to User web pages. In /opt/local/apache2/conf/httpd.conf, uncomment the lines:

Include conf/extra/httpd-userdir.conf


Include conf/extra/httpd-manual.conf

In /opt/local/apache2/conf/extra/httpd-userdir.conf, change the line

UserDir public_html


UserDir Sites

(this may already be done for you by the MacPorts installation) and add the following lines to the end of the file:

# Include user configurations
Include /private/etc/httpd/users/*.conf

Restart Apache (sudo /opt/local/apache2/bin/apachectl -k restart) and verify that everything's running. You should now be able to go to http://localhost/manual/ to access the manual, and you should be able to access your personal user web site at http://localhost/~username/, where "username" is your login id.

Note: When you upgrade your Apache installation (e.g., sudo port upgrade apache2), be aware that the upgrade will not affect your http.conf file, but it will overwrite the other configuration files, such as the httpd-userdir.conf file. Therefore, if you've made any changes to those files (e.g., activating User web pages), you will need to save your httpd-userdir.conf file before upgrading and then reapply your changes after the upgrade. However, it would probably be a good idea to save your http.conf file too, just to be safe.

Step 3: Install MySQL

sudo port install mysql5 +server

Activate your MySQL server installation so that it autostarts when you boot your machine:

sudo launchctl load -w /Library/LaunchDaemons/org.macports.mysql5.plist

Set up the main database

sudo mysql5 mysql_install_db5 
sudo chown -R mysql:mysql /opt/local/var/db/mysql5/ 
sudo chown -R mysql:mysql /opt/local/var/run/mysqld5/ 
sudo chown -R mysql:mysql /opt/local/var/log/mysql5/ 

If that doesn't work try this:

sudo mysql_install_db5 
sudo chown -R mysql:mysql /opt/local/var/db/mysql5/ 
sudo chown -R mysql:mysql /opt/local/var/run/mysqld5/

Start the MySQL daemon

sudo /opt/local/bin/mysqld_safe5 &

and then verify that it's running

ps –ax | grep mysql

Set the MySQL root password (it's currently empty)

mysqladmin5 –u root –p password <new-password>

where <new-password> is your new desired root password.

Test everything by logging in to the server.

mysql5 -u root -p

Once you're logged in, simply exit the session

mysql> exit

As a last test, reboot your machine and then run

ps –ax | grep mysql

again to verify that the daemon is still active.

Step 4: Install PHP

Note: Currently, the PHP5 installer uses wget to install the PEAR environment, but the installer does not list wget as a dependent component. So, the installation will fail during the staging operations if you do not manually install wget first.

sudo port install wget
sudo port install php5 +apache2 +mysql5

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:

DirectoryIndex index.html index.cgi index.php

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/extras-conf/mod_php.conf

You may also need to load the PHP module before including mod_php.conf as described above:

# Load the PHP module
LoadModule php5_module modules/

Set up your PHP configuration files

cd /opt/local/etc
sudo cp php.ini-dist php.ini
sudo cp pear.conf.sample pear.conf

Restart Apache so that your changes take effect

cd /opt/local/apache2/bin/
sudo ./apachectl -k restart

Create a file named phpinfo.php that contains the following three lines


and place it in your Apache "DocumentRoot" directory (should be "/opt/local/apache2/htdocs") or your own user "Sites" directory if you activated user directories as specified above. Point your browser to http://localhost/phpinfo.php (or http://localhost/~username/phpinfo.php if applicable) and verify that the correct version of PHP is active (v5.2.6 as of this writing) and that MySQL support is active (you may want to search the page for "mysql").

As a last check, reboot and verify that everything has autostarted and is running (i.e., repeat the above tests).

Step 5: Install phpMyAdmin (optional)

Use MacPorts to install the latest version of phpMyAdmin.

sudo port install phpmyadmin

Update Apache's httpd.conf file to find phpmyadmin. First add the following lines to the end of the file:

# Local access to phpmyadmin installation
Include conf/extra/httpd-phpmyadmin.conf

and then create a file /opt/local/apache2/conf/extra/httpd-phpmyadmin.conf containing this text:

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

<Directory "/opt/local/www/data/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. First, set up the config file:

cd /opt/local/www/data/phpmyadmin/
cp libraries/config.default.php ./

This will create a file in the phpMyAdmin folder. Edit that file, and locate the lines:

$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)

You can either change the auth_type from 'config' to 'cookie' or 'httpd', or alternatively provide the password you selected for the root user in the 'password' option.

To check your phpMyAdmin installation, point your browser to http://localhost/phpmyadmin (or http://localhost/~username/phpmyadmin if applicable) and verify that phpMyAdmin loads and can access your database (by providing a username and password, depending on the authentication method you selected).

<- Back to the HOWTO section