Off course you’ll want to have Apache started automatically, we will use the new launchd system in Leopard to accomplish this.
Create a file called ‘com.diymacserver.apache2.plist‘ in the directory ‘/System/Library/LaunchDaemons/‘. The content of the file should look like:
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.diymacserver.apache2</string>
<key>ProgramArguments</key>
<array>
<string>/usr/local/apache2/bin/apachectl</string>
<string>start</string>
</array>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
To load and activate the .plist file and to make sure that it is started after a reboot you’ll need to load the plist file by using the command:
You can see if it loaded correctly by using the command:
This will show you all the active plist files.
You can see if everything went correctly by trying to start it using:
Next step: Installing PHP



1:06 am
When I tried to load the plist I received the following,
launchctl: opendir() failed to open the directory
nothing found to load
Any ideas?
5:13 am
I guess the error message speaks for it self, did you make an error in the path, did you use sudo, etc… It can’t open the file therefore something is wrong in your commandline.
2:19 am
I admit I’m not very versed in the Mac launchctl process. (I’m not sure I like it.)
Following these startup steps I can use launchctl to ‘load’ the plist — which starts the server. Using the ’start’ subcommand issues no errors. But I observe that the ’stop’ subcommand does not stop the server.
Is this because of the ’start’ command here in the plist? Is it required to ‘unload’ the plist to shutdown the server?
7:56 am
You are correct, the launchctl program is configured to keep the Apache server running. If you like to have more control you could use the ‘old’ method as described here: http://diymacserver.com/installing-apache/starting-apache-at-boot/
It will still work on Leopard but it will be fased out at some point in the future as indicated by Apple.
8:44 pm
sudo launchctl load /System/Library/LaunchDaemons/com.diymacserver.apache2.plist
launchctl: Dubious ownership on file (skipping): /System/Library/LaunchDaemons/com.diymacserver.apache2.plist
nothing found to load
I don’t like launchctl as it doesn’t like my save files!
10:08 pm
Your files should be owned by root! Make sure that is the case.
5:26 pm
The biggest concern I have with using launchd with apache2 as show is that the httpd processes are ran as root – not a separate account (e.g., www, web). Running apache as root is really a bad idea (read security risk).
Using the .plist username property does not appear to work as it does with my other services.
UserName
www
This is commonly done, just need to find a reference to how to do it with OSX.
5:32 pm
The username for the apache processes is defined in the httpd.conf using:
User _www
Group _www
If you are using Leopard then use “_www” on Tiger use “www”
5:41 pm
Thanks Richard is was under my nose the whole time.
6:05 pm
Is this *starting* apache as root or running apache as root? my phpinfo still says its running under WWW … should i be concerned and still modify my plist file again?
6:08 pm
Apache will eventually run as the user/group configured in your httpd.conf. The starting process will be started as root but apache will spawn the httpd deamons as the configured user and die off…
1:13 pm
Richard,
Tried to start Apache (DIY version) but got following messages in log :
USER=root ; COMMAND=/bin/launchctl start com.diymacserver.apache2
Aug 11 14:06:45 macs-mac-mini com.apple.launchd[1] (com.diymacserver.apache2[68546]): Exited with exit code: 1
Any clue ?
Luc
10:35 pm
Hi Richard,
After recompiling I tested again but got some errors when trying to write to log :
(2)No such file or directory: httpd: could not open error log file /usr/local/apache2/logs/idsmac.net-error_log.
Unable to open logs
7:15 am
Luc, this should not be the logfile location as the apache user will not have access to write there. It will work but no logging will take place. All logs should be placed in /var/log/
11:35 am
Hi Richard,
the mentionned logfile is the one which is the definition of DIYMacServer layout in the vhost file.
Is this not correct ?
12:33 pm
No it is not, if you look in the DIYMacServer layout is says:
`localstatedir: /var
logfiledir: ${localstatedir}/log/httpd`
Which means logfiles should end up in /var/log/httpd which are write-able.
1:13 pm
Hi Richard,
Sorry to bother you but the logfile you mention is indeed in the /var/log/httpd directory.
The comment in the logfile is:
(2)No such file or directory: httpd: could not open error log file /usr/local/apache2/logs/idsmac.net-error_log.
Unable to open logs
But the logfile are the one in vhost configuration :
ErrorLog logs/site1.com.error_log
CustomLog logs/site1.com.access_log combined
Any help is welcome.
PS If I ping to idsmac.net I arrive to 127.0.0.1 as mentionned in the Hosts file. But the webpage does not show
1:28 pm
The logfile in vhost are relative to the logdirectory configured in httpd.conf. I think there is a problem in that configuration.
7:28 pm
At least in the current version of Leopard (10.5.8 at time of this posting), this isn’t working. It seems to be launching BEFORE OS X has finished starting up all its services. I added a “/bin/sleep 20″ to apachectl and that worked.
I’m sure there’s a better way to do that though then adding a 20 second sleep to apachectl. Thoughts?
10:34 pm
@Gee, it will run apache, this is working on my machines. If it doesn’t there is something wrong with your Apache or your config. Try running it from the command line.
10:43 pm
Hi Richard,
Works fine from the command line. Here’s what’s happening:
- launchd does start the apache process on boot, its in the launchd list. However, httpd is not running.
- If I launchctl unload, and reload the plist, it works.httpd is now running.
- That leads me to believe there’s some dependency that’s not loaded yet during boot when launchd loads the apache plist. I tested this theory by adding a 10 second sleep to the top of apachectl. Once I did that, httpd did start on boot when launchd asked it to.
-GB
8:51 am
@Gee, I think this is specific to your setup. I’ve used this on many machines and I’ve never encountered your problem. There might be something else interfering, I think checking your logfiles on why apache is failing to start might give ou some insight in the why.
9:57 pm
I get this when i try to do that command:
sudo launchctl load /System/Library/LaunchDaemons/com.diymacserver.apache2.plist
launchctl: propertyList is NULL
launchctl: no plist was returned for: /System/Library/LaunchDaemons/com.diymacserver.apache2.plist
launchctl: no plist was returned for: /System/Library/LaunchDaemons/com.diymacserver.apache2.plist
nothing found to load
Any help??
9:59 pm
@Matthew, please check that the plist file is created correctly and that there are no strange characters in it. Please try to use vi when possible. If not possible send the plist to info at this domain.
1:43 am
Richard,
I think you need the -w switch after load in the command.
sudo launchctl load -w \ /System/Library/LaunchDaemons/com.diymacserver.apache2.plist
This removes the key from the property list.
To turn it off use unload instead of load.
Best,
TimRob
sudo launchctl unload -w \
/System/Library/LaunchDaemons/com.diymacserver.apache2.plist
1:12 pm
TimRob, why would I want to use ‘-w’ which problem does this solve?
8:30 pm
Sorry Richard, I should have directed my reply to Matthew and those having problems using the command as written. If you’re getting the ‘Nothing Found To Load” error, try using the -w switch.
Mainly what this does is write a key in to the .plist.
launchctl load -w writes the key
Disabled
(Note, It may actually just remove the key if it exists depending on what version of OS X)
Launchctl unload -w changes the key to:
Disabled
man launchctl for more.
Also make sure you don’t have a typo in the name of the .plist file
Best,
TimRob