码迷,mamicode.com
首页 > Web开发 > 详细

84.LAMP的apache用户认证,域名跳转,日志文件

时间:2018-03-04 14:37:26      阅读:216      评论:0      收藏:0      [点我收藏+]

标签:用户认证   域名跳转   日志   

apache用户认证

PS:其实没有....用处的。。。

命令描述

htpasswd命令是Apache的Web服务器内置工具,用于创建和更新储存用户名、域和用户基本认证的密码文件。

命令语法

htpasswd [option] [参数]

命令选项

-c:=create,创建一个加密文件
-n:不更新加密文件,只将更新后的用户名密码显示在屏幕上
-m:使用MD5算法对密码进行加密(默认)
-d:使用CRYPT算法对密码进行加密
-p:不对密码进行加密,即明文密码
-s:使用SHA算法对密码进行加密
-b:在命令行一并输入用户名和密码,而不是根据提示输入密码
-D:删除指定用户

步骤如下

1,编辑虚拟主机配置文件

[root@localhost ~]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
<VirtualHost *:80>
DocumentRoot "/data/www/sdw.com"
ServerName sdw.com
ServerAlias www.example.com
<Directory /data/www/sdw.com> //指定认证的目录
AllowOverride AuthConfig //这个相当于打开认证的开关
AuthName "sdw.com user auth" //自定义认证的名字,作用不大
AuthType Basic //认证的类型,一般为Basic
AuthUserFile /data/.htpasswd //指定密码文件所在位置
require valid-user //指定需要认证的用户为全部可用用户
</Directory>
ErrorLog "logs/111.com-error_logo"
CustomLog "logs/111.com-access_log" common
</VirtualHost>

PS:在配置的时候最好把说明去除,以防报错

2,创建“httpd-vhosts.conf”中指定的密码文件

[root@dl-001 wwwroot]# /usr/local/apache2.4/bin/htpasswd -c -m /data/.htpasswd dl
New password:
Re-type new password:
Adding password for user dl
[root@dl-001 wwwroot]# cat /data/.htpasswd
dl:$apr1$QaOr7opI$AfAGBv1/utJws62.S/sbl.

PS:在“/data/.htpasswd”为用户dl创建一个使用MD5算法加密的密码文件。

3,重新加载

[root@dl-001 wwwroot]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[root@dl-001 wwwroot]# /usr/local/apache2.4/bin/apachectl graceful


4,测试

使用curl进行测试
[root@dl-001 ~]# curl -x192.168.0.168:80 sdw.com
HTTP/1.1 401 Authorization Required //说明:因为生成了密码,所以在不指定用户名和密码的情况下会报401错误
Date: Wed, 20 Dec 2017 14:48:52 GMT
Server: Apache/2.2.34 (Unix) DAV/2 PHP/5.6.30
WWW-Authenticate: Basic realm="sdw.com user auth"
Content-Type: text/html; charset=iso-8859-1


域名跳转

描述

域名跳转类似于将网页重新指向另一个网站,但区别是域名跳转会将域名本身重新指向网站,而不使用HTML或脚本来进行重新指向。当域名被设置为跳转至另一网站,域名的地址将不会保留在浏览器的URL栏中,该栏显示的会是新页面的URL。如果您希望保留该栏中的URL,则需要使用隐形跳转。
把www.sdw.com 跳转到 sdw.com上

1.修改配置文件

<VirtualHost *:80>
ServerAdmin 1239666161@qq.com
DocumentRoot "/data/www/sdw.com"
ServerName sdw.com
ServerAlias www.sdw.com
<IfModule mod_rewrite.c>
#//需要mod_rewrite模块支持
RewriteEngine on

//打开rewrite功能

    RewriteCond %{HTTP_HOST} !^sdw.com$

#//定义rewrite的条件,主机名(域名)不是www.123.com满足条件
RewriteRule ^/(.*)$ http://sdw.com/$1 [R=301,L]
#//定义rewrite规则,当满足上面的条件时,这条规则才会执行
</IfModule>
<Directory /data/www/sdw.com>
AllowOverride AuthConfig
AuthName "123.com user auth"
AuthType Basic
AuthUserFile /data/.htpasswd
require valid-user
</Directory>
ErrorLog "logs/sdw.com-error_log"
CustomLog "logs/sdw.com-access_log" common
</VirtualHost>

2.修改配置

[root@localhost ~]# vim /usr/local/apache2/conf/httpd.conf

LoadModule rewrite_module modules/mod_rewrite.so //去掉#,以启用这个模块

3.测试结果

[root@localhost ~]# curl -x 192.168.0.168:80 www.sdw.com -I
HTTP/1.1 301 Moved Permanently
Date: Sat, 03 Mar 2018 15:11:58 GMT
Server: Apache/2.4.29 (Unix) PHP/5.6.30
Location: http://sdw.com/
Content-Type: text/html; charset=iso-8859-1


apache日志文件

通过优化 可以更加方便进行查询日志

1、Apache访问日志所在位置:

[root@localhost ~]# ls /usr/local/apache2/logs/
123test-access_log  abstest-error_log                   dummy-host2.example.com-error_log  error_log
123test-error_log   access_log                          dummy-host.example.com-access_log  httpd.pid
abctest-access_log  dummy-host2.example.com-access_log  dummy-host.example.com-error_log
[root@localhost ~]#cat  /usr/local/apache2/logs/123test-access_log     //common格式日志
192.168.204.128 - - [02/Mar/2018:19:06:28 +0800] "HEAD HTTP://linuxtestbak.com/ HTTP/1.1" 301 -
192.168.204.128 - - [02/Mar/2018:19:07:51 +0800] "GET HTTP://linuxtest.com/ HTTP/1.1" 200 28
192.168.204.128 - - [02/Mar/2018:19:09:05 +0800] "HEAD HTTP://www.linuxtestbak.com/ HTTP/1.1" 301 -
192.168.204.1 - - [02/Mar/2018:19:10:55 +0800] "GET / HTTP/1.1" 200 28
192.168.204.1 - - [02/Mar/2018:19:11:08 +0800] "GET / HTTP/1.1" 200 28

2、查看日志格式

[root@localhost ~]# vim /usr/local/apache2/conf/httpd.conf      //搜索LogFormat

<IfModule log_config_module>
    #
    # The following directives define some format nicknames for use with
    # a CustomLog directive (see below).
    #
    LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
    LogFormat "%h %l %u %t \"%r\" %>s %b" common
说明:combined和common两种格式,默认使用common格式。

## 3、更改日志的格式为combined
[root@localhost ~]# vim /usr/local/apache2/conf/extra/httpd-vhosts.conf

    ErrorLog "logs/123test-error_log"
    CustomLog "logs/123test-access_log" combined

[root@localhost ~]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
## 4,做一些访问操作之后,再查看日志。
[root@localhost ~]#  /usr/local/apache2.4/bin/apachectl graceful

[root@localhost ~]# cat  /usr/local/apache2.4/logs/123test-access_log 
192.168.204.128 - - [02/Mar/2018:19:06:28 +0800] "HEAD HTTP://linuxtestbak.com/ HTTP/1.1" 301 -
192.168.204.128 - - [02/Mar/2018:19:07:51 +0800] "GET HTTP://linuxtest.com/ HTTP/1.1" 200 28
192.168.204.128 - - [02/Mar/2018:19:09:05 +0800] "HEAD HTTP://www.linuxtestbak.com/ HTTP/1.1" 301 -
192.168.204.1 - - [02/Mar/2018:19:10:55 +0800] "GET / HTTP/1.1" 200 28
192.168.204.1 - - [02/Mar/2018:19:11:08 +0800] "GET / HTTP/1.1" 200 28
192.168.204.1 - - [02/Mar/2018:19:20:16 +0800] "GET / HTTP/1.1" 200 28 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36"
192.168.204.1 - - [02/Mar/2018:19:20:19 +0800] "GET / HTTP/1.1" 200 28 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36"
192.168.204.1 - - [02/Mar/2018:19:20:27 +0800] "GET / HTTP/1.1" 200 28 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36"
192.168.204.128 - - [02/Mar/2018:19:20:39 +0800] "HEAD HTTP://www.linuxtestbak.com/ HTTP/1.1" 301 - "-" "curl/7.29.0"
192.168.204.128 - - [02/Mar/2018:19:20:45 +0800] "HEAD HTTP://www.linuxtestbak.com/ HTTP/1.1" 301 - "-" "curl/7.29.0"
192.168.204.128 - - [02/Mar/2018:19:20:54 +0800] "GET HTTP://linuxtest.com/ HTTP/1.1" 200 28 "-" "curl/7.29.0"
192.168.204.128 - - [02/Mar/2018:19:20:57 +0800] "GET HTTP://linuxtest.com/ HTTP/1.1" 200 28 "-" "curl/7.29.0"
192.168.204.128 - - [02/Mar/2018:19:20:58 +0800] "GET HTTP://linuxtest.com/ HTTP/1.1" 200 28 "-" "curl/7.29.0"

84.LAMP的apache用户认证,域名跳转,日志文件

标签:用户认证   域名跳转   日志   

原文地址:http://blog.51cto.com/sdwaqw/2082716

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!