Installing Apache, PHP, and MySQL on Mac OS X El Capitan

Main Thread 3 min read

macOS Update: While these instructions still work, there are new posts for recent versions of macOS, the latest being Install Apache, PHP, and MySQL on macOS Mojave.

PHP Update: Mac OS X El Capitan comes pre-installed with PHP version 5.5 which has reached its end of life. After you complete this post, you should upgrade PHP on Mac OS X.

Note: This post is for new installations. If you have installed Apache, PHP, and MySQL for Mac OS X Yosemite, read my post on Updating Apache, PHP, and MySQL for Mac OS X El Capitan.

Mac OS X runs atop UNIX. So most UNIX software installs easily on Mac OS X. Furthermore, Apache and PHP come packaged with Mac OS X. To create a local web server, all you need to do is configure Apache and install MySQL.

I am aware of the web server software available for Mac OS X, notably MAMP. These get you started quickly. But they forego the learning experience and, as most developers report, can become difficult to manage.

Running Commands

First, open the Terminal app and switch to the root user so you can run the commands in this post without any permission issues:

1sudo su -

Enable Apache on Mac OS X

1apachectl start

Verify It works! by accessing http://localhost

Enable PHP for Apache

First, make a backup of the default Apache configuration. This is good practice and serves as a comparison against future versions of Mac OS X.

1cd /etc/apache2/
2cp httpd.conf httpd.conf.bak

Now edit the Apache configuration. Feel free to use TextEdit if you are not familiar with vi.

1vi httpd.conf

Uncomment the following line (remove #):

1LoadModule php5_module libexec/apache2/libphp5.so

Restart Apache:

1apachectl restart

You can verify PHP is enabled by creating a phpinfo() page in your DocumentRoot.

The default DocumentRoot for Mac OS X El Capitan is /Library/WebServer/Documents. You can verify this from your Apache configuration.

1grep DocumentRoot httpd.conf

Now create the phpinfo() page in your DocumentRoot:

1echo '<?php phpinfo();' > /Library/WebServer/Documents/phpinfo.php

Verify PHP by accessing http://localhost/phpinfo.php

Install MySQL on Mac OS X El Capitan

Download and install the latest MySQL generally available release DMG for Mac OS X.

The README suggests creating aliases for mysql and mysqladmin. However there are other commands that are helpful such as mysqldump. Instead, you can update your path to include /usr/local/mysql/bin.

1export PATH=/usr/local/mysql/bin:$PATH

Note: You will need to open a new Terminal window or run the command above for your path to update.

Finally, you should run mysql_secure_installation. While this isn't necessary, it's good practice to secure your database.

Connect PHP and MySQL

You need to ensure PHP and MySQL can communicate with one another. There are several options to do so. I do the following:

1cd /var
2mkdir mysql
3cd mysql
4ln -s /tmp/mysql.sock mysql.sock

Additional Configuration (optional)

The default configuration for Apache 2.4 on Mac OS X seemed pretty lean. For example, common modules like mod_rewrite were disabled. You may consider enabling this now to avoid forgetting they are disabled in the future.

I edited my Apache Configuration:

1vi /etc/apache2/httpd.conf

I uncommented the following lines (remove #):

1LoadModule deflate_module libexec/apache2/mod_deflate.so
2LoadModule expires_module libexec/apache2/mod_expires.so
3LoadModule rewrite_module libexec/apache2/mod_rewrite.so

If you develop multiple projects and would like each to have a unique url, you can configure Apache VirtualHosts for Mac OS X.

If you would like to install PHPMyAdmin, return to my original post on installing Apache, PHP, and MySQL on Mac OS X.

Find this interesting? Let's continue the conversation on Twitter.