Blog's from the Bush
The ramblings of a lost technocrat...

Changing the user Apache runs as…

| No TrackBacks | Bookmark and Share

I’m normally quite happy with the standard Apache setup on my Mac’s and the simplicity with which MySQL installs makes it still the easiest platform to setup a development machine on. In fact we can do nearly all of our web app development on stock standard Mac’s as it gives a good base platform that compares favourably with most of our clients servers at various hosting providers.

For most of the software we develop is happy running under the default User and Group ( _www:_www ) for Apache, recently though we had to upgrade a third party extension that runs a few shell exec routines. In our Mac based world this presented a few problems as the _www user doesn’t normally come with a home directory and therefore no way to setup things like, ssh keys.

(Please don’t undertake these changes if you aren’t aware of how this changes the security of your computer.)

A Small Change

Apache on Mac OS X has a series of configuration files, first, it’s main configuration is found in /private/etc/apache2 and is called httpd.conf. This main file includes a variety of sub-configuration files based on the configuration of you Mac, things like /private/etc/apache2/extra/httpd-userdir.conf for user directories. You can find all of them at the end of the main httpd.conf file. As always when working with these files make a backup first, making one after you’ve made a mess is not the way to do things.

Editing the main httpd.conf file (using BBEdit or vi) allows you to change the user that Apache runs as. At around line 150 in the main configuration file, you will find:

# If you wish httpd to run as a different user or group, you must run
# httpd as root initially and it will switch.  
#
# User/Group: The name (or #number) of the user/group to run httpd as.
# It is usually good practice to create a dedicated user and group for
# running httpd, as with most system services.
#
User _www
Group _www

For our purposes all we had to do was stop Apache (either via unchecking “Web Sharing” in the Sharing Preferences or through the terminal command apachectl stop) and replace the User with the short name of our user account and the group with staff

User craig          <- this is my short user name don't use it :->
Group staff

Once the change was made and the httpd.conf file saved, all that’s left is to restart Apache. (Again, either via checking “Web Sharing” in the Sharing Preferences or through the terminal command apachectl start)

A nice advantage of this small change is, that apart from picking up values from the user account, things like ownership of files isn’t an issue in the ~/Sites folder anymore.

Troubleshooting

If Apache fails to restart check your user’s short name is correct and the group name is correct, these are case sensitive values.

How can you tell if Apache failed to restart?

In Sharing Preferences if there is a typo in the Apache configuration file (eg. you mis-spelled the username, or used an uppercase letter for the first letter of the Group) the “Web Sharing” will un-tick itself immediately. The most obvious indicator is the little status light which is normally green when web sharing (ie. Apache) is running correctly. It should look like this:

Web Sharing On

If you try and turn it on and the status changes to the orange light with “Web Sharing starting…”

Web Sharing Starting

Followed by closely by the grey light and “Web Sharing: Off”

Web Sharing Off

then, you’ve got a typo or other mistake in your httpd.conf file.

From the terminal you can easily check for a problem in the file by running a quick check on it:

craig: httpd -t
httpd: bad group name Staff

No TrackBacks

TrackBack URL: http://www.lensenergy.com/cgi-bin/mt/mt-tb.cgi/171

About the Author

 I've been described as a lost technocrat or a wondering luddite, personally I just like everything that takes us forward.


As the principal of CPPL I try to deliver OSS web solutions to our clients and Cocoa platform software for end-users and businesses alike. Oh, and we enjoy writing IOS software for the iPhone & iPad.

Pages

Powered by Movable Type 4.33-en

About this Entry

This page contains a single entry by craig published on December 20, 2011 1:33 PM.

Find recent content on the main index or look in the archives to find all content.