Apache is packaged with OSX so it's ready to go more or less out of the box. In versions of OSX prior to 10.8 there was the option to turn this on via Sharing in System Preferences by ticking the Web Sharing option. However in OSX 10.8 this option has been removed. Following are the steps for getting things going in 10.8 and 10.7
OSX 10.8
Open terminal then execute the following:
(NB: You will be asked for an admin password.)
> sudo apachectl start
> sudo defaults write /System/Library/LaunchDaemons/org.apache.httpd Disabled -bool false
The above will start Apache then make it so it automatically starts when you reboot
OSX 10.7
Open System Preferences > Sharing > Check Enable Web Sharing
Open terminal (aka bash) then execute the following:
> sudo vim /etc/apache2/users/`whoami`.conf
Once the editor is up put in the following configuration making sure to change username
with your user name. If you
are unsure of what your user name is then execute whoami
from bash.
<Directory "/Users/username/Sites/">
Options Indexes MultiViews FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>
We're going to keep the configuration fairly simple but allow for you to easily make more config files without having
to use sudo
all the time. As a part of the standard setup for OSX you get a folder called Sites in your home
directory. If it's not there then create it and apply the appropriate permissions to the directory for Apache.
> mkdir ~/Sites
> chnmod o+x ~/Sites
The second command is worth applying yo your Sites folder to ensure you have the right permissions required by Apache otherwise you will get Forbidden errors with visiting your site. In fact every directory leading to the root of your will need to have these permissions otherwise you'll get a Forbidden error.
So you have a common place for your website config files create a conf folder in site (ie mkdir ~/Sites/conf
) then do
the following:
> sudo vim /etc/apache2/httpd.conf
Go to the end of the file and the following line (substituting your username for myusername
) then save it:
Include /Users/myusername/Sites/conf/*.conf
You're now ready to add your website config:
> vim ~/Sites/conf/my-new-website.conf
Then put in the following changing my-user-name
, my-website-root
and my-website.local
appropriately:
<VirtualHost *:80>
ServerName my-website.local
DocumentRoot /Users/my-user-name/Sites/my-website-root/
<Directory /Users/my-user-name/Sites/my-website-root>
Options +FollowSymlinks +SymLinksIfOwnerMatch
AllowOverride All
</Directory>
</VirtualHost>
If the source is in another directory then you can symlink to it. Be aware that all folders leading to the root of the
other directory will need to have the appropriate permissions (ie apply chmod o+x
to each directory). To do the
symlink do the following:
ln -s ~/Site/my-web-site ~/Code/path/to/source
In order for you to view your local website through your browser using my-website.local
you will need to add a
hosts file entry.
> sudo vim /etc/hosts
Once the editor opens go to the end of the file and add the following lines
127.0.0.1 my-website.local
::1 my-website.local
Note the first line is IPv4 and the second is IPv6. The IPv6 is required because without it each visit to your site will be delayed by OSX trying to find the IPv6 version then timing out.
Now you've done doing the configuration you'll need to restart Apache. Should you make any changes your .conf
file
then you will need to restart Apache. You can do the restart as follows:
> sudo apachectl restart
Now you're done :)