AMP on OSX is probably the most difficult to setup, but it is nothing that an experienced and dedicated web application enthusiast like yourself cannot handle.
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. The time I first looked, only MacPorts had the most recent httpd, 2.4.7 at the time, so I resolved to compile from source, which turned out to be a lot easier than I first thought it would be. I found Patrick Bougie’s guides to be invaluable resources. If you follow his Apache setup guide, you should have a good time, but I’m going to share with you my own personal experience:
Compiling from source
First, take care of software dependencies. Bourgie predicts you will only need
pcre, but I was prompted to also install
libtools. I got both of these from Homebrew.
Get the httpd source code straight from Apache’s site. By convention, source code for programs you plan to compile for use on your system should go in /usr/local/src, so extract the tarball there. Take note of the Xcode commands that Mr. Bourgie suggests running in order to prevent problems during compilation. It would appear that the source prefers the
cc C++ compiler provided by XCode rather than the one already on your system.
It’s a good idea to check out the Compiling and Installing doc on Apache’s site, where they tell you how to enable modules as Dynamic Shared Objects. All you really need to do, however, is pass a few options to the configure script. Again, I direct you to Bourgie’s guide for details, rather than type them out again here.
After you’ve ran the configure script that installs httpd in the path
/usr/local/apache-2.4.x, followed by
make install, the Apache Web Server should essentially be in place. Next, you’ll just want to make a symbolic link to the folder created from this process that contains the web server’s binary files. It is suggested to name the link to the folder simply ‘apache’.
Next, of course, you’ll want to setup Apache’s configuration files to whatever is suitable to the kind of site you’d like to host. File name paths should follow the same rules as any other Linux system. One important difference between OS X and the other systems is that instead of services or daemons, OS X uses
.plist files to manage programs that should always be running. Bourgie’s guide, near the end, has advice for how to set that up as well. You will basically be making a new
.plist file for the new apache and keeping the one that comes with OS X where it is.
Lastly, you will probably want to use Bourgie’s shell commands to add the apache folders to your PATH within your
.bash_profile so you’ll have the right environment setup whenever you log in.
When all of these steps are complete, you should have an up-to-date Apache web server running on your OS X machine.
Initially, I was reluctant to use the httpd that comes with OS X, mostly on the grounds that it did not keep up with Apache’s latest stable version. You should still feel free to replace it with any of the other solutions if you like, but macOS Sierra may make it more difficult with their increasingly unique firewall settings, which require
pfctl configuration. Such a solution as discussed below has worked well in the past, but with the event of macOS Sierra, you may find your web server mysteriously not listening to any ports when programs other than the built-in httpd try to listen on port 80, or sometimes any port at all.
Luckily, the built-in httpd does not have these restrictions and is ready to use.
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.
If you’d like to manage httpd through a Homebrew formula, you can easily do so with the following:
brew tap homebrew/apache brew install httpd24 [--with-privileged-ports]
--with-privileged-ports option will use a launchd service that passes this option to httpd and allows listening on ports 80 and 443. Otherwise, httpd will need to listen on other ports and forward requests to your computer to corresponding ports. This guide has a launchd config for automatically redirecting such requests with the pfctl command.
--with-privileged-ports, you can copy the launchd configuration from
/Library/LaunchDaemons in order to use the
UserName key. The LaunchControl program reminds you that
GroupName are only supported by daemons. Set these to
wheel, respectively. Also, make sure that the httpd.conf has a
Listen 80 directive.
El Capitan 10.11
If upgrading from 10.10 to 10.11, there were more than likely permission and ownership changes for
brew doctor command will inform you if anything needs to be reset to the appropriate access.
This setup was slightly trickier, but not too much so. Bourgie’s PHP guide for OS X is equally indispensable. He warns that you will want the programs
freetype before you start, which I can vouch for. I also had to install
libpq, for PostgreSQL support. I was able to obtain all of these through Homebrew.
There are a ton more configure options available for PHP. You will absolutely want to see their list of these options. Again, Bourgie’s suggestions in the Compile and Install section will steer you onto the right path.
The installation options here are various and are best detailed at the Github page. In a nutshell, make sure you have Xcode and have installed the command line tools, then the basic installation commands are as follows:
brew tap homebrew/dupes brew tap homebrew/versions brew tap homebrew/homebrew-php brew install php71
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
sudo /usr/libexec/locate.updatedb locate my.cnf
mdfind (OS X’s equivalent to locate):
mdfind -name my.cnf