9

How do I setup a localhost using the "Sites" folder on my Mac?

1
  • 1
    For clarification, this is enabling http://localhost/~USERNAME/ to serve your own ~/Sites/ folder.
    – Dave X
    Aug 6, 2017 at 3:52

2 Answers 2

11

The web sharing checkbox was removed in 10.8, but sudo apachectl start also enbales the launchd agent permanently (see cat /usr/sbin/apachectl). To make http://localhost/~username/ work, you can save something like this as /etc/apache2/users/username.conf:

<Directory "/Users/username/Sites/">
    Options Indexes MultiViews
    AllowOverride All
    Order allow,deny
    Allow from all
</Directory>

If you want to direct localhost to ~/Sites/, you can change DocumentRoot in /etc/apache2/httpd.conf.

You can add a virtual host by uncommenting Include /private/etc/apache2/extra/httpd-vhosts.conf in /etc/apache2/httpd.conf, adding 127.0.0.1 testsite.dev to /etc/hosts, and adding this to /etc/apache2/extra/httpd-vhosts.conf:

<VirtualHost *:80>
    DocumentRoot "/Users/username/Sites/testsite/"
    ServerName testsite.dev
</VirtualHost>

sudo apachectl restart applies changes.

3
  • I wonder, is it possible to change location for this folder /Users/username/Sites/ to new one? for example /Users/username/PHP/ Aug 10, 2015 at 13:42
  • I needed to also uncomment two lines in /etc/apache2/httpd.conf: LoadModule userdir_module libexec/apache2/mod_userdir.so and Include /private/etc/apache2/extra/httpd-userdir.conf.
    – Dave X
    Aug 5, 2017 at 16:22
  • I also needed to uncomment a line in /etc/apache2/extra/httpd-userdir.conf: Include /private/etc/apache2/users/*.conf, otherwise the per-user /etc/apache2/users/*.conf files are not read. -- Hat tip coolestguidesontheplanet.com/…
    – Dave X
    Aug 6, 2017 at 3:32
6

Open System Preferences » Sharing, and enable Web Sharing.

On Mountain Lion Web Sharing has been removed from preferences, so use sudo apachectl start to start Apache.

You'll find the URLs specified there. For example, my Sites folder /Users/danielbeck/Sites is accessible as http://10.0.1.2/~danielbeck/.

2
  • Thank you, but I wanted to get local php working such as WordPress. I have seen many posts, but none about the "Sites" folder.
    – david
    Dec 24, 2010 at 0:41
  • 1
    @david In that case, your question is lacking your actual requirements. You need to configure Apache to support PHP, just as on any other system. httpd.conf is in /etc/apache2, and while it's been a while since I worked with it, you might have success by uncommenting the line containing php5_module, and toggling Web Sharing to restart Apache.
    – Daniel Beck
    Dec 24, 2010 at 0:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .