In this short article I’m going to show you how to setup Apache on macOS using Docker.
Why use Docker? macOS includes a build in Apache server, but whereas in days’ past you could easily enable it with a simple checkbox, nowadays you need a bit of Apache knowledge to activate it.
I’m not sure why Apple discontinued official support of it. Perhaps the version included in macOS isn’t secure enough, but there are other options, one option being XAMPP if you like to avoid the command line.
But, if you’re reasonably comfortable with the command line then the Docker approach may be for you. I’ve tried to write this guide assuming little familiarity with the concepts of Docker, especially as I’m incredibly new to it myself.
- Install Docker for MacThankfully, installing Docker on a Mac is straight forward. Head here to download and install it. Once installed it’ll sit happily as an icon in your menubar.
- Start Docker MachineThe Docker Machine is a virtual machine which will run your Apache container. Open up a command line and run the following:
docker-machine start && eval $(docker-machine env)
All being well, you should see something like this:
Starting "default"... (default) Check network to re-create if needed... (default) Waiting for an IP... Machine "default" was started. Waiting for SSH to be available... Detecting the provisioner... Started machines may have new IP addresses. You may need to re-run the `docker-machine env` command.
- Pull Down and Start linode/lampAssuming your website is in
~/Sites, run the following which will pull down an image for a full LAMP stack and start it:
sudo docker run -p 80:80 -v ~/Sites:/var/www/example.com/public_html -t -i linode/lamp /bin/bash
You’ll need to supply your user password.
Again, if everything goes according to plan, you’ll see a new prompt like this:
- Start ApacheFinal task, start Apache:
root@d3e5bfcbb42f:/# service apache2 start * Starting web server apache2 *
This is a very basic guide. If you’re like me, you probably have subdomains linking to different directories, and to get that working we’ll need to modify the image to our needs, and that’s a challenge for another time.