The easiest way to serve a page to localhost on OS X
Solution 1:
Since OS X comes with python2 I suggest:
$ python -m SimpleHTTPServer
Serving HTTP on 0.0.0.0 port 8000 ...
Solution 2:
As of PHP 5.4.0, the CLI SAPI provides a built-in web server. More details in the official PHP documentation:
- Open
terminal
. - Navigate to the folder (directory) containing the file you want to serve.
php -S localhost:8080
- Go to
http://localhost:8080
in the browser.
Solution 3:
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.
Solution 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:
- Download and install MAMP.
- Start MAMP.
- Once started it will create an Apache web server on
localhost:8888
. - Now if you head over to this path
/Applications/MAMP/htdocs
, any document you place in there will be accessible vialocalhost:8888
. So if you adjust or add anindex.html
orindex.php
file, that will be the new main file you get when you go tolocalhost:8888
. - 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.
- 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 vialocalhost
.