34

I have a page that I need to serve on localhost for the fonts to appear properly.

What is the easiest way to do this (on a Mac)?

66

Since OS X comes with python2 I suggest:

$ python -m SimpleHTTPServer
Serving HTTP on 0.0.0.0 port 8000 ...
  • Python serves itself via it’s own built in facilities. PHP on the other hand runs as an Apache module. I posted an answer suggesting you should use MAMP if you are looking for a quick and simple way to setup the Mac equivalent of a LAMP stack which includes PHP as well. – JakeGould Feb 15 '15 at 7:37
  • MAMP works, but I probably should have written 'quickest' rather than easiest, as a one-liner on the CLI seems preferable to dragging files around. But I think your answer is useful for anyone who wants the full stack. – Zachary Russell Heineman Feb 15 '15 at 9:58
  • @JakeGould PHP does not need to run as a Apache (or nginx for that matter) module. Since PHP 5.4 there's a built in server. As other anwer mentions, php -S does the trick. And python's "built in facilities" is kinda misleading, as -m SimpleHTTPServer is running a module from the default library, which is quite different from built in functions. – h7r Feb 15 '15 at 12:31
  • @ZacharyRussellHeineman Wouldn't it be http://localhost:8000, not :8080? Or am I missing something? – wchargin Feb 15 '15 at 16:36
  • 7
    If you have python3 installed it's even easier: $ python3 -m http.server. – Bakuriu Feb 16 '15 at 13:06
32

As of PHP 5.4.0, the CLI SAPI provides a built-in web server. More details in the official PHP documentation:

  1. Open terminal.
  2. Navigate to the folder (directory) containing the file you want to serve.
  3. php -S localhost:8080
  4. Go to http://localhost:8080 in the browser.
  • This is quite a bit slower than the Python version for some reason. – 0942v8653 Feb 16 '15 at 0:02
9

In older versions of OS X, there was a Preference Pane (System Preferences → Sharing → Web Sharing) that allowed you to easily enable the built-in Apache web server. Once enabled, all you had to do was place the file to be served in a directory named Sites inside your home directory, and then you could fetch http://localhost/~yourusername/filename.html.

Sadly, the Preference Pane was removed in OS X 10.8. You can either install a replacement for it or activate it using a shell command.

4

There are a lot of different ways to do this in Mac OS X. But to me, the simplest way—conceptually and visually—is via a graphical user interface like what MAMP offers. The non-Pro version of MAMP is 100% free and works great for basic usage like this:

MAMP installs a local server environment in a matter of seconds on your Mac OS X computer, be it MacBook or iMac. Like similar packages from the Windows- and Linux-world, MAMP comes free of charge, and is easily installed. MAMP will not compromise any existing Apache installation already running on your system. You can install Apache, PHP and MySQL without starting a script or having to change any configuration files! Furthermore, if MAMP is no longer needed, just delete the MAMP folder and everything returns to its original state (i.e. MAMP does not modify any of the "normal" system).

So basically you just download MAMP and it is installed like any other application, but it will allow you to create a Mac equivalent of a LAMP stack (Linux Apache MySQL PHP) on your desktop. And if all you want to do is serve a simple page via localhost, just do this:

  1. Download and install MAMP.
  2. Start MAMP.
  3. Once started it will create an Apache web server on localhost:8888.
  4. Now if you head over to this path /Applications/MAMP/htdocs, any document you place in there will be accessible via localhost:8888. So if you adjust or add an index.html or index.php file, that will be the new main file you get when you go to localhost:8888.
  5. Or if you wanted to create a new file/folder just add that in there and it’s accessible as being served via a web server.
  6. If somehow localhost:8888 is a bit obscure a URL for you to use, then just open up MAMP’s preferences and set the ports to the default Apache/MySQL ports of 80 and 3306. You will need to enter your administrator password to change the ports to these more standard ports, but after that is done, you can reach the content directly via localhost.
1

Someone looking for "the easiest" solution probably wants to use a GUI rather than the command line. Older versions of OS X had a checkbox in the Sharing Preference Pane that enabled the web server. You can replicate that functionality with the freeware https://clickontyler.com/web-sharing/

1

The free Anvil App is how I do this. It integrates with Hammer.app to display pre-processed sites, but you can also just add a static site folder. Sites can be accessed via a .dev site extension, which the browser sees properly to load external fonts, code, etc.

http://anvilformac.com

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.