35

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)?

Giacomo1968
  • 53,069
  • 19
  • 162
  • 212

7 Answers7

68

Since OS X comes with python2 I suggest:

$ python -m SimpleHTTPServer
Serving HTTP on 0.0.0.0 port 8000 ...
Rnhmjoj
  • 878
  • 8
  • 8
  • 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. – Giacomo1968 Feb 15 '15 at 07: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 09: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
  • Sorry, typo. Deleted to avoid confusion... – Zachary Russell Heineman Feb 15 '15 at 17:59
  • @h7r Oh! That I did not know at all. All of the PHP development I have been doing has been with legacy code in the PHP 5.3 realm and if I ran that code in PHP 5.4 it was just via Apache anyway so I would not have known that. Very good tip! – Giacomo1968 Feb 16 '15 at 00:13
  • 8
    If you have python3 installed it's even easier: `$ python3 -m http.server`. – Bakuriu Feb 16 '15 at 13:06
34

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.
Giacomo1968
  • 53,069
  • 19
  • 162
  • 212
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.

200_success
  • 1,231
  • 1
  • 11
  • 26
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.
Giacomo1968
  • 53,069
  • 19
  • 162
  • 212
3

Use your terminal, cd into your project folder then type:

npx http-server

That should start up a server for you and will be available on: http://127.0.0.1:8080 or follow the instructions on your terminal.

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/

Foo Bar
  • 448
  • 1
  • 6
  • 20
0

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