Skip navigation
Currently Being Moderated

Setting up a local web server on OS X

The 905
Level 7 Level 7 (23,905 points)
VERSION 10  Click to view user tip history Published
Last Modified:  Nov 24, 2013 4:11 PM

Here is my definitive guide to getting a local web server running on OS X. This is meant to be a development platform so that you can build and test your sites locally, then deploy to an internet server. This User Tip contains instructions for configuring the Apache and PHP. I have another User Tip for installing and configuring MySQL and Perl.

 

Requirements:

  1. Basic understanding of Terminal.app and how to run command-line programs.
  2. Basic understanding of web servers.
  3. Basic usage of vi. You can substitute nano if you want.

Optional:

  1. Xcode is required for adding PHP modules.

 

Lines in bold are what you will have to type in at the Terminal.

Replace <your local host> with the name of your machine. Ideally, it should be a one-word name with no spaces or punctuation. It just makes life easier.

Replace <your short user name> with your short user name.

 

Here goes... Enjoy!

 

Lion no longer creates personal web sites by default. If you already had a Sites folder in Snow Leopard, it should still be there. To create one manually, enter the following:

mkdir ~/Sites

echo "<html><body><h1>My site works</h1></body></html>" > ~/Sites/index.html.en

 

PHP is not enabled in recent versions of OS X. To enable it, do:

sudo vi /etc/apache2/httpd.conf

 

Uncomment the following line: (In Lion this is on line 111. In Mountain Lion it is on line 117. In Mavericks it is at 118.)

#LoadModule php5_module libexec/apache2/libphp5.so

to

LoadModule php5_module libexec/apache2/libphp5.so

(if you aren't familiar with vi, just press 'x' over the '#' character to delete it. Then type ':w!' to save and then 'ZZ' to quit.)

 

While you are in /etc/apache2, double-check to make sure you have a user config file. It should exist at the path: /etc/apache2/users/<your short user name>.conf. That file may not be created in Lion and if you upgrade to Mountain Lion, you still won't have it. It does appear to be created when you create a new user in Mountain Lion. If that file doesn't exist, you will need to create it with:

 

sudo vi /etc/apache2/users/<your short user name>.conf


Use the following as the content:

<Directory "/Users/<your short user name>/Sites/">

    Options Indexes MultiViews

    AllowOverride None

    Order allow,deny

    Allow from all

</Directory>

 

In vi, press <esc> and then ZZ to save and quit.

 

If you want to run Perl scripts, you will have to do something similar:

 

Uncomment the following line: (In Lion this is on line 110. In Mountain Lion it is on line 116. In Mavericks it is on 117.)

#LoadModule perl_module libexec/apache2/mod_perl.so

to

LoadModule perl_module libexec/apache2/mod_perl.so

 

Then, in /etc/apache2/users/<your short user name>.conf change the line that says:

    Options Indexes MultiViews

to:

    AddHandler perl-script .pl

    PerlHandler ModPerl::Registry

    Options Indexes MultiViews FollowSymLinks ExecCGI

 

Now you are ready to turn on Apache itself.

 

In Lion, do the following:

To turn on Apache, go to System Preferences > Sharing and enable Web Sharing.

 

NOTE: There appears to be a bug in Lion for which I haven't found a workaround. If web sharing doesn't start, just keep trying.

 

In Mountain Lion, the Web Sharing checkbox in System Preferences > Sharing is gone. Instead, do the following:

sudo launchctl load -w /System/Library/LaunchDaemons/org.apache.httpd.plist

 

In Safari, navigate to your web site with the following address:

http://<your local host>/

 

It should say:

 

It works!

Now try your user home directory:

http://<your local host>/~<your short user name>

 

It should say:

 

My site works

Now try PHP. Create a PHP info file with:

echo "<?php echo phpinfo(); ?>" > ~/Sites/info.php

 

And test it by entering the following into Safari's address bar:

http://<your local host>/~<your short user name>/info.php

 

You should see your PHP configuration information.

 

If you want to setup MySQL, see my User Tip on Installing MySQL.

 

If you want to add modules to PHP, I suggest the following site. I can't explain it any better.

 

If you want to make further changes to your Apache system or user config files, you will need to restart the Apache server with:

sudo apachectl graceful

Apple does not necessarily endorse any suggestions, solutions, or third-party software products that may be mentioned in this User Tip. Apple encourages you to first seek a solution at Apple Support. Any links in this user tip are provided as is, with no guarantee of the effectiveness or reliability of the information. Apple does not guarantee that these links will be maintained or functional at any given time. Use this user tip at your own discretion.
Comments (1)

Actions

Bookmarked By (33)

More Like This

  • Retrieving data ...
This site contains user submitted content, comments and opinions and is for informational purposes only. Apple disclaims any and all liability for the acts, omissions and conduct of any third parties in connection with or related to your use of the site. All postings and use of the content on this site are subject to the Apple Support Communities Terms of Use.