歡迎您光臨本站 註冊首頁

mod_auth_mysql 在 Apache 2.0 下的安裝和使用

←手機掃碼閱讀     火星人 @ 2014-03-12 , reply:0
  作者:freelamp 徐永久
mod_auth_mysql 是一款很好的基於資料庫對 Apache 用戶認證的模塊,目前已經加入 Apache 模塊庫,而且最新版本支持 Apache 2.0。

這款軟體已經在 FreeLAMP.com 上實施,現把實施過程作簡要介紹。

這款軟體的作者是 Ueli Heuer,其主頁位於

http://www.heuer.org/mod_auth_mysql/.

安裝說明十分簡單,其英文原文可以看下面的連接:

http://www.heuer.org/mod_auth_mysql/INSTALL


這個模塊目前只支持 Apache 2.0 ,安裝採用 DSO 方式:

假設 Apache 2.0.36 安裝於 /opt/httpd-2.0.36 ,那麼運行的命令是:

/opt/httpd-2.0.36/bin/apxs -c -L /usr/local/mysql/lib/mysql mod_auth_mysql.c
/opt/httpd-2.0.36/bin/apxs -i mod_auth_mysql.la

你只要下載主頁上的那個 mod_auth_mysql.c 就可以了。

然後在你的 MySQL 資料庫上建立一個新的資料庫,實際上使用原來存在的資料庫也沒有關係,只要新建的表名不和原來的表名重複就可以,但是為了安全考慮,建議建立單獨的資料庫。

建立資料庫后,可以把下載主頁上的那個 htpasswd.sql 導入到新的資料庫,這個 SQL 文件建立了三個表:

user_info:用戶信息
user_group:用戶的組
host_info:主機信息


#
# Table structure for table `host_info`
#
# the fields created, updated, and isadmin are not needed by the module!
# they may help you creating a php-htpasswd frontend
#

CREATE TABLE host_info (
id int(14) NOT NULL auto_increment,
host char(255) NOT NULL default '',
host_group int(14) NOT NULL default '0',
created timestamp(14) NOT NULL,
updated timestamp(14) NOT NULL,
PRIMARY KEY (id),
KEY host (host)
) TYPE=MyISAM PACK_KEYS=1;
# --------------------------------------------------------

#
# Table structure for table `user_group`
#

CREATE TABLE user_group (
id int(14) NOT NULL auto_increment,
user_name char(50) NOT NULL default '',
user_group char(20) NOT NULL default '',
host_group int(14) default NULL,
created timestamp(14) NOT NULL,
updated timestamp(14) NOT NULL,
PRIMARY KEY (id),
KEY host_group (host_group),
KEY user_group (user_group)
) TYPE=MyISAM PACK_KEYS=1;
# --------------------------------------------------------

#
# Table structure for table `user_info`
#

CREATE TABLE user_info (
id int(14) NOT NULL auto_increment,
user_name char(30) NOT NULL default '',
user_passwd char(20) NOT NULL default '',
host_group int(14) NOT NULL default '0',
created timestamp(14) NOT NULL,
updated timestamp(14) NOT NULL,
isadmin tinyint(4) NOT NULL default '0',
PRIMARY KEY (id),
UNIQUE KEY user_name (user_name,host_group)
) TYPE=MyISAM PACK_KEYS=1;


從以上三個表的結構,我們可以看到,其中最主要的就是 host_group 的一致,
user_info 的 host_group 和 host_info 的 host_group 要對應起來,user_info 的其他欄位,例如 isadmin,created,updated 等都還沒有使用,用於以後擴展。
因此,一個簡單的插入數據的例子就是:
insert into host_info (host) values ("www.freelamp.com");
insert into user_info (user_name,user_passwd) values ("albertxu",encrypt("my_log_passwd"));


然後修改 httpd.conf 加入:

LoadModule auth_mysql_module modules/mod_auth_mysql.so




AuthType Basic

AuthMySQLHost localhost ;連接資料庫的主機地址,一般用本地連接,所以為 localhost
AuthMySQLUser apache_auth ;連接資料庫的用戶名
AuthMySQLPassword my_secret_pass ;連接資料庫的口令
AuthMySQLDB apache ;資料庫的名字

AuthSQLAuthoritative On
AuthSQLKeepAlive off





需要認證的目錄下的 .htaccess 文件,要把 AuthUserFile 這項去掉。其他可以保持不變。

Authname "FreeLAMP.com Log Detail"
Authtype Basic
Require user albertxu


這樣一個可以針對大型虛擬主機的認證系統就建立起來了。

最後需要特別說明的是:user_info 表中的 user_passwd 欄位是採用 mysql 的 encrypt() 加密的,而不是 password() ,更不是明碼。這個在文檔上面沒有說明,我電子郵件給 Ueli Heuer 先生,便很快得到了回答:

======================================================================
On Mon, 27 May 2002 23:20:30 +0800
"Xu" wrote:

Hi Albert,


The problem are the clertext passwords in the db. You need to encrypt this with the
encrypt() function from mysql or with the unix password crypt() function.

Did you check the errorlog form apache? mod_auth_mysql writes some hints if somethings
fails (e.g. dbconenction fails, host is not in the db, and so on

as an example:
[Mon May 27 17:52:04 2002] [error] [client 192.168.1.39] password mismatch on
deadeye.maillink.ch: http://test:versuch@deadeye.heuer.org/

Hope it helps

Greetz
Ueli

===================================================================



< Apache 性能優化技巧 | FreeLAMP.com Web Server 升級到 Apache 2.0.36 >


相關連接
有關Tutorial 的文章
作者徐永久的所有文章
聯繫作者





下面的點評文字版權歸點評者所有。
( 點評 )


下載中心文件採用認證下載的辦法
by 徐永久 on 2002年05月30日 02:26

下載中心的文件採用了以上的認證辦法,您必須是論壇的註冊用戶才能下載。花費了我一定時間來做這個事情,提起 PHP 感覺都有點生疏了。 :-)

修改論壇的 register.php :
註冊校驗通過後:
$dl_db=mysql_connect(...);
$sql="INSERT INTO user_info (user_name,user_passwd,host_group) values ('".addslashes(htmlspecialchars($username))."',encrypt('$password'),9)";
mysql_query($sql,$dl_db);
$sql="INSERT INTO user_group (user_name,user_group,host_group) values ('".addslashes(htmlspecialchars($username))."','dl_grp',9)";
mysql_query($sql,$dl_db);
mysql_close($dl_db);

修改密碼后同時修改,認證庫的密碼,修改 member.php:
$dl_db=mysql_connect(...);
$sql="UPDATE user_info set user_passwd=encrypt('".addslashes($newpassword)."') WHERE user_name='$bbuserinfo[username]'";
mysql_query($sql,$dl_db);
mysql_close($dl_db);


做了一個移植的程序,把原來的論壇用戶添加到 MySQL 認證庫中:

$db=mysql_connect(...); // 論壇庫
$dl_db=mysql_connect(...); //認證庫
$result = mysql_query("select lower(username),password from user",$db)
while ($row = mysql_fetch_array($result)) {
$sql="INSERT INTO user_info (user_name,user_passwd,host_group) values ('".$row[0]."',encrypt('".$row[1]."'),9)";
mysql_query($sql,$dl_db);
$sql="INSERT INTO user_group (user_name,user_group,host_group) values ('".$row[0]."','dl_grp',9)";
mysql_query($sql,$dl_db);
}
mysql_free_result($result);
mysql_close($dl_db);
mysql_close($db);
?>


[ 回應此文 ]

Re: 下載中心文件採用認證下載的辦法
由 mydowns 發表於 2002年05月30日 08:44

找了很久了!

[ 回複本貼 ]



作者更新了文檔
by 徐永久 on 2002年06月04日 08:42

下面是他發給我的 Mail:

===================================

Hi

I have just written a short example for the data in the database. you may fetch it from
http://www.heuer.org/mod_auth_mysql/example_data.html

Ueli

--
"The software said it requires Windows 95 or better,
so I installed Linux"

==========================================

所以你去

http://www.heuer.org/mod_auth_mysql/example_data.html

就可以看到具體的數據插入的例子了。




[火星人 ] mod_auth_mysql 在 Apache 2.0 下的安裝和使用已經有760次圍觀

http://coctec.com/docs/linux/show-post-67843.html