Setup Apache on OSX 10.10 Yosemite

With OSX 10.10 Yosemite, apple removed the ability to toggle Web Sharing from System Preferences. But worry not, apache 2.4.9 is installed and ready to go. Since there isn't a GUI to toggle the option anymore, we'll do this from Terminal.

Open terminal

cmd + space "terminal"

Apache Commands

Before we start up apache from terminal application, lets find out which version of apache is installed. It should be 2.4.9 as noted above.

httpd -v
# Server version: Apache/2.4.9 (Unix)
# Server built:   Sep  9 2014 14:48:20

These are the commands to start, stop, and restarts apache.

sudo apachectl start
sudo apachectl stop
sudo apachectl restart

Lets fire up apache by using the start command above and check to see if the webserver is working in the broswer by visiting 'loalhost' - you should see "It Works!" text.

Root Directories

At this moment, the default web directory is using a system level root under "/Library/WebServer/Document". We'd want to change this to use a user level root.

If OSX is a fresh install, the folder "Sites" will most likely be missing under the default user level root. Lets check and create "Sites" if it doesn't exist.

cd ~/Sites
# -bash: cd: Sites: No such file or directory
mkdir Sites

After creating the Sites folder, lets copy the index.html from system level web root directory to the Sites folder.

sudo cp /Library/WebServer/Documents/index.html.en Sites/index.html
ls Sites/
# index.html

username.conf

Now that we have the user level web root folder "Sites" created, we need to add and update a few files to enable apache to point localhost to use "Sites" as our web directory.

The setings/configurations for apache is located under "/etc/apache2/", find out what our current username is, create a "username.conf" under the "users" folder, and then update the permission of "username.conf"

cd ~/etc/apache2/
whoami
# username
sudo touch users/username.conf
sudo chmod 644 users/username.conf
ls users -lah
# -rw-r--r--   1 root  wheel   1B Nov  9 14:09 username.conf

Now, we want to open up username.conf with an editor of your choice and add the following content.

open username.conf

sudo vim users/username.conf

copy, paste, and save the content below.

DirectoryRoot "/Users/username/Sites/"
<Directory "/Users/username/Sites/">
  AllowOverride All
  Options Indexes MultiViews FollowSymLinks
  Require all granted
</Directory>

httpd-userdir.conf

Now that we added a "username.conf" to the users folder, we need to edit "http-userdir.conf" under the "extra" folder. Lets open up "httpd-userdir.conf" with an editor of your choice and uncomment an include.

open httpd-userdir.conf

sudo vim extra/httpd-userdir.conf

uncomment the following line

Include /private/etc/apache2/extra/httpd-userdir.conf

httpd.conf

Now that "username.conf" is added, we want to update httpd.conf to use the settings from "username.conf" instead of the settings within httpd.conf.

Lets open up "httpd.conf" with an editor of your choice again and make sure the following modules and includes are uncommented.

open httpd.conf

sudo vim httpd.conf

uncomment the following if they are commented.

LoadModule authz_core_module libexec/apache2/mod_authz_core.so
LoadModule authz_host_module libexec/apache2/mod_authz_host.so
LoadModule userdir_module libexec/apache2/mod_userdir.so

Restart Apache

That should be it, so lets restart apache and check it again on the browser.

sudo apachectl restart
open http://localhost

Update index.html

After restarting the server, its hard to tell if this is working as it should. Lets open up "index.html" under "Sites" and update the text from "It works!" to "My site!"

open index.html

vim ~/Sites/index.html

Update

<h1>It works!</h1>
<h1>My site!</h1>

restart apache

sudo apachectl restart
open http://localhost

Voila!

Revisions

  • updated 5/3/2015