Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Install a MySQL authentication module for Apache Internet
After much trying to work out how to protect a directory with PHP so that full URLs couldn't access any of the static images, I finally found a mod for Apache (mod_auth_mysql) that allows authentication against mySQL. While this means that I end up not using the nice PHP form I had created, it does secure the files. This is my first attempt at a how-to, so be gentle. Hope it helps.

How to set up OS X 10.3.5 Apache (built in) and mySQL for authentication via mod_auth_mysql. Why? Because I like gross overkill.

Firstly you will need to install mySQL if you haven't already. I recommend getting the OS X binary from this page at It's version 4.0.21 at the time of writing. Many tutorials online tell you how to install mySQL for OS X. Most of them tell you to set up a user called mysql in your netinfo. This is not necessary, as it is already there. Install the package and follow the Readme to setup your root password. Install the MySQLStartupItem.pkg as well.

Now we test to see if it is working. Type the following in the Terminal:
$ alias mysql=/usr/local/mysql/bin/mysql
$ alias mysqladmin=/usr/local/mysql/bin/mysqladmin

$ mysqladmin -u root password root_password
$ mysql -u root -p
Enter Password:
MySQL should load at this point. This is a good time to create your database for user login:
mysql>CREATE DATABASE users;
mysql> USE users;

CREATE TABLE user_info (
  user_name CHAR(30) NOT NULL,
  user_passwd CHAR(20) NOT NULL,
    [ any other fields if needed ]
  PRIMARY KEY (user)

YAY! Note : I suggest using phpMyAdmin to manage your database and populate it with data. [Download 2.6.0-pl1]

Once that is up and running, download the mod_auth_mysql package from sourceforge; the current version iis 2.6.0 at time of writing. Extract the archive and compile the package. The Readme that comes with the package tells you how to do this. Unfortunately, the apxs command in the Readme is incorrect. use these two lines instead:

$ apxs -c -D lmysqlclient -lm -lz -I/usr/local/mysql/include/ \
-L/usr/local/mysql/lib/  mod_auth_mysql.c
$ apxs -i
Now you need to edit you httpd.conf to use the new mod. Open /etc/httpd/httpd.conf in your favorite editor. Note the editor must be run as sudo if you hope to save the file in its original location --i.e.: sudo vi httpd.conf. The Readme for mod_auth_mysql gives the standard Apache install tree for adding this mod to your config file. OS X has a slightly different tree for apache, so insert the following lines in the appropriate places instead of the original Readme lines:
LoadModule mysql_auth_module libexec/httpd/

AddModule mod_auth_mysql.c
I used per-directory in the httpd.confinstead of .htaccess files, but both are usable. The mod_auth_mysql Readme gives a complete list of all the options available. While the Readme states that the mod assumes a particular table format in your database, I had to declare each field anyway:
<Directory "/dir/you/want/to/protect/">
 AuthType Basic
 AuthGroupFile /dev/null
 AuthMySQLEnable On
 AuthMySQLHost localhost
 AuthMySQLDB users
 AuthMySQLUserTable user_info
 AuthMySQLUser <user>
 AuthMySQLPassword <password>
 AuthMySQLPasswordField user_passwd
 AuthMySQLPwEncryption none
 AuthMySQLSaltField none
 AuthMySQLNoPasswd Off
 AuthMySQLAuthoritative On
 require valid-user
This config uses the following table in the database "users":
CREATE TABLE user_info (
  user_name CHAR(30) NOT NULL,
  user_passwd CHAR(20) NOT NULL,
    [ any other fields if needed ]
  PRIMARY KEY (user)
Save your file and check it with apachectl configtest; if that works out, then restart your server by typing sudo apachectl restart.

That should be it. Enjoy!
  • Currently 2.33 / 5
  You rated: 1 / 5 (3 votes cast)

Install a MySQL authentication module for Apache | 9 comments | Create New Account
Click here to return to the 'Install a MySQL authentication module for Apache' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Install a MySQL authentication module for Apache
Authored by: mfried on Oct 06, '04 11:51:36AM
I had to change the line above

apxs -c -D lmysqlclient -lm -lz -I/usr/local/mysql/include/ -L/usr/local/mysql/lib/ mod_auth_mysql.c
to this

apxs -c -D lmysqlclient -lm -lz -I/Library/MySQL/include/mysql/ -L/Library/MySQL/lib/mysql/ mod_auth_mysql.c
My path to the files was different. I installed my version of MySQL from ServerLogistics.


Buy a Mac, save the world!

[ Reply to This | # ]

Install a MySQL authentication module for Apache
Authored by: hedgepig on Oct 06, '04 01:11:15PM

The build process and the installation into the module directory work without incident, but apache just doesn't want to start afterwards. Configtest gives me just "...because of an unknown error" on the loadmodule line.

I am also uncertain if I understand how the table setup should be. Even the original documentation that comes with the sourcecode is wrong about the initial table design! (The primary key of the table can't have a name, that doesn't exist in the columnnames. This error is also present in this hint, thats why I doubt that it will work for anyone)

So which database is used anyway? The original documentation says: it is assumed that every database has this user-info table... :( (If that is actually true, this is pretty poor programming!)
Also: as which user does the module try to access the userinformation table?

Maybe someone can enlighten me here...
Did it really work for someone?

[ Reply to This | # ]
Perhaps this will help
Authored by: XJ0 on Oct 06, '04 09:33:58PM

As to the configtest error, i am not sure. I assume you checked the module directory to make sure the mod is there and the http.conf has right path to the mod:

ie: Loadmodule mod_auth_mysql libexec/httpd/
and you did add:
addmodule mod_auth_mysql.c

you could try using >apsx -i -a

for the DB your correct. the primary key should be : user_name
In the apache <Directory /xxx/xxx/xx> example you will see that i have set the DB to users --> AuthMySQLDB users.
The mod is supposed to assume this DB but i found it doesn't so i set it.
The mod also seems to try to use the user www to access the DB unless told otherwise. For this reason i setup a webaccess user in mysql and told the mod to use that user.

Sorry i didn't make this clear.

[ Reply to This | # ]
I can't compile this
Authored by: jpkelly on Oct 16, '04 07:37:50PM

on OS 10.2.6
I tried:
apxs -c -D -lmysqlclient -lm -lz -I/usr/include/mysql -L/usr/lib/mysql mod_auth_mysql.c

and got:
gcc -DDARWIN -DUSE_HSREGEX -DUSE_EXPAT -I../lib/expat-lite -g -Os -pipe -DHARD_SERVER_LIMIT=2048 -DEAPI -DSHARED_MODULE -I/usr/include/httpd -I/usr/include/mysql -D-lmysqlclient -c mod_auth_mysql.c
cc -bundle -undefined suppress -flat_namespace -Wl,-bind_at_load -o mod_auth_mysql.o -L/usr/lib/mysql -lm -lz

Any ideas or help?

[ Reply to This | # ]
I can't compile this(still)
Authored by: jpkelly on Oct 16, '04 07:44:01PM

I also tried:
apxs -c -lmysqlclient -lm -lz -I/usr/include/mysql -L/usr/lib/mysql mod_auth_mysql.c

and got:
gcc -DDARWIN -DUSE_HSREGEX -DUSE_EXPAT -I../lib/expat-lite -g -Os -pipe -DHARD_SERVER_LIMIT=2048 -DEAPI -DSHARED_MODULE -I/usr/include/httpd -I/usr/include/mysql -c mod_auth_mysql.c
cc -bundle -undefined suppress -flat_namespace -Wl,-bind_at_load -o mod_auth_mysql.o -L/usr/lib/mysql -lmysqlclient -lm -lz

[ Reply to This | # ]
No need to compile!
Authored by: Elander on Nov 15, '04 12:33:09PM
You can find a ready made installation package for OS X on this page (link opens a new window):

It's for 2.6.1 (the current version is 2.7.0 but if you don't need to be on the bleeding edge this'll work just fine in most cases).



[ Reply to This | # ]

mod_auth_mysql on Leopard
Authored by: ti.m on Feb 19, '08 04:36:48PM

Has anyone tried to get mod_auth_mysql working on a default apache install of Leopard (apache 2.2.6)? I spent too much time yesterday trying to get it working and got absolutely nowhere...

Any ideas?

[ Reply to This | # ]
Building/compiling mod_auth_mysql- Problem
Authored by: EddieG on Jul 29, '08 03:11:27PM

Hi. I downloaded the mod_auth_mysql 3.0 from sourceforge. I tried following the instructions on the ReadMe file and looked through several postings but still no luck.

Here is what I typed:

$ apxs -c -I/usr/local/mysql/include -L/usr/local/mysql/lib/mysql -lmysqlclient -lm mod_auth_mysql.c

Then this would display:

/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 -I/usr/local/mysql/include -c -o mod_auth_mysql.lo
mod_auth_mysql.c && touch mod_auth_mysql.slo
/usr/share/apr-1/build-1/libtool: line 1219: gcc: command not found
apxs:Error: Command failed with rc=65536

I checked to make sure the mysql.h and libmysqlclient files were in the appropriate directories.

How do I fix this? Thanks

[ Reply to This | # ]
Install a MySQL authentication module for Apache
Authored by: jweinberger on Dec 06, '08 10:56:33PM

I spent some time looking around and found this:


It overcomes the errors on 10.5 (Leopard) Intel. I'm on a Mac Mini, so I had to take out the arch= stuff (you should regardless if your Apache is compiled as 32-bit).

I still can't actually get it to work - I'd love any help anyone can offer...

configtest shows OK but when I actually try to open the protected page in a browser, I get "forbidden" and the server log (set to LogLevel debug) says "client denied by server configuration" and I can't seem to figure out why...



[ Reply to This | # ]