kevin0490 发表于 2018-11-18 06:21:55

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

apache用户认证


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

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

命令语法
  htpasswd [参数]

命令选项

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

步骤如下

1,编辑虚拟主机配置文件
  # vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf

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

ErrorLog "logs/111.com-error_logo"
CustomLog "logs/111.com-access_log" common


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

2,创建“httpd-vhosts.conf”中指定的密码文件
  # /usr/local/apache2.4/bin/htpasswd -c -m /data/.htpasswd dl
New password:
Re-type new password:
Adding password for user dl
# cat /data/.htpasswd
dl:$apr1$QaOr7opI$AfAGBv1/utJws62.S/sbl.

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

3,重新加载
  # /usr/local/apache2.4/bin/apachectl -t
Syntax OK
# /usr/local/apache2.4/bin/apachectl graceful

4,测试
  使用curl进行测试
# 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.修改配置文件
  
ServerAdmin 1239666161@qq.com
DocumentRoot "/data/www/sdw.com"
ServerName sdw.com
ServerAlias www.sdw.com

#//需要mod_rewrite模块支持
RewriteEngine on

//打开rewrite功能

    RewriteCond %{HTTP_HOST} !^sdw.com$
  #//定义rewrite的条件,主机名(域名)不是www.123.com满足条件
RewriteRule ^/(.*)$ http://sdw.com/$1
#//定义rewrite规则,当满足上面的条件时,这条规则才会执行


AllowOverride AuthConfig
AuthName "123.com user auth"
AuthType Basic
AuthUserFile /data/.htpasswd
require valid-user

ErrorLog "logs/sdw.com-error_log"
CustomLog "logs/sdw.com-access_log" common


2.修改配置
  #vim /usr/local/apache2/conf/httpd.conf
  LoadModule rewrite_module modules/mod_rewrite.so   //去掉#,以启用这个模块

3.测试结果
  # 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访问日志所在位置:

# ls /usr/local/apache2/logs/
123test-access_logabstest-error_log                   dummy-host2.example.com-error_logerror_log
123test-error_log   access_log                        dummy-host.example.com-access_loghttpd.pid
abctest-access_logdummy-host2.example.com-access_logdummy-host.example.com-error_log
#cat/usr/local/apache2/logs/123test-access_log   //common格式日志
192.168.204.128 - - "HEAD HTTP://linuxtestbak.com/ HTTP/1.1" 301 -
192.168.204.128 - - "GET HTTP://linuxtest.com/ HTTP/1.1" 200 28
192.168.204.128 - - "HEAD HTTP://www.linuxtestbak.com/ HTTP/1.1" 301 -
192.168.204.1 - - "GET / HTTP/1.1" 200 28
192.168.204.1 - - "GET / HTTP/1.1" 200 28
2、查看日志格式

# vim /usr/local/apache2/conf/httpd.conf      //搜索LogFormat

#
# 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
# vim /usr/local/apache2/conf/extra/httpd-vhosts.conf
ErrorLog "logs/123test-error_log"
CustomLog "logs/123test-access_log" combined
# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
## 4,做一些访问操作之后,再查看日志。
#/usr/local/apache2.4/bin/apachectl graceful
# cat/usr/local/apache2.4/logs/123test-access_log
192.168.204.128 - - "HEAD HTTP://linuxtestbak.com/ HTTP/1.1" 301 -
192.168.204.128 - - "GET HTTP://linuxtest.com/ HTTP/1.1" 200 28
192.168.204.128 - - "HEAD HTTP://www.linuxtestbak.com/ HTTP/1.1" 301 -
192.168.204.1 - - "GET / HTTP/1.1" 200 28
192.168.204.1 - - "GET / HTTP/1.1" 200 28
192.168.204.1 - - "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 - - "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 - - "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 - - "HEAD HTTP://www.linuxtestbak.com/ HTTP/1.1" 301 - "-" "curl/7.29.0"
192.168.204.128 - - "HEAD HTTP://www.linuxtestbak.com/ HTTP/1.1" 301 - "-" "curl/7.29.0"
192.168.204.128 - - "GET HTTP://linuxtest.com/ HTTP/1.1" 200 28 "-" "curl/7.29.0"
192.168.204.128 - - "GET HTTP://linuxtest.com/ HTTP/1.1" 200 28 "-" "curl/7.29.0"
192.168.204.128 - - "GET HTTP://linuxtest.com/ HTTP/1.1" 200 28 "-" "curl/7.29.0"


页: [1]
查看完整版本: 84.LAMP的apache用户认证,域名跳转,日志文件