(Alternative titles for this article could be ‘How not to install Apache on Mac OSX’ or ‘Don’t make the same mistakes I made trying to install Apache on Mac’).
As my team gear up for supporting the increasing number of Autodesk Cloud Platform APIs, and in preparation for some really cool new APIs that we’ll be announcing soon :-), I’ve been taking the plunge and starting to get to grips with setting up my own web server. As a Windows desktop programmer making the jump to the web, my natural instinct is simply to take advantage of the free Windows Azure credits available through my MSDN subscription or to setup IIS on my local computer. But in the real world, many developers prefer to use Linux for their server OS – a big reason for that being that its a lot cheaper server option than a Microsoft OS (i.e. its free).
The server of choice on Linux is Apache (or so I’m told), so I decided that I’d use Apache for my first serious attempt at learning to setup a web server. However, I felt that trying to learn Linux from scratch at the same time as learning Apache was too big an ask, so I chose to install Apache on my Mac OS (rather than on my Parallels Desktop Windows image).
Not many people are likely to be creating production web servers using Mac Os, but the Mac OS is Unix based, so I decided this would give me an experience closer to Linux than installing on Windows. Besides, its always easy to install software on the Mac – or so I thought.
Off I went to apache.org and found my way to the Apache httpd download page. (Apache httpd is the Apache HTTP Server). I clicked on the link to the latest stable release, clicked the link to the Binaries downloads and did a double-take – they don’t have pre-compiled binaries for Mac OS. Ok – so now I had to download the source code and compile it myself. How hard can that be?
[
BTW Don’t try to do any of this yourself until you’ve read this whole article – I’ll tell you a really easy way to install Apache and various nice Apache add-ons at the end.
Also, after I went through all this, I discovered that Mac OS comes with Apache pre-installed – its in the hidden /private/etc folder – but that will likely be an outdated version that came with your Mac OS install, so you’d probably want to update it anyway.
]
My second problem – the last time I used Unix was about 20 years ago during my physics research days. Compiling Apache code requires using the Mac OS Unix terminal. At least after going through this, I have now recovered a basic understanding of using Unix. (Thank you to Cyrille for helping me with the basics – particularly for telling me about prefixing a Unix command with the ‘sudo’ command to invoke it using super-user privileges).
To compile the Apache source, you can follow the ‘simple’ instructions at http://httpd.apache.org/docs/2.4/install.html. I put ‘simple’ in quotes to denote sarcasm, because there are a few caveats:
- Don’t just dive into the ‘Overview for the impatient’ (a.k.a. quick start instructions). Make sure you read the Requirements listed lower down. In my case, I had to install and build the PCRE library before I could compile Apache httpd.
- Compiling httpd and PCRE requires Xcode to be installed. The Apache Configure file expects Xcode to be installed in the Applications folder and to be called Xcode.app. I’d renamed my Xcode to append its version number because I have multiple versions installed on my Mac – this caused the Apache Configure to fail.
- I had to make an additional tweak to my XCode.app, as described here - http://stackoverflow.com/questions/13587001/problems-with-compiling-apache2-on-mac-os-x-mountain-lion.
After working through those caveats, and making liberal use of ‘sudo’, I’d finally compiled the Apache HTTP Server. I ran the ‘apachectl -k start’ command, and Apache started with no problem.
Now I had two more tasks – configure Apache to allow SSL (HTTPS calls), and install an extension to enable PHP?
Why PHP? I was setting up Apache during our DevTech EMEA team’s annual meeting, so I quizzed those present on which they thought was the best Apache server-side language for a newbie. The main options suggested were PHP or Python, and the summary that convinced me to go for PHP was this:
“PHP can be embedded directly into an HTML page and is translated by the server when they page is served to the client, whereas Python must write the additional code to the HTML”.
It remains to be seen whether I’ve made the right choice.
To setup SSL, I started following the nicely detailed instructions posted to webdev studios - http://webdevstudios.com/2013/05/24/how-to-set-up-ssl-with-osx-mountain-lions-built-in-apache/, but lunchtime arrived before I’d really started and (for some reason) I changed to researching how to install PHP when I got back from lunch (perhaps that was the influence of the ‘sandwich poulet curry’ from our Neuchatel office cafe).
Googling for setting up PHP on Apache on a Mac, one of the first search hits was this Wikipedia article:
http://en.wikipedia.org/wiki/List_of_Apache%E2%80%93MySQL%E2%80%93PHP_packages
This looked interesting – lists of full solution stacks for various OS platforms containing Apache, MySql, Perl/PHP/Python. The first stack listed for Mac was AMPPS – a stack of Apache, MySQL, MongoDB, PHP, Perl & Python – which included just what I needed and gave me the easy option of switching from PHP to Python if I changed my mind about learning PHP.
Another Google search took me to http://www.ampps.com/. A click on the Downloads link and I was downloading a Mac OS disk image from which I did the usual drag and drop routine into the Applications folder. Then running the installed AMPPS application gave me this:
Further investigating the Apache configuration file showed me that PHP was already enabled, as well as SSL (complete with a pre-installed test certificate).
Clicking the little house button in the AMPPS console opened this admin page on my localhost server:
Which gives access to easily downloadable extension modules for wikis, blogs, e-commerce etc. If you’re an experienced web developer, then all of this is probably boringly obvious to you, but after going through the Apache compilation process this was all very exciting for me. (BTW If you’re an experienced web developer, then why are you reading this :-)).
My final task was to run a sample web page on my new server that demonstrated a new Autodesk API (which hasn’t been released, so I can’t talk about it yet). That sample was using some JavaScript scripts stored on an Amazon S3 instance and only accessible using SSL (this is why I had to setup SSL). However, the sample wasn’t running, and my Chrome debugger was displaying an error telling me it couldn’t access the .js files from the S3 instance. I knew the files were there, so I guessed that the problem was a security issue – there was probably some setting required in Apache to allow scripts to be loaded and run from a different server.
Sure enough, after experimenting with google search terms, I finally found this article:
http://enable-cors.org/server_apache.html
(‘Cross-origin resource sharing’ was the search term I needed).
Once I’d updated my Apache configuration and restarted using the AMPPS console, my sample web page was working perfectly – which means that when our new API is released I should be in a position to write some blog posts about it.
Hi Stephen,
A really quick and easy way to set up a web server on mac is to use grunt and the grunt-contrib-connect plugin:
http://gruntjs.com
https://github.com/gruntjs/grunt-contrib-connect
Cheers, Jeremy.
Posted by: Jeremy Tammik | 04/25/2014 at 01:10 AM