Please note that you may have been redirected (you might like to make a note of the URL in the address bar of your browser and update accordingly) This is a permanent archvie but is no longer actively maintained. Please visit http://joshuaink.com for the latest updates.

Setting up an OS X development server by A.N. Idiot

Sunday June 12, 2005

Okay, so this was very much a case of fumbling around in the dark until stuff worked, lot's of Googling and breaking stuff. The end result is a dev server on OS X that is running Textpattern with clean URLs and a copy of WordPress for good measure. These notes are primarily so I don't forget how I did it, if they are useful to someone else, great! Be warned, though, I have no idea how secure this set up is and what flaws it has, so you follow these instructions at your own risk! Also, I am using OS 10.3.9 so I don't know if this would work on Tiger.

Update

Be sure to read the comments that follow this post before attempting this setup, there is some great advice to be had.

Turn off Personal Web Sharing

OS X does, as you probably know, ship with Apache and it's real easy to switch on and for a while I did use it. I am quite interested in learning a bit more about the whole serving up websites business, so first job is to do away with all the pre-installed Mac stuff.

Go to System Preferences > Sharing and make sure Personal Web Sharing is stopped.

Software

I started off with Mamp and that is probably good enough for a basic set up but I wanted to run Textpattern with clean URLs. Getting mod_rewrite to work on Mamp just wouldn't happen for me. So after a bit of Googling I came up with these downloads in preparation for my mission... gulp!

Terminal

You will need to have at hand Terminal in order to do some of the stuff. This is located in Applications > Utilities. This is the best bit actually because you get to feel like Neo for five minutes! Be careful though, I am told you can do some serious damage with Terminal.

Show hidden files

Having already had a play with Mamp, I noticed that I couldn't see .htaccess files. Also when installing Complete MySQL there was another hidden file I needed to get to. Eventually I found a note on Apple's developer site that describes how to show hidden files. It's gonna make your Mac look at little messier than before but it's kind of essential:

Open up Terminal and type in the following:

defaults write com.apple.Finder AppleShowAllFiles true

And that should be that.

Complete installs

The links to the three Complete packages are self explanatory. Each comes with a detailed Install document, follow them and you can't go wrong, much.

The only problem I ran into was creating a .bash_profile document in the home directory. With hidden files now showing I could see that my home directory (the house with my name on it) had no .bash_profile in it, so I created one with TextEdit. Again, something to watch out for is TextEdit saving it with an extension e.g. .bash_profile.rdf. If this happens click on the file and press COMMAND + I which will bring up the File Info panel and you can simply delete the .rdf from the Name & Extensions panel

In truth this had little effect for me when trying to access mysql via Terminal, unless I used the complete path i.e. /Library/MySQL/bin/mysql. Given that I have no intention of using Terminal for accessing MySQL I didn't worry about it and it has not had any impact on this setup thus far. Note: If anyone does know why I was getting a command not found error, I'd love to know.

One other note with these complete installs is the location they end up in. Obvious now but it caused me a bit of confusion, they are in the root library file and not the library file in you home directory. To find this spot open up your hard drive and look for the Library folder

phpMyAdmin versus CocoaMySQL

I had a bash at installing phpMyAdmin but to be honest they may as well have written the instructions in Wookie. I stumbled across CocoaMySQL after a bit of Googling, opened it up, it found the path to my MySQL server and within five minutes I had created a database as was running a local copy of Joshuaink. I thoroughly recommend it for the less technically minded.

Also worth noting is that which ever way you access MySQL, you can use your root account and the password you set when setting up MySQL for all your databases which is pretty damn convenient.

Httpd.config

Certainly if you are going to be experimenting, the httpd.config file will come into play. I started off with the Web Control app because it makes back ups, reverts easily back to the original file if you mess it up and it can check your syntax for you and if you aren't feeling confident it's a great way to start. It soon started to get a bit frustrating though because I couldn't do a find search to locate bits of the document.

I ended up going back to TextEdit but found I could no longer save the file from that app (though I could from Web Control). I am not sure if this happens by default or whether Web Control did it when it first ran but it turned out that the conf directory, located at /Library/Apache2/conf was locked, so again clicking on the directory and COMMAND + I brings up the info and I changed the Ownership & Permissions details from Owner: system to Owner: [my username]. I also did the same to the httpd.conf file for good measure and made sure they were both set to Read & Write for owners.

Virtual hosts

Virtual hosts were one of the big things I wanted to get done and I found two tutorials. One over at Mezzoblue and one over at SitePoint (scroll down the tutorial a bit). In the end I opted for the SitePoint one because it was getting late and my head seemed to manage with it a little better, though the URLs it produces are no where near as cool as Dave's. Again something to consider if you do use the SitePoint one, be careful with your naming conventions because it can impact how you use the web. For example I had a directory called joshuaink and where I used to just type joshuaink -- as opposed to the full URL -- into Firefox to reach my live site, I was now being taken to my localhost.

DirectoryIndex

It wasn't until I opened up the WordPress admin that I noticed I was getting a directory listing and had to manually click on index.php to get to the login page. This seems to be something to do with the DirectoryIndex bit of httpd.conf. Initially I was dropping the .htaccess file that ships with Textpattern into each and every directory with an index.php as it's starting point and it did solve the problem but that was getting a bit tiresome. Eventually I found out that there is something called DirectoryIndex in the httpd.conf file and having located it I changed it to this so that Apache recognises an index.php:

DirectoryIndex index.html index.htm index.php index.html.var

I really don't know if that is correct but it seems to have solved the problem.

Deleting .htaccess files

My final problem was deleting those .htaccess files I had spread everywhere and OS X wouldn't let me because it is a hidden file. To solve this I renamed it to .htaccess.txt and then I could delete it.

Conclusions

I have got a lot to learn about Apache but it was an interesting start and well worth the effort. With my iBook mostly offline, security is not a big deal for me. No doubt I will continue to fiddle until it breaks. If you have any tips or see something very wrong with the way I have setup, please do say.

  1. David Brent

    1735 days ago

    Nice one John! Just what I needed, I can finally crack back onto my development projects. Cheers
  2. Frode Danielsen

    1735 days ago

    Now wasn’t that fun? :) Now you could try building your own Apache, PHP and MySQL from source! Or maybe not ;)

    But to answer a specific question you had, about typing “mysql” in Terminal and getting “command not found”. It’s because the application mysql is not “in your path” as every unix geek would probably say. The path is a global environment variable the shell uses to search for commands (what we would call applications normally). It contains a string of paths, separated with :.

    Try typing “env” in Terminal, and you’ll get a list of all environment variables set for you. The path should be one of them.

    Now, concerning how to fix this for mysql, the answer would be to add /Library/MySQL/bin to your path. How to do this varies with the shell you use, but I think the default one for you is “bash”. So, in your .bash_profile file, add a line like this:
    export PATH=$PATH:/Library/MySQL/bin

    And you should be all set… Sorry for the long answer, but hope it helps you somewhat understand the why, not just the how :)
  3. Andy Hume

    1735 days ago

    Hi John, I’m not exactly sure what your problem with the ~/.bash_profile was. It sounds like the PATH variable might be set wrongly (although I’m no expert either). PATH defines the directories which you have direct access to. So for your MySQL commands you might want something like:

    PATH=/usr/bin/mysql

    Anyway, if your using Cocoa MySQL you probably won’t need shell access much, and then you can just issue the commands from the correct directory anyway.

    At home I run a Linux/Apache dev server on an old Pentium II box, and it works like a dream. I have access through any machine I connect to the network, and can even configure it for remote access. It has made a huge difference to the way I work to have this always available.

    That said, I do use the built-in Apache on Mac OS X as well, as it’s really handy if you want to do any work on, say, a long train journey.

    I just used the ready made stuff from Apple because it was sooo simple to set up once I’d installed MySQL. Now I just sync certain directories with my main dev when I’m working without a connection and then resync when I’m back online.

    I was thinking of doing a basic tutorial on setting Linux/Apache up on an old Windows machine. Loads of people must have old Win 98 boxes sitting around doing nothing. Kill Windows and put them to good use I say.
  4. Andy Hume

    1735 days ago

    ...and if it hadn’t taken me 15 minutes to write that answer then it might acutally have been of some use. I’m sure Frode’s expertise massively outshines mine anyway. :)
  5. Martin Smith

    1735 days ago

    Excellent guide John! Between you and me we have got this local site serving nonsense well and truly sorted – who needs techies eh? ;-)

    BTW – Did you give XAMPP a shot? Not sure if it’s the same for OSX but Steven Urmston made a comment on my site that under XAMPP the clean URLs work “right out of the box”. XAMPP also comes with phpMyAdmin built into the installer.
  6. Thomas B. Aschim

    1735 days ago

    Yup, the DirectoryIndex variable is the right way to go when you want to set index-files (you know you can have apache bring up e.g. thisishome.phtml as an index page -as long as it’s in that variable and isn’t outrun by any of the preceding files).

    You can also use .htaccess to do this in folders where you would prefer index.php to precede index.html. I believe the appropriate entry in the .htaccess file would then be:

    DirectoryIndex index.php index.html
  7. Martin Smith

    1735 days ago

  8. quis

    1735 days ago

    If you want to know a bit more about terminal geekiness, including how to rename/delete those .files without resorting to workarounds in the Finder then try http://freeengineer.org/learnUNIXin10minutes.html. Also BareBone’s free Textwrangler app can open hidden files.
  9. Chris

    1735 days ago

    First, the problem with MySQL in the terminal is definitely your path. But, what you might have missed is that after you edit your .bash_profile or .bashrc you have to run it again. To do this from the terminal

    source .bash_profile
    or
    . .bash_profile

    Second, rather than using TextEdit to handle unix text files you might consider something like TextWrangler or SubEthaEdit. TextWrangler can see hidden files for one, won’t add file extensions for another, and really is very nice for doing what you seem to be getting yorself into. All while having a not terribly steep learning curve.

    To see hidden files in the terminal when you do a list use
    ls -a
    and to delete them
    rm filename

    A decent primer for using the terminal in OS X can be found at macdevcenter
  10. Frode Danielsen

    1735 days ago

    Hm, you mentioned running OS X.3.9.. I remember using .bashrc earlier in OS X, and was a bit surprised to find it didn’t work in X.4, whereas .bash_profile does. Maybe you should use .bashrc in X.3.9..

    And as Chris mentions, these “profile”-files are only loaded when the shell is started. So you can’t have a Terminal window open, edit the .bashrc or .bash_profile and expect the changes to be active in the open Terminal window. Open a new window or reload it using Chris’ examples.

    I didn’t know much about Unix until I started using OS X when it first shipped. But I’m a geek and enjoy learning things like this, so I’m not scared from learning by doing – just use the man pages and try to understand the basic uses of commands before trying them and you should be alright :)
  11. Bruce Boughton

    1734 days ago

    “Initially I was dropping the .htaccess file that ships with Textpattern into each and every directory with an index.php as it’s starting point and it did solve the problem but that was getting a bit tiresome.”

    A .htaccess file affects its directory and its subdirectories so you shouldn’t need so many .htaccess files with the same command. This is more useful when you’re using them on a remote, shared server where you can’t edit apache.conf.

    For anyone looking to do something similar on Windows, I recommend WAMPServer.
  12. Lisa McMillan

    1733 days ago

    Hey John,
    I saw your article – awesome work! I had some old notes from a class I teach and thought I’d post them in case you could use them. After MUCH textile tweaking in txp I think I have something worth looking at. It’s long, but it might be a good reference.
    Hope it’s useful to you. Have fun with the server stuff! It’s so much fun. I love the command line LOL me=ubergeek.
    Have a boo at my notes about running a web server on mac os x
    I hope that isn’t comment spam… LOL it really might come in handy. I’m not just lookin for traffic ;-)
    Cheers man!
    Lisa
  13. Chris Winfield

    1733 days ago

    mod_rewrite and clean URLs with TXP using MAMP

    Little late now but it doesn’t take much effort to get MAMP to do what you want it to.
  14. John Oxton

    1733 days ago

    I tried that Chris but it didn’t work :(
  15. ati

    1732 days ago

    I could only agree Chris, but if someone wants to use terminal, than one should find httpd.conf, and uncomment (remove the # character) the following two lines:
    #LoadModule rewrite_module
    #AddModule mod_rewrite.c
    and restart apache, and write the .htaccess’ mod_rewrite lines.
    good luck with your new server setup!
  16. Phil Sherry

    1732 days ago

    A lot of this stuff gets covered in a certain book ;)
  17. Matthew

    1732 days ago

    Jon, there’s a groovy little app. you can use to show and hide hidden files easily. I use it when I need my .htaccess files to show up, and then I use it again to hide them when I’m done. I don’t like the mess. You can check it out here. Might be useful to someone that reads this.
  18. hink

    1732 days ago

    Interesting to read about how “MAMP” does it—my experience is purely Linux and Solaris.

    About .htaccess files and virtualhosts—if you ever get to the point where you want to cutomize access to a given directory, remember that it won’t take unless the specific directory is given an AllowOverride directive, (Apache default is, thankfully, restrictive) so if you wanted to do rewrites or redirects on specific directories, you would add

    < Directory "/path/to/docroot/specific_directory" >
    AllowOverride All
    < /Directory >

    inside the specific virtualhost “element”.

    You then define the specific rules in the .htaccess file. I know this might seem fairly obvious, but I have gotten calls from people freaking out because they can’t get .htaccess to
    work; a lot of people not comfortable with bash are overwhelmed by httpd.conf.

    Here’s the apache core reference, a handy bookmark to have: http://httpd.apache.org/docs/mod/core.html

    Oh yeah, another helpful tip, first thing you do should be backup httpd.conf!

    $ cp httpd.conf httpd.conf.default

    should do it—nothing more frustrating than having Apache tell you that you are stupid and refusing to start :P
  19. tom

    1730 days ago

    It is a good idea to have your Mac with custom firewall settings with ipfw, if you are going to run a server. Mac OSX or Mac OSX Server aren’t secure enough to actually run a server off of it. With minimal configuration you did just to get the thing working, I am pretty sure it can be exploited in someway or another in a few days. But other than, have fun! :)