OS X

Updated:

You will have the easiest time if you use the httpd and php that comes with the OS, but some alternatives are listed below.

There are some software repositories for OS X that aim to provide the same kind of versatility that Linux software repos provide. The most popular are probably MacPorts, Homebrew, and Fink. These days, I would only recommend Homebrew because you will find the most support for it. They provide the most current versions of httpd and php.

Previously, I had instructions for compiling the Apache Web Server from source, but I don’t see a real benefit unless there was a particular version you wanted. Again, you will run into the least problems using either the built-in programs or getting them from Homebrew

Apache

System-provided Apache

If there are problems getting httpd to listen on ports 80 and 443, try running service as root. You can do so with sudo apachectl start. You can also use sudo launchtl -w /System/Library/LaunchDaemons/org.apache.httpd.plist.

Configuration

As of macOS Sierra, and surely some older versions of OS X, you will find the httpd configuration files in /private/etc/apache2. There are a number of files in here, but you will want to look at httpd.conf to start, in order to familiarize yourself with the basic setup. You will see many familiar sections including the ServerRoot, a giant block of LoadModule statements, a well-advised AllowOverride None directive within a basic site definition for /Library/WebServer/Documents, and some configuration to be included that has been commented out. Most of these would-be included files are in the extra folder. You’ll also notice the very last line that will include any .conf files in the other folder. That’s where you’ll want to add any additional site or server configuration, and where you’ll see such a file for setting up the PHP module.

Homebrew

To get started, just do the following:

brew install httpd24

macOS Sierra

As of the latest macOS, things are running pretty smoothly. Run the command brew info httpd24 to get a quick review on how things are setup. You can start the service with apachectl start or brew services start httpd.

Doing so will make the web server available on ports 8080 and 8443. To use ports 80 and 443, you’ll have to run the service as root. You can do so with either the command sudo apachectl start or sudo brew services start httpd.

The default DocumentRoot is /usr/local/var/www. The config files can be found in /usr/local/etc/httpd. At the bottom of httpd.conf, you should add the line IncludeOptional /usr/local/etc/httpd/other/*.conf and create the other folder as a place to house additional site configurations.

PHP

System-Provided

You will find the php executable in /usr/bin/php. By default, it looks in /etc/ for the php.ini which probably won’t be in there, but you can create one from /private/etc/php.ini.default. You can easily add the module to the built-in httpd by uncommenting the LoadModule php7_module line in /private/etc/apache2/httpd.conf.

Homebrew

Make sure you have Xcode and have installed the command line tools installed or you may be prompted to do so before proceeding:

brew install php72

Run brew info php72 to get familiar with the landscape. I’d recommend adding the configuration commands for loading the module and applying it to PHP files in a separate httpd conf file.

MySQL

Getting the latest MySQL on OS X is, fortunately, not too tricky. For one, on the download page are DMG archives which you can use to easily install the program and configure it to run automatically. After mounting the DMG, open the PKG file, which will start the install. By default, it will install the program in /usr/local under a directory named after the MySQL version. If you run the Startup Item package, that will save the plist file that defines the service. Also, be sure to open the prefPane file which will add MySQL to System Preferences. More info can be found in the ReadMe that came with the disk image.

If you would like to use MariaDB, it will probably be easiest to install it from Homebrew, as they keep it up to date and help manage any dependencies. If you follow MariaDB’s instructions for installation via Homebrew, you should definitely be good to go. One thing to watch out for is that some of the commands, like mysql_secure_installation, must be ran from a particular directory. Be sure to carefully follow the directions in the Starting MariaDB section.

Also, I recommend taking a look at the plist file that you must enable with the launchctl command. It shows exactly what command is executed to start the MySQL server. Take note of the value sent to the --data-dir option, which is the directory that contains the database files. This is where you will want to place any databases you are migrating from other MySQL databases.

If you are having trouble finding the my.cnf with MySQL’s configuration, be sure to check the common MySQL and MariaDB locations. Otherwise, you may have to resort to a system-wide search of the file. From the command line you can either use locate:

sudo /usr/libexec/locate.updatedb
locate my.cnf

or mdfind (OS X’s equivalent to locate):

mdfind -name my.cnf

What say you?