一、apache用户认证
- 1.对目录加密
vim /usr/local/apache2.4/bin/apachectl start
vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf //把123.com那个虚拟主机编辑成如下内容
<VirtualHost *:80>
DocumentRoot "/data/wwwroot/www.123.com"
ServerName www.123.com
<Directory /data/wwwroot/123.com> //指定认证的目录
AllowOverride AuthConfig //这个相当于打开认证的开关
AuthName "123.com user auth" //自定义认证的名字,作用不大
AuthType Basic //认证的类型,一般为Basic
AuthUserFile /data/.htpasswd //指定密码文件所在位置
require valid-user //指定需要认证的用户为全部可用用户
</Directory>
</VirtualHost> - 如下,在之前基础上修改
<Directory /data/wwwroot/123.com> AllowOverride AuthConfig AuthName "123.com user auth" AuthType Basic AuthUserFile /data/.htpasswd require valid-user </Directory>
curl -x127.0.0.1:80 123.com
- 创建用户密码
/usr/local/apache2.4/bin/htpasswd -c -m /data/.htpasswd chinantfy
- 创建/data/.htpasswd 文件并建立用户,密码一md5加密,以后增加用户时只需要-m选项即可
/usr/local/apache2.4/bin/apachectl -t /usr/local/apache2.4/bin/apachectl graceful
用浏览器登陆测试
curl -x127.0.0.1:80 www.123.com //状态码为401 curl -x127.0.0.1:80 -uaming:passwd www.123.com -I //状态码为200
- 2.对单一文件加密
将之前配置文件修改为<FilesMatch admin.php> AllowOverride AuthConfig AuthName "123.com user auth" AuthType Basic AuthUserFile /data/.htpasswd require valid-user </FilesMatch>
然后创建admin.php文件
vim /data/wwwroot/123.com/admin.php
<?php
echo "admin":
/usr/local/apache2.4/bin/apachectl -t
/usr/local/apache2.4/bin/apachectl graceful
用浏览器测试
二、域名跳转
需求,把www.good.com域名跳转到www.123.com,配置如下:
vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
<VirtualHost :80>
DocumentRoot "/data/wwwroot/www.123.com"
ServerName www.123.com
ServerAlias 123.com www.good.com
<IfModule mod_rewrite.c> //需要mod_rewrite模块支持
RewriteEngine on //打开rewrite功能
RewriteCond %{HTTP_HOST} !^www.123.com$ //定义rewrite的条件,主机名(域名)不是www.123.com满足条件
RewriteRule ^/(.)$ http://www.123.com/$1 [R=301,L] //定义rewrite规则,当满足上面的条件时,这条规则才会执行,301永久重定向,302临时重定向,通常用301
</IfModule>
</VirtualHost>
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.123.com$
RewriteRule ^/(.*)$ http://www.123.com/$1 [R=301,L]
</IfModule>
/usr/local/apache2/bin/apachectl -M|grep -i rewrite //若无该模块,需要编辑配置文件httpd.conf,删除rewrite_module (shared) 前面的#
/usr/local/apache2.4/bin/apachectl -t
/usr/local/apache2.4/bin/apachectl graceful
curl -x127.0.0.1:80 -I www.good.com //状态码为301
三、访问日志
访问日志记录用户的每一个请求
vim /usr/local/apache2.4/conf/httpd.conf //搜索LogFormat
可以看到log有两种格式,之前默认是common
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %b" common
把虚拟主机配置文件改成如下:
<VirtualHost *:80>
DocumentRoot "/data/wwwroot/www.123.com"
ServerName www.123.com
ServerAlias 123.com
CustomLog "logs/123.com-access_log" combined
</VirtualHost>
重新加载配置文件 -t,graceful
/usr/local/apache2.4/bin/apachectl -t
/usr/local/apache2.4/bin/apachectl graceful
原文地址:http://blog.51cto.com/13569831/2103779