Installing MAMP (Mac OS X Apache MariaDB PHP) using MacPorts

Update: I updated the blogpost for PHP 7 instead of PHP 5. If you want to update your current PHP 5 installation to a PHP 7 installation, see this blogpost: Migrating/updating from PHP 5 to PHP 7 on OS X (MacPorts).

MacPorts is a BSD ports like package management system for OS X.

The MacPorts Project is an open-source community initiative to design an easy-to-use system for compiling, installing, and upgrading either command-line, X11 or Aqua based open-source software on the OS X operating system.

The tool is very handy when it comes to installing command line tools for Mac. In this guide I will use it to install Apache, MariaDB and PHP. You could also install them using Homebrew, or use the packages that come with your Mac, but I prefer MacPorts… So if you don’t have MacPorts installed, follow the installation instruction on their website.

Before installing any ports, make sure you have the latest version of the ports tree:

$ sudo port selfupdate


If you have web sharing enabled on your Mac, you should disable it before continuing. Web sharing can be found under ‘System preferences’, ‘Sharing’, …

Time to install Apache:

$ sudo port install apache2

Whenever your installation is completed, you can edit Apache’s configuration file: /opt/local/apache2/conf/httpd.conf. Probably you want to set DocumentRoot to your local Sites folder. To do this change /opt/local/apache2/htdocs to your local sites folder e.g. /Users/Mathias/Sites.

You must also allow traffic to go to your webserver otherwise you will get “Permission denied” errors. By default you have a block like this in your Apache config:

<Directory />
    Order deny,allow
    Deny from all

Replace it with the following directive:

<Directory /path/to/webroot>
    Order allow,deny
    Allow from all

Don’t forget to verify your changes after every modification you do to httpd.conf!

$ /opt/local/apache2/bin/apachectl -t

When everything is configured, you can start Apache using MacPorts services:

$ sudo port load apache2

Stopping services can be done using the unload statement.

Apache should be functioning right now, more configuration details can be found everywhere on the internet, I’m not gonna explain the whole config file here…

MariaDB (MySQL)

Again, we use MacPorts:

$ sudo port install mariadb-server

Once MariaDB is installed, we need to create the main databases:

$ sudo -u _mysql /opt/local/lib/mariadb/bin/mysql_install_db

Time to start MariaDB:

$ sudo port load mariadb-server

Next we need to create a password for the root user, don’t forget to do this step! This procedure will interactively ask you some security details:

$ /opt/local/lib/mariadb/bin/mysql_secure_installation

If you work a lot with sockets for MySQL/MariaDB, you can create a symbolic link from the default socket path to MacPort’s path:

$ sudo ln -s /opt/local/var/run/mariadb/mysqld.sock /tmp/mysql.sock

You can also specify the socket path in your PHP config file: see below…

Note: MacPorts MariaDB has skip-networking enabled by default in /opt/local/etc/mariadb/macports-default.cnf. If you want to use for your MySQL connections, you should comment out that line.

If you want to use mysql on the command line, you can link mysql to MariaDB:

$ sudo port select --set mysql mariadb


Last step is installing PHP:

$ sudo port install php70-apache2handler
$ sudo port install php70-mysql

You might also need the following PHP extensions:

$ sudo port install \
	php70-curl \
	php70-exif \
	php70-gd \
	php70-gettext \
	php70-iconv \
	php70-imap \
	php70-mbstring \
	php70-mcrypt \
	php70-openssl \

Set up your PHP configuration files. For development purposes use:

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

For production use:

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

Enable the PHP module in Apache

$ cd /opt/local/apache2/modules
$ sudo /opt/local/apache2/bin/apxs -a -e -n "php7"

in Apache’s config file /opt/local/apache2/conf/httpd.conf, add index.php to the DirectoryIndex:

<IfModule dir_module>
    DirectoryIndex index.php index.html

Make sure that Apache includes the PHP config, check your httpd.conf file for the following lines:

# Include PHP configurations
Include conf/extra/mod_php70.conf

Also verify that the .so shared object for PHP is included:

# Load the PHP module
LoadModule php7_module modules/

Before we can use MySQL in our PHP code, we must set the default socket path in /opt/local/etc/php70/php.ini. Search for mysql.default_socket, mysqli.default_socket and pdo_mysql.default_socket and assign the MariaDB socket to them: /opt/local/var/run/mariadb/mysqld.sock.

If you regularly use PHP from the command line, you also want to link the php command to the MacPorts PHP version:

$ sudo port select --set php php70

If you want to have colored PHP CLI output,  you must enable it by installing php posix.

$ sudo port install php70-posix


Verify your Apache config, restart Apache, restart MariaDB and everything should work correctly!