Apache用户认证方法汇总

一.基本的Apache用户认证方法:

若对某一目录下的文件如/home/ftp/pub需要做到用户认证,则在httpd.conf
中加入下面的行
<>
options indexes followsymlinks
allowoverride authconfig
order allow,deny
allow from all

<>
用在目录/home/ftp/pub下放文件.htaccess,内容如下:
authname "shared files"
authtype basic
authuserfile /etc/.passwd
require valid-user

用随Apache来的程序htpasswd 生成文件/etc/.passwd,每行一个用户名:密码

只要能提供正确的用户名和密码对,就允许登录访问,这是针对任何地址来的
请求都要求提供用户名和密码认证。

二.针对部分网段或地址要求认证。

若公司LAN所在网段为192.168.0.0/24,且有一防火墙专线接入Internet,
内部网卡的地址为192.168.0.1/32,则现在希望所有通过拨本地163通过
防火墙上的apache反向代理向LAN上的另一WWW服务器访问时需要认证,而本地
LAN上的用户不需认证。可以在httpd.conf中放入:
〈Directory /home/ftp/pub>
Options Indexes FollowSymLinks
AllowOverride AuthConfig
order deny,allow
deny from 192.168.0.1
〈/Directory>

且在/home/ftp/pub/.htaccess中放入:
AuthName "shared files"
AuthType Basic
AuthUserFile /etc/.passwd
require valid-user
satisfy any

三.对同一目录及其下的子目录有不同的权限,仅某些人可以存取一目录下的
子目录。
如有一目录/home/ftp/pub/sales,有三个用户user1,user2,user3都需要用户名
和密码进入/home/ftp/pub,但仅user1,user2能进入/home/ftp/pub/sales.则
放下面的行到httpd.conf

〈Directory /home/ftp/pub>
Options Indexes
AllowOverride AuthConfig
order allow,deny
allow from all
〈/Directory>

〈Directory /home/ftp/pub/sales>
Options Indexes
AllowOverride AuthConfig
order allow,deny
allow from all
〈/Directory>

且看/home/ftp/pub/.htaccess为:
AuthName "shared files"
AuthType Basic
AuthUserFile /etc/.passwd
require valid-user

且看/home/ftp/pub/sales/.htaccess
AuthName "shared files"
AuthType Basic
AuthUserFile /etc/.passwd
AuthGroupFile /etc/.salesgroup
require group manager

且文件/etc/.passwd内容为:
user1:passwd1
user2:passwd2
user3:passwd3

且文件/etc/.salesgroup内容为:
manager: user1 user2

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是在CentOS 7上配置Apache用户认证的步骤: 1. 安装Apache和htpasswd 在终端中运行以下命令安装Apache和htpasswd: ``` sudo yum install httpd sudo yum install httpd-tools ``` 2. 创建认证文件 在终端中运行以下命令创建一个名为“auth”的文件夹,并在其中创建一个名为“password”的文件: ``` sudo mkdir /etc/httpd/auth sudo htpasswd -c /etc/httpd/auth/password username ``` 其中,“username”是要设置的用户名。运行上述命令后,您将被要求输入密码。输入并确认密码后,一个新的密码文件将创建在“/etc/httpd/auth/password”路径下。 3. 创建虚拟主机 在终端中运行以下命令创建一个名为“mywebsite”的虚拟主机: ``` sudo nano /etc/httpd/conf.d/mywebsite.conf ``` 在文件中添加以下内容: ``` <VirtualHost *:80> ServerName mywebsite.com DocumentRoot /var/www/mywebsite <Directory /var/www/mywebsite> Options FollowSymLinks AllowOverride All Order deny,allow Allow from all AuthType Basic AuthName "Restricted Content" AuthUserFile /etc/httpd/auth/password Require user username </Directory> </VirtualHost> ``` 其中,“mywebsite.com”是您的网站的域名,“/var/www/mywebsite”是您的网站的根目录,而“username”是您在第2步中创建的用户名。 4. 重新启动Apache 在终端中运行以下命令重新启动Apache: ``` sudo systemctl restart httpd.service ``` 现在,您的网站将受到Apache用户认证的保护。访问网站时,您将被要求输入用户名和密码才能访问受限内容。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值