首先安装一下mysql数据库
[root@localhost ~]# yum install -y mysql-sever mysql-devel
启动mysql数据库
[root@localhost ~]# service mysqld restart
给mysql的root设置一个密码为123
[root@localhost ~]# mysqladmin -uroot password 123
进入mysql数据库
[root@localhost ~]# mysql -uroot -p123
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.0.95 Source distribution
Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> create database zhanghan;
Query OK, 1 row affected (0.00 sec)
mysql> use zhanghan;
Database changed
mysql> create table edong(name char(25),pwd char(25),primary key(name));
Query OK, 0 rows affected (0.01 sec)
mysql> insert into edong(name,pwd) values('user01',ENCRYPT(111));
Query OK, 1 row affected (0.01 sec)
mysql> insert into edong(name,pwd) values('user02',ENCRYPT(111));
Query OK, 1 row affected (0.00 sec)
mysql> select * from edong;
+--------+---------------+
| name | pwd |
+--------+---------------+
| user01 | oLdqcSYvbXsu2 |
| user02 | .MquW8d0Wn.Xc |
+--------+---------------+
2 rows in set (0.00 sec)
我们在apache建立虚拟用户
[root@localhost ~]# cd /etc/httpd/conf.d/
[root@localhost conf.d]# vim virtual.conf
ServerName 192.168.115.128
DocumentRoot /www/users/html
AuthName linux ----认证的名字
AuthType Basic ----认证的类型,这里为基本的
AuthMYSQLEnable on ----开启mysql认证
AuthMYSQLUser root ----mysql的用户为root
AuthMYSQLPassword 123 ----mysql的root的密码为123
AuthMYSQLDB zhanghan ----mysql里面需要认证的数据库(zhanghan)
AuthMYSQLUserTable edong ----mysql里面需要认证数据库里的表(edong)
AuthMYSQLNameField name ----mysql里面需要认证表里面的字段(name)
AuthMYSQLPasswordField pwd ----mysql里面需要认证表里面的密码(pwd)
Require valid-user ----每个用户都可以访问
保存退出
安装mysql的认证,不然启动httpd会报错
[root@localhost html]# yum install -y mod_auth_mysql
重启httpd
[root@localhost html]# /etc/init.d/httpd restart
Stopping httpd: [ OK ]
Starting httpd: [ OK ]
最后测试
http://192.168.115.128/
会跳出一个框框,输入用户名:user01
密码:111