Off course you’ll want to have Apache started automatically, just like the built in Apache would do. To do this we’ll need to write our own Startup item.
Open the Terminal and execute the following commands:
sudo mkdir Apache2
Then use your favourite editor (I like VI, but others like pico, it doesn’t matter) to create the following files into your newly created directory. Make sure you create them using sudo, so in the case of using VI the command would be: ‘sudo vi Apache2‘
The first file is called ‘Apache2′ and this is what it’s content should look like:
# Apache 2 Web Server startup item
# http://diymacserver.com
#
APACHECTL=”/usr/local/apache2/bin/apachectl”
. /etc/rc.common
StartService ()
{
if [ "${WEBSERVER:=-NO-}" = "-YES-" ]; then
echo “Please disable Personal Web Sharing and restart.”
else
if [ "${APACHE2:=-NO-}" = "-YES-" ]; then
echo “Starting Apache 2 Web Server”
$APACHECTL start
fi
fi
}
StopService ()
{
echo “Stopping Apache 2 Web Server”
$APACHECTL stop
}
RestartService ()
{
if [ "${WEBSERVER:=-NO-}" = "-YES-" ]; then
ConsoleMessage “Please disable Personal Web Sharing and restart.”
StopService
else
echo “Restarting Apache 2 Web Server.”
$APACHECTL restart
fi
}
RunService “$1″
This first file has the start, stop and restart commands which will be called after a reboot. Use:
to make the script executable.
You could test this script yourself after you created the next file using the command to start Apache2:
to stop Apache2 call:
and to just restart after a configuration change:
The second file is called ‘StartupParameters.plist’ and this is what it’s content should look like:
Description = “Apache 2 Web Server”;
Provides = (“Apache2″);
Requires = (“DirectoryServices”);
Uses = (“Disks”, “NFS”, “Network Time”);
OrderPreference = “None”;
}
This file determines in which order Apache2 should start in relation to other programs that are started on your Mac. Apache for instance requires a running DirectoryService (or DNS) and it will wait for it t ostart before it starts itself. We can have other programs wait for Apache to start by putting in their StartupParameter file ‘Requires = (“Apache2″);’
The last thing you’ll need to do is to add the line:
to the file ‘/etc/hostconfig‘. This file is used to control the start of all the startup items on your machine.
Note: If you have the firewall running on your server (which by itself is a good idea) the ports for the webserver are closed because you stopped the Apache1 program in the Sharing Preference pane. You can open up those ports again by going into the Sharing preference pane and click on the Firewall tab. Create a new setting and make it look like:

Click “OK” and everything should be running as intended.
Next step: Installing PHP



9:31 pm
#!/bin/sh
# Apache 2 Web Server startup item
# http://diymacserver.com
#
APACHECTL=”http://diymacserver.com/usr/local/apache2/bin/apachectl”
Change to
APACHECTL=”/usr/local/apache2/bin/apachectl”
I think I needed to change the script access from:
-rw-r–r– 1 root wheel 737 Feb 14 21:29 Apache2
to:
-rwxr-xr-x 1 root wheel 1241 Feb 14 21:31 Apache2
Otherwise
./Apache2 stop
./Apache2 start
didn’t work.
2:44 am
Yes, the file has to be executable or it won’t work. –V
9:52 pm
Thanks for instruction – worked like a champ!
5:58 pm
Works great OS 10.5
1:33 pm
@Marco, on Leopard your better of using the method described here: Starting Apache at boot on Leopard
7:20 pm
So, unless I’m missing something here, this script doesn’t do anything except hit the bottom call to “RunService” which does nothing since neither of the functions are called that. I tried.
?
10:33 pm
@Gee, no you are wrong, it will run apache. If it doesn’t there is something wrong with Apache or your config. Try running it from the command line.
10:31 pm
Thanks so much for this useful and excellent HOWTO. Two criticisms: 1) please don’t use forward and backward quotes in shell scripts, it makes it impossible to cut & paste them into my editor. Just use normal quotes: “”. 2) These are instructions for installing Apache on Mac OS X 10.4 Tiger, but from here you link to “Installing PHP”, which only has instructions for Leopard and Snow Leopard.
10:53 pm
@Jesse, I’m sorry about the quote problem I thought I eliminated most of them they are a remnant of a database upgrade gone wrong. I will correct it later. The instructions for Tiger have been deleted. I’m unable to support Tiger as well next to the other OS X versions (not enough hardware and such). But I’ll send you some instructions I’ve found in a backup.