Start a Web Server on Your Mac

Posted by admin on July 19th, 2014 filed in Mac

Sometimes you want to preview some web content locally on your Mac. Maybe you want to browse a backup of your home page or blog, or you want to run a local web application.

In previous versions of MacOS, you simply activated Personal Web Sharing in the control panel, and you could access the content of “Users/yourname/Sites” in the browser. This option is gone in the current MacOS. The web server however is still installed. You just need to know how to switch it on. :-)

  1. Open the Terminal.app and enter
    whoami

    Remember the response, this is your name that you need to replace in the path and the URLs below!

  2. Start the web server by entering the following command (it will ask for the admin password):
    sudo apachectl start
  3. Open the Finder and copy your web content to “/Users/name/Sites/”. It’s okay to create subfolders under “Sites”.
  4. Open your web browser (by default, Safari) and browse to your local web URL (note the tilde):
    http://localhost/~name/

    You should see an index page or folder listing.

  5. To access a file such as “/Users/name/Sites/page.html”, or a directory such as “/Users/name/Sites/subfolder/”, the URLs look as follows:
    http://localhost/~name/page.html
    http://localhost/~name/subfolder/

If the webpage in question contains PHP files, they may show up as PHP code in the web browser instead of being executed. In this case, you need to activate PHP, too.

  1. Open the Terminal and edit the following configuration file.
    sudo nano /etc/apache2/httpd.conf
  2. Search for lines that contain the word “PHP”. By default, they are commented out by a “#” character.
    #LoadModule php5_module libexec/apache2/libphp5.so
  3. Remove the “#” character and save the file.
  4. Restart the web server.
    sudo apachectl restart
  5. Reload the PHP page on your localhost in the web browser.

PHP is activated until you comment the line out again.

Comments are closed.