操作环境RedHat5.8
这里面是会说到httpd服务器的用户页面验证,目录容器+访问控制,香港服务器,错误页面提示,用户的个人空间,还有基于端口,ip,和域名的虚拟主机,关于基础的部分,可以看上一篇博客www(apache)服务器的常规配
一.httpd的用户验证
给apache目录加访问控制 必须输入用户名密码才能访问
首先要开启Apache mod_auth 模块 或者 auth_basic //默认是开启的
1.
[root@tx1 ~]# vim /etc/httpd/conf/httpd.conf
327 AllowOverride None //是否开启验证(类型:1.all:全部的权限均可被复写, 2.authconfig: 仅有网页认证(账号密码)可复写,3.indexes:仅允许indexes方面的复写,4,服务器空间,limits:允许用户利用allow,deny与order管理可浏览的权限,none:不可复写,即.htaccess文件失效)
328 authname "hello" //验证的名字
329 authtype basic //验证的类型
330 authuserfile "/var/www/html/.htpasswd" //验证时要读的文件
331 require valid-user //只有保存在文件中的用户才是合法的
注:如果在主配置文件/etc/httpd/conf/httpd.conf中,这样编写无论AllowOverride all/none,验证都会实现,如果没有在主配置文件里写这个,而采用其他文件验证,只有当AllowOverride all时成立
[root@tx1 html]# htpasswd -c .htpasswd a1 //创建一个用户,第一需要加上-c选项,为创建一个加密的文件
New password:
Re-type new password:
Adding password for user a1
[root@tx1 html]# htpasswd .htpasswd a2 //第二次就可以去掉这个-c的选项了
New password:
Re-type new password:
Adding password for user a2
[root@tx1 html]# cat .htpasswd
a1:p20QloCycdD1.
a2:CT5QlTslMlmV2
[root@tx1 html]# echo nihao > index.html
[root@tx1 html]# service httpd restart
Stopping httpd: [FAILED]
Starting httpd: [ OK ]
测试:
2.使用其他文件验证
[root@tx1 ~]# vim /etc/httpd/conf/httpd.conf
327 AllowOverride all //记住这里要开启哦!之前做的删掉
402 AccessFileName .htaccess //这里指的是验证的文件名
[root@tx1 html]# vim .htaccess
authname "hello"
authtype basic
authuserfile "/var/www/html/.htpasswd"
require valid-user
[root@tx1 html]# rm .htpasswd
rm: remove regular file `.htpasswd'? y
[root@tx1 html]# htpasswd -c .htpasswd t1
New password:
Re-type new password:
Adding password for user t1
[root@tx1 html]# service httpd restart
测试:
二.目录容器+访问控制
1. 好处:方便管理, 网站中的所有网页和相关文件都要存放在主目录下,为了对文件进行归类整理,也可以在主目录下面建立子文件夹,分别存放不同内容的文件
自定义一个目录容器:
[root@tx1 html]# vim /etc/httpd/conf/httpd.conf
<directory "/var/www/html/123">
options indexes
authname "456"
authtype basic
authuserfile "/var/www/html/123/.htpasswd"
require valid-user
order allow,deny
allow from all
</directory>
[root@tx1 123]# pwd
/var/www/html/123
[root@tx1 123]# htpasswd -c .htpasswd tt
New password:
Re-type new password:
Adding password for user tt
测试:
2.访问控制
order allow,deny
allow from all
allow 允许deny 拒绝
allow,deny :以allow指定的客户端能够访问,没有允许的全部deny-拒绝访问
deny,allow :以deny指定的客户端拒绝访问,没有拒绝的客户端allow-都能访问
(1)黑名单:
order deny,allow
deny from 拒绝的客户端
(2)白名单:
order allow,deny
allow from 允许的客户端
例:
根据上一个目录容器添加一个访问控制
[root@tx1 123]# vim /etc/httpd/conf/httpd.conf
<directory "/var/www/html/123">
options indexes
authname "456"
authtype basic
authuserfile "/var/www/html/123/.htpasswd"
require valid-user
order allow,deny
allow from 127.0.0.1 192.168.8.71
</directory>
[root@tx1 123]# service httpd restart
Stopping httpd: [ OK ]
Starting httpd: [ OK ]
测试:
192.168.8.71
192.168.8.72
(1)白名单
order allow,deny
allow from 127.0.0.1 192.168.8.71
只有本机和192.168.8.71可以访问
192.168.8.71
192.168.8.72
(2)黑名单:
order deny,allow
deny from 127.0.0.1 192.168.8.71
只有本机和192.168.8.71不可以访问,这里就不做测试了
三.错误页面提示
1xx 请求被接受,临时响应
2xx 成功
3xx 客户端与服务器建立连接的时候,还需要输入一些其他的内容
4xx 客户端请求的时候出现错误 ××××
5xx 服务器的内部错误 ××××
例:
403 是没有权限
404 是文件找不到
更详细的某网友的总结
[root@tx1 html]# vim /etc/httpd/conf/httpd.conf
<directory "/var/www/html/test">
options indexes
errordocument 404 /error/404.html
errordocument 403 /error/403.html
order allow,deny
allow from all
</directory>
837 Alias /error/ "/var/www/error/" //错误文件的位置
[root@tx1 test]# cd /var/www/error/
[root@tx1 error]# vim 403.html
Permission denied
[root@tx1 error]# vim 404.html
can not found file
[root@tx1 html]# mkdir test
[root@tx1 html]# cd test
[root@tx1 test]# ls
tx
[root@tx1 test]# chmod 440 tx
[root@tx1 test]# echo nihao > tx
测试:
另外还可以添加图片的错误提示,这个目录下保存着图片
[root@tx1 html]# vim /etc/httpd/conf/httpd.conf
<directory "/var/www/html/test">
options indexes
#errordocument 404 /error/404.html
errordocument 404 /icons/world2.png
errordocument 403 /error/403.html
order allow,deny
allow from all
</directory>
[root@tx1 html]# service httpd restart
Stopping httpd: [ OK ]
Starting httpd: [ OK ]
测试:
四.用户的个人空间
实现用户个人主页的功能是通过UserDir来实现的
[root@tx1 html]# vim /etc/httpd/conf/httpd.conf
357 <IfModule mod_userdir.c>
358 #
359 # UserDir is disabled by default since it can confirm the presence
360 # of a username on the system (depending on home directory
361 # permissions).
362 #
363 UserDir public_html //默认是关闭的,开启
364 <directory "/home/u1/public_html">
365 options indexes
366 order allow,deny
367 allow from all //真是环境相要做好访问控制,这里做个试验
368 </directory>
[root@tx1 ~]# useradd tx
[root@tx1 ~]# cd ~tx
[root@tx1 tx]# mkdir public_html
[root@tx1 tx]# cd public_html/
[root@tx1 public_html]# echo WELCOME > index.html
[root@tx1 public_html]# service httpd restart
Stopping httpd: [ OK ]
Starting httpd: [ OK ]
启动的同时监控一下日志
[root@tx1 httpd]# cd /var/log/httpd/
[root@tx1 httpd]# tail -0f error_log
[Wed Feb 06 05:35:34 2013] [error] [client 192.168.8.71] (13)Permission denied: access to /~tx/ denied
这里说明httpd用户不能够进入
[root@tx1 tx]# chmod +x . //修改tx家目录的权限,让用户能够进入
[root@tx1 tx]# service httpd restart
Stopping httpd: [ OK ]
Starting httpd: [ OK ]
测试
五.基于三种方式的虚拟主机(ip,香港服务器,端口,域名(最常用的))
值不值得,真是不足为外人道,自己心里有数就行。