Matt Kelsey

Technical notes, thoughts and examples

Using Symlinks and Virtual Hosts to Serve Pages Outside of Sites Directory Using Apache on the MAC

I like keeping all my code and websites in a single “Code” directory on my machine, but it isn’t straightforward, from what I can tell, to get Apache on the MAC to serve up pages anywhere outside of the default “Sites” directory that is created when you enable “Web Sharing” on the MAC.
After doing a lot of searching, I finally found a solution that works for me.

I doubt this is the only solution, or even the “proper” solution, but it seems to work.

Below are all the steps I took in order to serve pages out of directories other then those in the “Sites” directory. For these steps, I will assume there is an existing directory of “~/Code/MyWebSite” that has a simple “Hello World” index.html file in it.

  • Enable Web Sharing on the MAC by going to System Prefrences –> Sharing –> Check Enable Web Sharing

  • Edit your username.conf file located in /private/etc/apache2/users and add the “FollowSymLinks” directive

console
1
2
3
4
5
6
<Directory "/Users/yourUserName/Sites/">
        Options Indexes MultiViews FollowSymLinks
        AllowOverride None
        Order allow,deny
        Allow from all
    </Directory>
  • Edit the /private/etc/apache2/httpd.conf file and make sure the line under “# Virtual hosts” is not commented out, like so:
console
1
Include /private/etc/apache2/extra/httpd-vhosts.conf
  • Edit the /private/etc/apache2/extra/httpd-vhosts.conf file and add:
console
1
2
3
4
5
6
7
8
<VirtualHost *:80>  
        <Directory /Users/yourUserName/Sites/MyWebSite.com>
            Options +FollowSymlinks +SymLinksIfOwnerMatch
            AllowOverride All
        </Directory>
      DocumentRoot /Users/yourUserName/Sites/MyWebSite
      ServerName MyWebSite.local
    </VirtualHost>
  • Edit the /etc/hosts file and add this at the top:
console
1
127.0.0.1 MyWebSite.local
  • Make a Symlink to link your Code directory to one in the Sites directory.
console
1
ln -s ~/Code/MyWebSite ~/Sites/MyWebSite
  • Run apachectl to check config file syntax.
console
1
sudo apachectl -t
  • Restart Apache
console
1
sudo apachectl restart
  • Open a browser and go to MyWebSite.local and you should see the results.

Comments