标签:basic wro iso 图片 access 加载 lib 调整 php
11.18 Apache用户认证[root@taoyuan ~]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
#内容参数如下:
<VirtualHost *:80>
DocumentRoot "/data/wwwroot/123.com"
ServerName 123.com
<Directory /data/wwwroot/123.com>
AllowOverride AuthConfig
AuthName "123.com user auth"
AuthType Basic
AuthUserfile /data/.htpasswd
require valid-user
</Directory>
ServerAlias www.123.com
ErrorLog "logs/123.com-error_log"
CustomLog "logs/123.com-access_log" common
</VirtualHost>
#含义说明
<Directory /data/wwwroot/123.com> #指定认证的目录
AllowQverride AuthConfig #这个相当于打开认证的开关
AuthName "123.com user auth" #自定义认证的名字,作用不大
AuthType Basic #认证 的类型,一般为Basic
AuthUserFile /data/.htpasswd #指定密码文件所在位置
require valid-user #指定需要认证的用户为全部可用用户
[root@taoyuan ~]# /usr/local/apache2.4/bin/htpasswd -cm /data/.htpasswd ayun
# -c 表示创建 -m 指定MD5加密 指定所在位置 如果再次增加用户可以不用-c选项,-c是首次创建文件使用的
[root@taoyuan ~]# cat /data/.htpasswd
ayun:$apr1$0bRU1d91$qRqDfW4bvxeHhShhtSCKs1
[root@taoyuan ~]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[root@taoyuan ~]# /usr/local/apache2.4/bin/apachectl graceful
[root@localhost ~]# curl -x127.0.0.1:80 www.123.com -I
HTTP/1.1 401 Unauthorized #状态码401
Date: Thu, 21 Dec 2017 02:49:11 GMT
Server: Apache/2.4.28 (Unix) PHP/7.1.6
WWW-Authenticate: Basic realm="123.com user auth"
Content-Type: text/html; charset=iso-8859-1
#此时提示状态码为“401”,说明当前所访问的内容需要进行用户认证。
# -u 验证用户和密码
[root@localhost ~]# curl -x127.0.0.1:80 -uayun:ayunayun www.123.com -I
HTTP/1.1 200 OK
Date: Thu, 21 Dec 2017 02:50:23 GMT
Server: Apache/2.4.28 (Unix) PHP/7.1.6
X-Powered-By: PHP/7.1.6
Content-Type: text/html; charset=UTF-8
#状态码“200”,即访问成功。
对网站中指定文件设置用户认证
<VirtualHost *:80>
#ServerAdmin webmaster@dummy-host.example.com
DocumentRoot "/data/wwwroot/123.com"
ServerName 123.com
#<Directory /data/wwwroot/123.com> 注释Directory标签
<FilesMatch 123.php> #改成FilesMatch标签
AllowOverride AuthConfig
AuthName "123.com user auth"
AuthType Basic
AuthUserfile /data/.htpasswd
require valid-user
</FilesMatch>
#</Directory>
ServerAlias www.123.com
ErrorLog "logs/123.com-error_log"
CustomLog "logs/123.com-access_log" common
</VirtualHost>
#创建123.php文件
[root@taoyun ~]# echo ‘<?php echo "abc.cm/123.php " ?>‘ > /data/wwwroot/123.com/123.php
#检测配置文件是否错误
[root@localhost ~]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
#重新加载配置文件
[root@localhost ~]# /usr/local/apache2.4/bin/apachectl graceful
#cur测试
[root@localhost ~]# curl -x127.0.0.1:80 123.com
123.com[root@localhost ~]#
[root@localhost ~]# curl -x127.0.0.1:80 123.com/123.php -I
HTTP/1.1 401 Unauthorized
Date: Thu, 21 Dec 2017 03:11:17 GMT
Server: Apache/2.4.28 (Unix) PHP/7.1.6
WWW-Authenticate: Basic realm="123.com user auth"
Content-Type: text/html; charset=iso-8859-1
#访问123.com/目录下的123.php 报错:401,需要用户认证
#使用制定用户名&密码访问
[root@localhost ~]# curl -x127.0.0.1:80 -uayun:ayunayun 123.com/123.php
<h1>123.php</h1><hr><p> www.123.com/123.php </p>[root@localhost ~]#
浏览器访问
注:在访问123.php时,需要输入认证用户名&密码。
#配置文件
[root@localhost ~]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
#配置参数
<VirtualHost *:80>
DocumentRoot "/data/wwwroot/123.com"
ServerName 123.com
<IfModule mod_rewrite.c>
#需要mod_rewrite的支持
RewriteEngine on
#开启rewrite功能
RewriteCond %{HTTP_HOST} !^123.com$
#Cond=condition,定义rewrite条件:所有非123.com的主机名(域名)
RewriteRule ^/(.*)$ http://123.com/$1 [R=301,L]
#定义rewrite规则:当满足上面条件时才执行当前规则,即跳转到111.com。
</IfModule>
ServerAlias www.abc.com
ErrorLog "logs/123.com-error_log"
CustomLog "logs/123.com-access_log" common
</VirtualHost>
检查系统配置
[root@localhost ~]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[root@localhost ~]# /usr/local/apache2.4/bin/apachectl graceful
#检查Apache是否加载了配置中调用的rewrite模块,如果没有加载,需要编辑Apache配置文件“httpd.conf”
LoadModule alias_module modules/mod_alias.so
LoadModule rewrite_module modules/mod_rewrite.so #注释去掉
LoadModule php7_module modules/libphp7.so
查看是否有加载模块
[root@localhost ~]# /usr/local/apache2.4/bin/apachectl -M |grep rewrite
rewrite_module (shared)
[root@localhost ~]# curl -x127.0.0.1:80 www.abc.com -I
HTTP/1.1 301 Moved Permanently
Date: Thu, 21 Dec 2017 03:46:09 GMT
Server: Apache/2.4.28 (Unix) PHP/7.1.6
Location: http://123.com/
Content-Type: text/html; charset=iso-8859-1
状态码为301,即设定了域名永久跳转
在浏览器进行检测时,访问“www.example.com”会直接跳转到“123.com”。
[root@localhost ~]# ls /usr/local/apache2.4/logs
111.com-access_log 123.com-access_log access_log httpd.pid
111.com-error_log 123.com-error_log error_log
#查看日志记录
[root@taoyun ~]# cat /usr/local/apache2.4/logs/abc.com-access_log
192.168.1.222 - - [05/Jan/2018:16:09:39 +0800] "GET /favicon.ico HTTP/1.1" 404 209
192.168.1.222 - - [05/Jan/2018:16:09:40 +0800] "GET /.php HTTP/1.1" 404 202
192.168.1.222 - - [05/Jan/2018:16:09:47 +0800] "GET /index.html HTTP/1.1" 404 208
192.168.1.222 - - [05/Jan/2018:16:09:50 +0800] "GET /index.html HTTP/1.1" 404 208
192.168.1.222 - - [05/Jan/2018:16:12:35 +0800] "GET /favicon.ico HTTP/1.1" 404 209
192.168.1.222 - - [05/Jan/2018:16:12:35 +0800] "GET /favicon.ico HTTP/1.1" 404 209
#文件路径
[root@taoyun ~]# vim /usr/local/apache2.4/conf/httpd.conf
#参数注解
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %b" common
#h表示host来源IP
#l表示login用户
#u表示user用户密码
#t表示time时间
#r表示request(行为)
#s表示status状态码
#b表示byte大小
#user-agent:用户代理
#referer:跳转到当前位置的上一个网址(即:提供当前IP的网站)
即:有combine和common两种格式,默认使用common模式
截图如下:
编辑配置文件“httpd-vhosts.conf”
[root@taoyun ~]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
重新加载
[root@localhost ~]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[root@localhost ~]# /usr/local/apache2.4/bin/apachectl graceful
标签:basic wro iso 图片 access 加载 lib 调整 php
原文地址:http://blog.51cto.com/3622288/2058618