设为首页 收藏本站
查看: 963|回复: 0

[经验分享] 11.18 Apache用户认证

[复制链接]

尚未签到

发表于 2018-11-18 10:06:08 | 显示全部楼层 |阅读模式
- 11.18 Apache用户认证
- 11.19/11.20 域名跳转
- 11.21 Apache访问日志
- 扩展
- apache虚拟主机开启php的短标签 http://ask.apelearn.com/question/5370

# 11.18 apache用户认证
### httpd的用户认证
- vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf //把123.com那个虚拟主机编辑成如下内容
```

    DocumentRoot "/data/wwwroot/www.123.com"
    ServerName www.123.com
     //指定认证的目录
        AllowOverride AuthConfig //这个相当于打开认证的开关
        AuthName "123.com user auth" //自定义认证的名字,作用不大
        AuthType Basic //认证的类型,一般为Basic,其他类型阿铭没用过
        AuthUserFile /data/.htpasswd  //指定密码文件所在位置
        require valid-user //指定需要认证的用户为全部可用用户
   

/usr/local/apache2.4/bin/htpasswd -cm /data/.htpasswd aming
```
- 先开打虚拟主机配置文件
```
[root@localhost ~]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
# Please see the documentation at
#
# for further details before you try to setup virtual hosts.
#
# You may use the command line option '-S' to verify your virtual host
# configuration.
#
# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for all requests that do not
# match a ServerName or ServerAlias in any  block.
#

    DocumentRoot "/data/wwwroot/abc.com"
    ServerName abc.com
    ServerAlias www.abc.com www.123.com
    ErrorLog "logs/abc.com-error_log"
    CustomLog "logs/abc.com-access_log" common


    DocumentRoot "/data/wwwroot/111.com"
    ServerName 111.com
    ServerAlias www.example.com
    ErrorLog "logs/111.com-error_log"
    CustomLog "logs/111.com-access_log" common


                                                                  37,5          93%
```
-  把文件内容改为
```
# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for all requests that do not
# match a ServerName or ServerAlias in any  block.
#

    DocumentRoot "/data/wwwroot/abc.com"
    ServerName abc.com
    ServerAlias www.abc.com www.123.com
    ErrorLog "logs/abc.com-error_log"
    CustomLog "logs/abc.com-access_log" common


    DocumentRoot "/data/wwwroot/111.com"
    ServerName 111.com
    ServerAlias www.example.com
   
        AllowOverride AuthConfig
        AuthName "111.com user auth"
        AuthType Basic
        AuthUserFile /data/.htpasswd
        require valid-user
   
    ErrorLog "logs/111.com-error_log"
    CustomLog "logs/111.com-access_log" common


:wq         
```
- /usr/local/apache2.4/bin/htpasswd -c -m /data/.htpasswd aming
- -c 是creaate 创建用户  -m 是使用MD5方式 加密  /data/.htpasswd 指定密码文件目录  aming 是创建的新用户
```
[root@localhost ~]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
[root@localhost ~]# /usr/local/apache2.4/bin/htpasswd -c -m /data/.htpasswd aming
New password:
Re-type new password:
Adding password for user aming
[root@localhost ~]#
[root@localhost ~]# cat /data/.htpasswd
aming:$apr1$EXwYfiem$WmlVecIGEuLU781VJMO6y/
[root@localhost ~]# ls /data/.htpasswd
/data/.htpasswd
[root@localhost ~]#
```
- 再增加一个用户zhangsan
```
[root@localhost ~]# /usr/local/apache2.4/bin/htpasswd -m /data/.htpasswd zhangsan
New password:
Re-type new password:
Adding password for user zhangsan
[root@localhost ~]#
[root@localhost ~]# cat /data/.htpasswd
aming:$apr1$hRjEjYks$LpCPxZ/PUOvox0ZE5Qea9.
zhangsan:$apr1$cwKQ8Lwu$P.iw/DySVIn2sBrAF3AUb0
[root@localhost ~]#
```
-  重新加载配置-t , graceful  绑定hosts,浏览器测试
-  curl -x127.0.0.1:80 www.123.com //状态码为401
-  curl -x127.0.0.1:80 -uaming:passwd www.123.com //状态码为200
```
[root@localhost ~]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[root@localhost ~]# /usr/local/apache2.4/bin/apachectl graceful
[root@localhost ~]#
[root@localhost ~]# curl -x127.0.0.1:80 111.com


401 Unauthorized

Unauthorized
This server could not verify that you
are authorized to access the document
requested.  Either you supplied the wrong
credentials (e.g., bad password), or your
browser doesn't understand how to supply
the credentials required.

[root@localhost ~]#
[root@localhost ~]# curl -x127.0.0.1:80 111.com -I
HTTP/1.1 401 Unauthorized
Date: Sun, 08 Oct 2017 14:59:22 GMT
Server: Apache/2.4.27 (Unix) PHP/7.1.6
WWW-Authenticate: Basic realm="111.com user auth"
Content-Type: text/html; charset=iso-8859-1
[root@localhost ~]#
```
-  401 这个状态码 ,是说明你访问的内容需要做用户验证
-  同样也可以在浏览器里面看下,前提你要在windows hosts文件里定义111.com
-  ![mark](http://oqxf7c508.bkt.clouddn.com/blog/20171008/230249462.png?imageslim)
```
[root@localhost ~]# curl -x127.0.0.1:80 -uaming:123456 111.com -I
HTTP/1.1 200 OK
Date: Sun, 08 Oct 2017 15:50:41 GMT
Server: Apache/2.4.27 (Unix) PHP/7.1.6
X-Powered-By: PHP/7.1.6
Content-Type: text/html; charset=UTF-8
[root@localhost ~]# curl -x127.0.0.1:80 -uaming:123456 111.com
111.com[root@localhost ~]#
```
- 故意输错密码,又是这样
```
111.com[root@localhost ~]# curl -x127.0.0.1:80 -uaming:12345 111.com


401 Unauthorized

Unauthorized
This server could not verify that you
are authorized to access the document
requested.  Either you supplied the wrong
credentials (e.g., bad password), or your
browser doesn't understand how to supply
the credentials required.

[root@localhost ~]#
```
- 关于用户认证还有另一种需求,不想针对所有的目录,一个网站总有一个敏感信息,比如一些后台访问的数据,做一个认证,针对某一个文件,做一个认证
- 打开配置文件
```

    DocumentRoot "/data/wwwroot/111.com"
    ServerName 111.com
    ServerAlias www.example.com
   #
     
        AllowOverride AuthConfig
        AuthName "111.com user auth"
        AuthType Basic
        AuthUserFile /data/.htpasswd
        require valid-user
     
   #
    ErrorLog "logs/111.com-error_log"
    CustomLog "logs/111.com-access_log" common

:wq                    
```
- 把diretory 注释掉,换上
```
[root@localhost ~]# !vi
vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
[root@localhost ~]# /usr/local/apache2.4/bin/apachectl -t
[root@localhost ~]# /usr/local/apache2.4/bin/apachectl graceful
[root@localhost ~]# vim /data/wwwroot/111.com/123.php

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-636472-1-1.html 上篇帖子: MacOSX apache 无法访问服务器 下篇帖子: 配置Apache支持HTTPS
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表