一、虚拟主机配置
1、基于ip
要求:通过192.168.32.31可以访问/var/www/html目录内容,通过192.168.32.32可以访 问/var/www/virt目录内容
[root@station1 ~]#vi /etc/httpd/conf/httpd.conf
<VirtualHost 192.168.32.31:80>
ServerAdmin [email protected]
DocumentRoot /var/www/html
ServerName 192.168.32.31:80
ErrorLog logs/dummy-host.example.com-error_log
CustomLog logs/dummy-host.example.com-access_log common
</VirtualHost>
<VirtualHost 192.168.32.32:80>
ServerAdmin [email protected]
DocumentRoot /var/www/virt
ServerName 192.168.32.32:80
ErrorLog logs/dummy-host.example.com-error_log
CustomLog logs/dummy-host.example.com-access_log common
</VirtualHost>
2、基于端口
要求:通过192.168.32.31的80端口可以访问/var/www/html目录内容,通过192.168.32.31的8080端口可以访问/var/www/virt目录内容
[root@station1 ~]#vi /etc/httpd/conf/httpd.conf
Listen 80 #此端口配置文件默认就有
Listen 8080 #手动添加此端口
<VirtualHost 192.168.32.31:80>
ServerAdmin [email protected]
DocumentRoot /var/www/html
ServerName 192.168.32.31:80
ErrorLog logs/dummy-host.example.com-error_log
CustomLog logs/dummy-host.example.com-access_log common
</VirtualHost>
<VirtualHost 192.168.32.31:8080>
ServerAdmin [email protected]
DocumentRoot /var/www/virt
ServerName 192.168.32.31:8080
ErrorLog logs/dummy-host.example.com-error_log
CustomLog logs/dummy-host.example.com-access_log common
</VirtualHost>
3、基于主机头
要求:通过station1.example.com可以访问/var/www/html目录内容,通过www.example.com可以访问/var/www/virt目录内容 (注意要求DNS服务器上有这两个网站解析)
[root@station1 ~]#vi /etc/httpd/conf/httpd.conf
NameVirtualHost 192.168.32.31:80 #要求必须由此行,此行表示打开主机头虚拟主机
<VirtualHost 192.168.32.31:80>
ServerAdmin [email protected]
DocumentRoot /var/www/html
ServerName station1.example.com
ErrorLog logs/dummy-host.example.com-error_log
CustomLog logs/dummy-host.example.com-access_log common
</VirtualHost>
<VirtualHost 192.168.32.31:80>
ServerAdmin [email protected]
DocumentRoot /var/www/virt
ServerName www.example.com
ErrorLog logs/dummy-host.example.com-error_log
CustomLog logs/dummy-host.example.com-access_log common
</VirtualHost>
二、多种用户认证方式配置
1、使用htpsswd工作生成的密码文件认证用户来源
[root@station1 conf.d]# htpasswd -cm /etc/httpd/.webusers linuxidc
[root@station1 conf.d]# htpasswd -m /etc/httpd/.webusers linuxidcster
[root@station1 conf.d]# htpasswd -m /etc/httpd/.webusers zhxy
[root@station1 conf.d]# htpasswd -m /etc/httpd/.webusers zxy
[root@station1 conf.d]# vi /etc/httpd/.webgroup #给用户分组
net:linuxidc linuxidcster
zh:zhxy zxy
# -c:表示创建密码文件
# -m:用md5方式加密认证信息
# -D:从密码文件中删除用户
[root@station1 conf.d]#
[root@station1 conf.d]# vi /etc/htttpd/conf/httpd.conf
<VirtualHost 192.168.32.31:80>
ServerAdmin [email protected]
DocumentRoot /var/www/html
ServerName station1.example.com
<Directory /var/www/html>
AuthName TestAdmin #提示信息
AuthType basic #基本身份认证,即基于密码文件的身份认证
AuthUserFile /etc/httpd/.webusers
Require valid-user #所有授权用户均可访问;
AuthGroupFile /etc/httpd/.webgroup #可访问用户为net组中用户
Require Group net
#valid-user:表所有密码文件中的用户均可访问此目录,也可为Require linuxidc则表示只有密码文件中linuxidc账户可以访问此目录
</Directory>
ErrorLog logs/dummy-host.example.com-error_log
CustomLog logs/dummy-host.example.com-access_log common
</VirtualHost>
2、使用MySQL数据库认证用户来源
安装mysql及httpd中mysql认证模块
[root@station1 ~]# yum install mysql-server.i386
[root@station1 ~]# yum install mysql-devel.i386
[root@station1 ~]# yum install mod_auth_mysql.i386
[root@station1 ~]# service mysqld start
[root@station1 ~]# chkconfig mysql on
创建认证用户和认证组
[root@station1 ~]# mysqladmin -u root password RedHat
[root@station1 ~]# mysql -uroot -p RedHat
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 131
Server version: 5.0.77 Source distribution
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> create database apacheusers;
mysql> use apacheusers;
mysql> create table user (name char(25),pwd char(25), primary key (name));
mysql> create table grp (uname char(25),gname char(25),primary key (uname,gname));
mysql> grant select on apacheusers.user to apacheuser@localhost identified by ' RedHat';
mysql> grant select on apacheusers.grp to apacheuser@localhost identified by ' RedHat';
mysql> insert into user (name,pwd) values ('linuxidc','111');
mysql> insert into user (name,pwd) values ('linuxidcster','111');
mysql> insert into user (name,pwd) values ('zhxy','222');
mysql> insert into user (name,pwd) values ('zxy','222');
mysql> insert into grp (uname,gname) values ('linuxidc','net');
mysql> insert into grp (uname,gname) values ('linuxidcster','net');
mysql> insert into grp (uname,gname) values ('zhxy','zh');
mysql> insert into grp (uname,gname) values ('zxy','zh');
修改配置文件,开启mysql认证
[root@station1 ~]# vi /etc/httpd/conf/httpd.conf
NameVirtualHost 192.168.32.31:80
<VirtualHost 192.168.32.31:80>
ServerAdmin [email protected]
DocumentRoot /var/www/html
ServerName station1.example.com
<Directory /var/www/html>
AuthName TestAdmin
AuthType basic
AuthMySQLEnable on
AuthMySQLUser apacheuser
AuthMySQLPassword RedHat
AuthMySQLDB apacheusers
AuthMySQLUserTable user
AuthMySQLNameField name
AuthMySQLPasswordField pwd
Require valid-user
AuthMySQLGroupTable grp
AuthMySQLGroupField gname
Require Group net
</Directory>
ErrorLog logs/dummy-host.example.com-error_log
CustomLog logs/dummy-host.example.com-access_log common
</VirtualHost>