Apache, http_auth og MySQLApache, http_auth and MySQL

Det er ganske enkelt at opsætte Apache til at beskytte en webfolder med brugernavn og kodeord ud fra en .htpasswd fil, men det er faktisk heller ikke så besværligt at opsætte den til at tjekke brugere ud fra en MySQL database.

Først installér mod_auth_mysql og så enable den:

sudo apt-get install libapache2-mod-auth-mysql
sudo a2enmod auth_mysql
sudo service apache2 restart

Så skal der oprettes en .htaccess fil i roden af dit webdir med følgende:

AuthName "Mit hemmelige sted"
AuthType Basic
AuthUserFile /dev/null
AuthBasicAuthoritative off

Auth_MySQL_Authoritative off
Auth_MySQL On
Auth_MySQL_Host localhost
Auth_MySQL_User dit_mysql_username
Auth_MySQL_Password dit_mysql_password
Auth_MySQL_DB dit_mysql_database_navn
Auth_MySQL_Password_Table users
Auth_MySQL_Username_Field username
Auth_MySQL_Password_Field password
Auth_MySQL_Empty_Passwords Off
Auth_MySQL_Encryption_Types PHP_MD5
require valid-user

Her er den konfigureret til at bruge tabellen users og kigge på feltene username og password for henholdsvis brugernavn og kodeord.

Læg mærke til at jeg har sat Auth_MySQL_Encryption_Types til PHP_MD5. Det er fordi jeg har brugt PHP’s MD5 funktion til at kryptere kodeorderne.

Af andre krypterintstyper er; Plaintext, Crypt_DES, Crypt_MD5, Crypt, SHA1Sum, MySQL og Apache. Plaintext siger sig selv, MYSQL er MYSQLa PASSWORD() funktion og Apache er htpasswd’s måde at kryptere på.

Håber at det lykkes for dig. Desværre har http_auth og MySQL altid været pisse besværligt, da der er forskellige forks af koden, som har krævet forskellige konfigurationer. Dertil er debugging tæt på umulig.

Tips: Kig i din /var/log/apache2/error.log og i gz arkivet /usr/share/doc/libapache2-mod-auth-mysql/DIRECTIVES.gz ligger der lidt dokumentation.

It is quite simple to set up Apache to protect a web folder with a username and password from a .htpasswd file, but it is actually not that hard to set it up to check users against a MySQL database either.

First install mod_auth_mysql and enable it:

sudo apt-get install libapache2-mod-auth-mysql
sudo a2enmod auth_mysql
sudo service apache2 restart

Then create a .htaccess file in the root of your web directory with the following:

AuthName "My secret place"
AuthType Basic
AuthUserFile /dev/null
AuthBasicAuthoritative off

Auth_MySQL_Authoritative off
Auth_MySQL On
Auth_MySQL_Host localhost
Auth_MySQL_User your_mysql_username
Auth_MySQL_Password your_mysql_password
Auth_MySQL_DB your_mysql_database_name
Auth_MySQL_Password_Table users
Auth_MySQL_Username_Field username
Auth_MySQL_Password_Field password
Auth_MySQL_Empty_Passwords Off
Auth_MySQL_Encryption_Types PHP_MD5
require valid-user

Here it is configured to use the table users and look at the fields username and password for the username and password.

Notice that I have set Auth_MySQL_Encryption_Types to PHP_MD5. That is because I used PHP’s MD5 function to encrypt the passwords.

Other encryption types are Plaintext, Crypt_DES, Crypt_MD5, Crypt, SHA1Sum, MySQL and Apache. Plaintext speaks for itself, MySQL is MySQL’s PASSWORD() function and Apache is htpasswd’s way of encrypting.

I hope it works out for you. Unfortunately http_auth and MySQL have always been a real pain, since there are several forks of the code that have needed different configurations. On top of that, debugging is close to impossible.

Tip: look in your /var/log/apache2/error.log, and the gz archive /usr/share/doc/libapache2-mod-auth-mysql/DIRECTIVES.gz holds a little documentation.