Compiling and Installing mod_auth_mysql on OS X 10.5.6

April 17th, 2009 by nils

The apche module mod_auth_mysql makes it possible to use a MySQL database instead of e.g. a passwd file. There is an installer of the module for Mac OS X 10.4, but no installation package or port for Mac OS X 10.5 or later. I played around with the sources a little bit and finally managed to get it to compile and apache to start without error messages. I’ve created a diff file to patch mod_auth_mysql.c. The following steps explain how to apply the patch, compile the module and test your apache configuration.

We’ll first create a src folder in your user directory, download mod_auth_mysql and expand it:

$ mkdir ~/src
$ cd ~/src
$ curl -O http://puzzle.dl.sourceforge.net/sourceforge/\
modauthmysql/mod_auth_mysql-3.0.0.tar.gz
$ tar zxvf mod_auth_mysql-3.0.0.tar.gz

Now you have the sources of the module in the folder mod_auth_mysql-3.0.0. Before we can compile it, the patch has to be applied. Here is the diff file mod_auth_mysql, save it to

~/src/mod_auth_mysql-3.0.0/mod_auth_mysql.diff

To apply the patch, we change to the sources directory and execute the unix patch program that takes the original file and applies the diff file as patch:

$ cd mod_auth_mysql-3.0.0
$ patch mod_auth_mysql.c mod_auth_mysql.diff

Now we are ready to compile the module, install it and add it to http.conf, the apache configuration file. Apache modules are compiled using apxs, the APache eXtenSion tool. Before we start, lets look at the command:

$ sudo apxs -c -i -a -D lmysqlclient -lm -lz \
-I/usr/local/mysql/include/mysql/\
-L/usr/local/mysql/lib/ -Wc,"-arch x86_64" \
-Wl,"-arch x86_64" mod_auth_mysql.c

We should execute apxs as super user because it will attempt to install the module to /usr/libexec/apache2/, so we prepend the command with sudo. the -c option tells apxs to compile the module, -i to install it and -a to add a line to http.conf, telling apache to load the module. The -I and -L options are used to provide information about where MySQL is installed and where to find the mysql.h file. I compiled and installed MySQL from source and installed it in the /usr/local/mysql/ directory, if your installation is in a different place, you might have to adjust these parameters. (An excellent guide on compiling and installing MySQL on OS X is available at http://hivelogic.com: “Installing MySQL on Mac OS X“)

When you execute the above command, you should see an output like this:

/usr/share/apr-1/build-1/libtool --silent --mode=compile gcc    -DDARWIN -DSIGPROCMASK_SETS_THREAD_MASK -no-cpp-precomp  -I/usr/include/apache2  -I/usr/include/apr-1   -I/usr/include/apr-1  -arch x86_64 -I/usr/local/mysql/include/mysql/ -Dlmysqlclient  -c -o mod_auth_mysql.lo mod_auth_mysql.c && touch mod_auth_mysql.slo
/usr/share/apr-1/build-1/libtool --silent --mode=link gcc -o mod_auth_mysql.la -arch x86_64  -L/usr/local/mysql/lib/ -lm -lz -rpath /usr/libexec/apache2 -module -avoid-version    mod_auth_mysql.lo
/usr/share/httpd/build/instdso.sh SH_LIBTOOL='/usr/share/apr-1/build-1/libtool' mod_auth_mysql.la /usr/libexec/apache2
/usr/share/apr-1/build-1/libtool --mode=install cp mod_auth_mysql.la /usr/libexec/apache2/
cp .libs/mod_auth_mysql.so /usr/libexec/apache2/mod_auth_mysql.so
cp .libs/mod_auth_mysql.lai /usr/libexec/apache2/mod_auth_mysql.la
cp .libs/mod_auth_mysql.a /usr/libexec/apache2/mod_auth_mysql.a
ranlib /usr/libexec/apache2/mod_auth_mysql.a
chmod 644 /usr/libexec/apache2/mod_auth_mysql.a
----------------------------------------------------------------------
Libraries have been installed in:
   /usr/libexec/apache2

If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the `-LLIBDIR'
flag during linking and do at least one of the following:
   - add LIBDIR to the `DYLD_LIBRARY_PATH' environment variable
     during execution

See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------
chmod 755 /usr/libexec/apache2/mod_auth_mysql.so
[activating module `mysql_auth' in /private/etc/apache2/httpd.conf]

If there are no error messages, you can test the apache configuration with apachectl:

$ apachectl configtest

If the output is

Syntax OK

you can continue by restarting apache:

$ sudo apachectl graceful

The next step would be to set up your locations in http.conf, but that’s for another time…