How do I use mod_auth_mysql on my server?

Question:

How do I use mod_auth_mysql on my server?

Answer:

Mod_auth_mysql is already installed on your server; all you need to do is use it.

Software:

Apache, MySQL

Detail:

mod_auth_mysql is a great way to password protect directories and files on your server. Instead of using hard to maintain password files mod_auth_mysql stores usernames and passwords in a database. At first mod_auth_mysql may seem a bit complex but hopefully by following the instructions in this FAQ you should have problems at all.

Solution:

Follow these steps to install mod_auth_mysql:

  1. Log into the server using Telnet or, more preferably, SSH.

  2. Create the proper database and tables.

First, create the database from a Telnet or SSH prompt by entering the following command:

> mysqladmin create http_auth

Next, create the tables by entering the following command:

> mysql http_auth

Finally, copy and paste the following at the mysql command prompt:

create table mysql_auth (

          username char(25),

          passwd char(25),

          groups char(25),

          primary key (username)

        );  

This should create the proper database and table information that mod_auth_mysql will need.

You can now go ahead and exit the MySQL monitor by typing:

> exit

  1. Create the configuration file in the directory you want to protect.

Suppose you want to password protect the "private" directory under yourdomain.com. To do so, you would enter the following command:

> cd /www/vhosts/yourdomain.com/htdocs/private

Once you are in the proper directory, create the .htaccess file with the following command. The -w flag ensures that word wrapping will not occur:

> pico -w .htaccess

Copy and paste the following information into the .htaccess file:

AuthName "Title information for the prompt"

AuthType Basic

require valid-user

Auth_MySQL_Encryption_Types Plaintext Crypt_DES

Auth_MySQL_DB http_auth

Finally, exit the pico editor and save the file.

  1. Restart Apache.

From your SSH or Telnet prompt, type:

> apachectl restart

  1. To add a user to the database the procedure is simple.

From your Telnet prompt, type:

> mysql http_auth

Once inside the MySQL monitor, type:

mysql> INSERT INTO mysql_auth VALUES('your_user_name','your_password',NULL);

This should be it!

Comments:

When you add the information to the database you can choose whether or not you'd like to encrypt the password. If you are to encrypt it. In the password field encapsulate the password like so password('password_of_your_choice').

More information can be found at the following sites:

http://www.cgi101.com/class/password/mod_auth_mysql.html

http://www.mysql.com/