一.基于htpasswd创建的用户名密码的验证
1.创建用户名密码文件
2.配置/etc/apache2/sites-available/default文件
二.基于MySQL数据库表中信息的验证
1.MySQL的验证,要求安装mod_auth_mysql模块。
1.创建用户名密码文件
touch -f /tmp/shou/.passwd
htpasswd -b /tmp/shou/.passwd shou test1234
※htpasswd命令用法参照:http://hi.baidu.com/hellgrowl/item/75aa898fd97eb1c298255f9d
2.配置/etc/apache2/sites-available/default文件
Alias /test "/tmp/shou/test"
<Directory "/tmp/shou/test">
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
AuthName "Authorization TEST(01)"
AuthType Basic
AuthUserFile /tmp/shou/.passwd
Require valid-user
</Directory>
二.基于MySQL数据库表中信息的验证
1.MySQL的验证,要求安装mod_auth_mysql模块。
sudo apt-get install libapache2-mod-auth-mysql
2.激活mode_auth_mysql模块
cd /etc/apache2/mods-enabled
ln -s ../mods-available/auth_mysql.load
3.创建数据库表,添加数据
CREATE TABLE tbl_user (
id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
username VARCHAR(128) NOT NULL,
password VARCHAR(128) NOT NULL,
email VARCHAR(128) NOT NULL
);
INSERT INTO tbl_user (username, password, email) VALUES ('test1', 'pass1', 'test1@example.com');
INSERT INTO tbl_user (username, password, email) VALUES ('test2', 'pass2', 'test2@example.com');
4.配置/etc/apache2/sites-available/default文件
Alias /test1 "/tmp/shou/test1"
<Directory "/tmp/shou/test1">
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
AuthName "Authorization TEST(02)"
AuthType Basic
Auth_MySQL On
Auth_MySQL_Authoritative On
Auth_MySQL_Host localhost
Auth_MySQL_Username root
Auth_MySQL_Password rootadmin
Auth_MySQL_DB test
Auth_MySQL_Password_Table tbl_user
Auth_MySQL_Username_Field username
Auth_MySQL_Password_Field password
Auth_MySQL_Encryption_Types Plaintext
Auth_MySQL_Empty_Passwords Off
AuthBasicAuthoritative Off
AuthUserFile /dev/null
Require valid-user
</Directory>