Remember the good old days when OS X ‘s Apache came with mod_bonjour and user sites were broadcast automagically. It can be a useful feature especially in a multiuser or lab setting. Well never fear, you can activate it once again. All you need to do is compile and install mod_bonjour for Apache on El Capitan.
What can mod_bonjour do? It can automate three types of tasks over your LAN:
- Broadcast a link to a default website for your computer.
- Broadcast links to other webpages which will be seen as paths of the default web address.
- Broadcast a link to a website for each individual user account on your computer .
All of which require zero configuration once the Apache module has been compiled, loaded, and a few directives set. Pretty nifty huh? So let’s get to it.
This tutorial was made on OS X 10.11.3 with Apache 2.4 and mod_bonjour 23. My text editor of choice is TextMate and it’s command line utility is “mate.” So when you see that command sutitute the text editor that you prefer. Also when you see the placeholder <-myusername-> insert your actual username.
One dependency we have to install is Xcode. You’ll need Xcode’s developer tools in order to follow along with this tutorial so go ahead and Install Xcode.
Temporarily Disable System Integrity Protection
The first thing we need to take into account is that we are intending to modify the /usr/libexec/apache2 directory. This directory is flagged as “restricted” thanks to El Capitan’s new rootless System Integrity Protection feature. We’ll need to temporarily disable SIP to complete this tutorial so read up and follow the instructions on How to work with SIP here.
Compile from source:
O.K. now that you’re back Let’s make a src directory for your source code in your /usr/local, set our ownership, and get into it.
sudo mkdir /usr/local/src sudo chown <-myusername->:admin /usr/local/src cd /usr/local/src
Now grab a tarball of mod_bonjour from Apple, open it up and cd into it. The code below downloads mod_bonjour version 23. Check here for the latest version and adjust your commands accordingly.
curl -O http://www.opensource.apple.com/tarballs/apache_mod_bonjour/apache_mod_bonjour-23.tar.gz tar zxvf apache_mod_bonjour-23.tar.gz cd apache_mod_bonjour-23
Now you can make and install.
make sudo make install
Success… mod_bonjour.so has been installed in:
Thats all for working with System Integrity Protection flagged directories so you can reactivate SIP whenever you’re ready.
Loading Apache Modules:
Now on to the final steps. Next we’ll need to open up out httpd.conf and make some changes.
Uncomment (remove the # symbol) from the following line:
LoadModule userdir_module libexec/apache2/mod_userdir.so
Now add the following line after the one you just uncommented:
LoadModule bonjour_module libexec/apache2/mod_bonjour.so
Next search for the following line in httpd.conf and uncomment it.
Go ahead and save the changes to your httpd.conf.
Editing http-userdir Config File:
At the start of this tutorial we asked “What can mod_bonjour do?” There were three items listed in the answer. Now that we have imported our httpd-userdir.conf we can make those things happen. Open httpd-userdir.conf in your text editor.
Now uncomment this line to allow for individual user.conf files to be imported.
And notice the line that reads: UserDir Sites. That line is what defines document root for the user web sites. What if we want it to be in a different directory? Simple enough, just change the line to this for example: UserDir Sites/mysite. That would make /Users/<-myusername->/Sites/mysite act as the document root for http://computer.local./~<-myusername->. Now take a look at:
<IfModule bonjour_module> RegisterUserSite customized-users </IfModule>
RegisterUserSite lets mod_bonjour know that we want to broadcast websites for user accounts on this machine and customized-users limits the broadcast to users who have modified the default index file in their Sites directory. That’s all for exploring mod_bonjour’s user sites.
What bout a default computer website? All we need to do is make the following changes to the IfModule conditional statement like so:
<IfModule bonjour_module> RegisterUserSite customized-users RegisterDefaultSite </IfModule>
Notice all that we added was RegisterDefaultSite in the If statement. Document root for the default site is set in /private/etc/apache2/httpd.conf and it maps to:
As if that was’t enough, mod_bonjour has one more trick up it’s sleeve. It can register resources (i.e. other websites) that map as subdirectories of the default computer website. For example: http://mycomputer.local/resource.
<IfModule bonjour_module> RegisterUserSite customized-users RegisterDefaultSite RegisterResource <-name-> <-path-> <-port-> <-protocol-> </IfModule>
The <-name-> is what will show up in Safari’s bookmarks or favorites bar. The <-path-> is the path of the website relative to document root. You don’t need to enter <-port-> or <-protocol-> since their defaults will suffice. So for example:
RegisterResource phpMyAdmin /phpmyadmin
Will map to:
W00T! We’ve covered all three ways you can broadcast websites via mod_bonjour.
This leads us to creating our user.conf file. Change directories in Terminal to /private/etc/apache2/users/ and write the new file with your text editor.
cd /private/etc/apache2/users/ sudo mate <-myusername->.conf
Once in your text editor, insert the following:
<Directory "/Users/<-myusername->/Sites/"> DirectoryIndex index.html Options Indexes MultiViews FollowSymLinks Includes AllowOverride All Require all granted </Directory>
Save the file.
Checking And Restarting Apache:
Almost there… Let’s check our Apache config with:
If the Syntax is all good it’s time to restart Apache:
sudo apachectl restart
At this point, let’s check the Apache error log to see if any errors are present when we start Apache.
open -a Console /private/var/log/apache2/error_log
I’ve found the following warning in the Apache error log:
[:warn] [pid 79] mod_bonjour: Cannot stat template index file '/System/Library/User Template/English.lproj/Sites/index.html'.
It’s telling us that mod_bonjour is looking into the OS X User template file to check the file status of an index.html in the ~/Sites directory. It makes sense. Mod_bonjour needs a baseline for comparison to see which users have customized their Sites directory. Since the file and path don’t exist Apache throws a warning in the log. If you want to remove the warning. Open up a terminal session as root and add an index.html at the path listed in the warning.
Oh sweet success!
The only thing that users on your computer and other computers on the local network need to do to see the site links is check the checkbox for showing Bonjour sites in the bookmarks menu or favorites bar of Safari. This setting is found in the Advanced section of Safari’s Preferences.