Your Mac as a Server

Mac's come with the ability to serve static content out of the box, and there are simple options to get fancier with dynamic content. Here are some examples.

Static Content

Built-In

Your mac has a super-easy way to server static content out of the box, without installing anything. This makes use of the fact that Mac's come with Python, and Python's standard library contains the super-handy SimpleHTTPServer module.

$ cd /path/to/static/html
$ python -m SimpleHTTPServer 8000

After running the above command, you'll see something like Serving HTTP on 0.0.0.0 port 8000 ... - you're good to go! Head over to http://localhost:8000 to see what you find!

The beauty of this is that you can run this from any directory/location on your Mac, even off of shared network drives - as long as your Mac can read the files.

$ cd /Volumes/SomeNetworkDrive/path/to/html
$ python -m SimpleHTTPServer  # awww, yeah

But wait, there's more! Mac's usually come bundled with Twisted as well! You can run a (supposedly production-grade) static file server using Twisted with this command:

$ twistd -n web -p 8888 --path /path/to/html

BUT WAIT, there's even more! You can do this in Ruby also!

$ ruby -run -e httpd /path/to/html -p 8888

With some installation...

If you have NodeJS installed, you can find an equally simple static file server.

# Install this server script somewhere:
$ curl https://gist.github.com/rpflorence/701407/raw/cef3e3af742d4a3d138685eff62007b1fac437e1/static_server.js \
   > /path/to/somewhere/static_server.js
$ cd /path/to/somewhere
$ node static_server.js 8888

Ruby options?

Mavericks even comes with the Gem package manager, so you can use something like this as well!

Dynamic Content

Serving dynamic content is, of course, harder.

If you are on Mavericks, you actually have php 5.4+ installed. This means PHP's built-in web server will work!

$ cd /path/to/php/files
$ php -S localhost:8888

Of course, if you need to install things like mcrypt, PDO, GD or other PHP modules which might not come with Mac OS, you should probably get a virtual machine going and keep your Macintosh clean of such things. Avoiding the pain of configuring "server stuff" on your Macintosh is worth it.

I need something more complex!

If you need something more complex (chances are, you do), get a virtual machine.

Vagrant is great for easy virtual machines. Don't forget to also install VirtualBox.

The ability to cheaply test, create, break, discard, upgrade or bloat (with extra software/processes) without affecting your host OS makes the use of Virtual Machines amazingly beneficial to your productivity and well-being.

comments powered by Disqus