November 6th, 2009
Apache configuration is one of the stumbling blocks that keeps a lot of web designers from using programming languages like PHP. This page is geared toward web designers with little to no previous knowledge of how to use terminal or configure Apache.
When making websites and testing them locally you won’t be able to use server side programming languages like PHP. You also can’t use absolute paths to files or images which is a major drag. If you have a mac with OS X though you have the Apache web server already installed on your computer. You just need to turn it on and do some configuration and you can develop your sites locally instead of uploading your files to a remote server every time you want to see a change.
This tutorial walks you through making changes to your system’s default installation of Apache, it’s been tested on a few computers running OSX 10.3, 10.4, & 10.5 and everything has worked great. This does not however mean we guarantee it’ll work for you.
Use this tutorial at your own risk: We’re not in any way responsible for any errors in this text, or any damage that may result from misunderstanding the tutorial.
If you aren’t very interested in learning about how to configure Apache and you’d rather it just worked, you may want to check out MAMP and VirtualHostX.
Still with us? If so, get a cold drink and get comfortable because the one time setup steps can take a bit of time if you’re new to this stuff.




If you can see this, it means that the installation of the Apache web server software on this system was successful…

/Users/*yourusername*
*yourusername*:~ *yourusername*$
Because Leopard upgraded to Apache 2 and PHP 5, your httpd.conf file is in a different location depending on your version of OS X.
sudo pico /etc/apache2/httpd.conf
sudo pico /etc/httpd/httpd.conf
We’re using the UNIX command sudo, which means ‘super user do’. Since this is a protected file on your computer that you don’t want just anyone editing, sudo lets you edit it, provided you type in your system password and hit enter.

#LoadModule perl_module libexec/httpd/libperl.so
#LoadModule php4_module libexec/httpd/libphp4.so
LoadModule hfs_apple_module libexec/httpd/mod_hfs_apple.so
LoadModule bonjour_module libexec/httpd/mod_bonjour.so
LoadModule perl_module libexec/httpd/libperl.so
LoadModule php4_module libexec/httpd/libphp4.so
LoadModule hfs_apple_module libexec/httpd/mod_hfs_apple.so
LoadModule bonjour_module libexec/httpd/mod_bonjour.so
AddModule mod_php4.c
AddModule mod_perl.c
Next, we’ll include a file that defines our virtual hosts (our websites).
#Virtual Hosts
#Include /private/etc/apache2/extra/httpd-vhosts.conf
#Virtual Hosts
Include /Users/*yourusername*/Sites/my-vhosts.conf
#Include /private/etc/httpd/users/*.conf
Include /Users/*yourusername*/Sites/my-vhosts.conf
we’re pointing to a file in your Sites/ directory instead of where they suggest because it’ll be easier for us to edit it with a regular text editor in the future and it just makes sense to have the file there because this is where the rest of your websites will live. Also don’t forget to change *yourusername* to your actual user name.
####### Override some of the default httpd.conf settings #######
## listens to 80 which is your local computer ##
NameVirtualHost *:80
## Changes default permissions for directories that Apache has access to ##
<Directory />
Options ExecCGI FollowSymLinks
AllowOverride all
Allow from all
</Directory>
####### virtual hosts #######
NameVirtualHost test:80
<VirtualHost test:80>
DocumentRoot /Users/*yourusername*/Sites/test
ServerName test
RewriteEngine on
</VirtualHost>
sudo pico /etc/hosts
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
127.0.0.1 test
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
127.0.0.1 test
sudo apachectl graceful
You should now be able to see your website by going to http://test/ in any web browser on the computer you setup. You can even simply type test into the browser and get the page.
If you are still having problems let us know and maybe we can help
If it did work but there’s something that you thought could be clearer, let us know how we can improve the tutorial.
NameVirtualHost test:80
<VirtualHost test:80>
DocumentRoot /Users/*yourusername*/Sites/test
ServerName test
RewriteEngine on
</VirtualHost>
NameVirtualHost newsite:80
<VirtualHost newsite:80>
DocumentRoot /Users/*yourusername*/Sites/newsite
ServerName newsite
RewriteEngine on
</VirtualHost>
NameVirtualHost test:80
<VirtualHost test:80>
DocumentRoot /Users/*yourusername*/Sites/test
ServerName test
RewriteEngine on
</VirtualHost>
sudo pico /etc/hosts
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
127.0.0.1 test
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
127.0.0.1 test newsite
Follow the instructions above for Restarting Apache
You should now be able to see your website by going to http://newsite/ in any web browser on the computer you setup. You can even simply type newsite into the browser and get the page.
If you are still having problems let us know and maybe we can help
If it did work but there’s something that you thought could be clearer, let us know how we can improve the tutorial.
Thanks to Justin Blecher for getting us started with vhosts and Jessica Chen for letting us use her computer as a guinea pig.
To keep up with our web development articles subscribe to the mym-dev RSS feed