Version 84 (modified by magill@…, 7 years ago) (diff)

Apache instructions extracted to howto/Apache2

<- Back to the HOWTO section

These installation instructions are written for the following versions:

  • Apache 2.2.* - See: howto/Apache2 for instructions on installation of Apache2
  • MySQL 5.6.*
  • PHP 5.4.*

Step 3: Install MySQL

If you merely want to access a MySQL server running on another computer, skip to step 4. If you want to run a MySQL server on this computer, install MySQL like this:

sudo port install mysql51-server

mysql51-server is used in this example because it is the closest to the port that was previously used in this example, which was mysql5-server. However, there are newer versions of mysql in MacPorts that may be worth considering instead. The only reason for not using any of these newer versions in this example is because that would probably require making even more changes than the move to mysql51 required. If you are personally moving from the mysql5 port to the mysql51 port, you may want to add the +openssl variant, as the openssl support that had previously been available by default in the mysql5 port has been moved to a variant in the mysql51 port. Even though the mysql51-server port does not have this variant itself, if you supply the variant with your invocation of port, it will still get passed down to the mysql51 port, because it will be installed as a dependency. Also, mysql51 now uses the port select mechanism, so you will have to run one of the following to get the mysql commands in your PATH: either:

sudo port select mysql mysql51


export PATH=$PATH:/opt/local/lib/mysql51/bin

Once you have done at least one of the two previous commands, then how to set up the main database depends on the version of OS X you're using.

Set up the main database like this:

sudo -u _mysql mysql_install_db5 
sudo chown -R _mysql:_mysql /opt/local/var/db/mysql51/ 
sudo chown -R _mysql:_mysql /opt/local/var/run/mysql51/ 
sudo chown -R _mysql:_mysql /opt/local/var/log/mysql51/ 

If that fails to work, then try this:

sudo mysql_install_db5 
sudo chown -R _mysql:_mysql /opt/local/var/db/mysql51/ 
sudo chown -R _mysql:_mysql /opt/local/var/run/mysql51/

However, for OS X 10.4 and below, up the main database like this:

sudo -u mysql mysql_install_db5 
sudo chown -R mysql:mysql /opt/local/var/db/mysql51/ 
sudo chown -R mysql:mysql /opt/local/var/run/mysql51/ 
sudo chown -R mysql:mysql /opt/local/var/log/mysql51/ 

If that fails to work, then try this:

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

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

sudo port load mysql51-server

and then verify that it is running:

ps -ax | grep mysql

Set the MySQL root password (it should currently be empty, see also the security option below):

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

where <new-password> is your new desired root password. You will be prompted for your existing password ("Enter password:"); since it is empty, just press Return.

Test everything by logging in to the server.

mysql5 -u root -p

Once you are logged in, simply exit the session like this:

mysql> exit ;

If desired, reboot your machine and then run:

ps -ax | grep mysql

again to verify that the daemon is again running.

Optional database upgrade

If the database exists from a previous installation, you may need to upgrade.

man mysql_upgrade
sudo /opt/local/etc/LaunchDaemons/org.macports.mysql5/mysql5.wrapper restart
sudo /opt/local/lib/mysql5/bin/mysql_upgrade -u root -p
sudo /opt/local/etc/LaunchDaemons/org.macports.mysql5/mysql5.wrapper restart

(note: these instructions might not be applicable any longer...)

Optional security configuration

There is an interactive program to secure a MySQL installation.

$ man mysql_secure_installation
$ /opt/local/bin/mysql_secure_installation5

The following is a more detailed process that may achieve the same results as the interactive program. Place the following into mysql_security.sql and replace the 'MyNewPass' with your root password. Note that the SQL will remove all access for root from any location other than 'localhost'. You might like to keep this SQL file - all the lines beginning with '--' are doc-comments about how to use it.

-- HOW TO USE THIS FILE (with a MacPorts installation):
-- $ sudo /opt/local/etc/LaunchDaemons/org.macports.mysql5/mysql5.wrapper stop
-- $ sudo mysqld_safe5 --init-file=mysql_security.sql &
-- The init will terminate if there are any errors in the init file.
-- Wait a bit to be sure the server is running.
-- If it's running, then shutdown the server (root password required):
-- $ /opt/local/bin/mysqladmin5 -u root -p shutdown
-- Check that everything worked.  There may be an ERROR if the test database
-- doesn't exist.  Otherwise there should be no errors in the file reported by
-- 'mysqld_safe Logging to ..." during the mysql_safe5 startup from above:
-- $ sudo tail -n 20 /opt/local/var/db/mysql5/*.err 

UPDATE mysql.user SET Password=PASSWORD('MyNewPass') WHERE User='root';
DELETE FROM mysql.user WHERE User='root' AND Host!='localhost';
DELETE FROM mysql.user WHERE User='';
DELETE FROM mysql.db WHERE Db='test' OR Db='test\\_%';

The server startup options --skip-networking and --skip-grant-tables may be useful while implementing security. See also

Step 4: Install PHP

sudo port install php54-apache2handler
sudo port install php54-mysql

Note: the php54 port contains the core PHP features, but there are many optional features available in separate ports, some of which you may want to install as well (such as MySQL support via the php54-mysql port). Use port search php54 to see all the ports that are available.

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.html

and change it this way:

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_php54.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/

Notice either of the above commands are only required if not present in the httpd.conf file, as the apxs command (executed above) will write those for you.

Set up your PHP configuration files

cd /opt/local/etc/php5
sudo cp php.ini-development php.ini

or (depending on your needs)

cd /opt/local/etc/php5
sudo cp php.ini-production php.ini

Setup the MySQL default socket to use the MacPorts configuration (/opt/local/var/run/mysql5/mysqld.sock)

$ sudo -i
# cd /opt/local/etc/php5
# cp php.ini php.ini.bak
# defSock=`/opt/local/bin/mysql_config5 --socket`
# cat php.ini | sed \
  -e "s#pdo_mysql\.default_socket.*#pdo_mysql\.default_socket=${defSock}#" \
  -e "s#mysql\.default_socket.*#mysql\.default_socket=${defSock}#" \
  -e "s#mysqli\.default_socket.*#mysqli\.default_socket=${defSock}#" > tmp.ini
# grep default_socket tmp.ini  # Check it!
# mv tmp.ini php.ini
# exit # OR rm php.ini.bak && exit

If you installed php5 with the +pear variant you should also:

cd /opt/local/etc/php5
sudo cp pear.conf.sample pear.conf

Restart Apache so that your changes take effect

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

Note: Make sure to use the full path as shown here, as apachectl in your PATH refers to the system provided Apache!

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.3.0 as of this writing) and that MySQL support is active (you may want to search the page for "mysql").

If by some reason the server still doesn't interpret PHP files (your web client tries to download them) it means the PHP configurations, as described above, are not taking effect and in that case you should open the httpd.conf file once again and search for the block

<IfModule mime_module>

And then add before the end

AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps

Then restart the server.

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/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

Restart Apache so that your changes take effect

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

Note: Make sure to use the full path as shown here, as apachectl in your PATH refers to the system provided Apache!

Finally, you need to set up the phpMyAdmin configuration to access mySQL. First, set up the config file:

cd /opt/local/www/phpmyadmin/
sudo cp

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)

Where ' ' is an empty password; fill it with your MySQL root password. 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.

There is also an interactive setup, see and the demo at

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

Optional pmadb

For additional features provided by pmadb (, run

mysql5 -u root -p < /opt/local/www/phpmyadmin/examples/create_tables.sql

Then run this SQL to setup the pma 'controluser' (change the 'pmapass' to your password).

-- HOW TO USE THIS FILE (with MacPorts installation)
-- mysql5 -u root -p < /opt/local/www/phpmyadmin/examples/create_tables.sql
-- mysql5 -u root -p < mysql_phpMyAdmin_pmaSetup.sql

CREATE USER 'pma'@'localhost' IDENTIFIED BY 'pmapass';

GRANT USAGE ON mysql.* TO 'pma'@'localhost' IDENTIFIED BY 'pmapass';
    Host, User, Select_priv, Insert_priv, Update_priv, Delete_priv,
    Create_priv, Drop_priv, Reload_priv, Shutdown_priv, Process_priv,
    File_priv, Grant_priv, References_priv, Index_priv, Alter_priv,
    Show_db_priv, Super_priv, Create_tmp_table_priv, Lock_tables_priv,
    Execute_priv, Repl_slave_priv, Repl_client_priv
    ) ON mysql.user TO 'pma'@'localhost';
GRANT SELECT ON mysql.db TO 'pma'@'localhost';
GRANT SELECT ON TO 'pma'@'localhost';
GRANT SELECT (Host, Db, User, Table_name, Table_priv, Column_priv)
    ON mysql.tables_priv TO 'pma'@'localhost';

-- Privileges
GRANT SELECT, INSERT, DELETE, UPDATE ON `phpmyadmin`.* TO 'pma'@localhost;

Finally, edit the config file, at /opt/local/www/phpmyadmin/, so it's like this:

/* User for advanced features */
$cfg['Servers'][$i]['controluser'] = 'pma';
$cfg['Servers'][$i]['controlpass'] = 'pmapass';
/* Advanced phpMyAdmin features */
$cfg['Servers'][$i]['pmadb'] = 'phpmyadmin';
$cfg['Servers'][$i]['bookmarktable'] = 'pma__bookmark';
$cfg['Servers'][$i]['relation'] = 'pma__relation';
$cfg['Servers'][$i]['table_info'] = 'pma__table_info';
$cfg['Servers'][$i]['table_coords'] = 'pma__table_coords';
$cfg['Servers'][$i]['pdf_pages'] = 'pma__pdf_pages';
$cfg['Servers'][$i]['column_info'] = 'pma__column_info';
$cfg['Servers'][$i]['history'] = 'pma__history';
$cfg['Servers'][$i]['designer_coords'] = 'pma__designer_coords';

<- Back to the HOWTO section