Apache 安全设定
AutoIndex
预设安装好 Apache 之后,其预设目录是在 /var/www/html/,如果没有设定 index.html 的话,那么就会印出目前目录里的所有档案和目录,基於安全理由,希望把
AutoIndex 这个取消,如此在别人打入网址后,就会出现 403 的存取权限不足,只有在很“明确”的指出档案时才可以浏览。
关闭 /var/www/html 里(含子目录)的自动印出首页功能
[root@rhel conf]# vi httpd.conf
_______________________________
<Directory "/var/www/html">
#把 Options Indexes FollowSymLinks 注解起来
#Options Indexes FollowSymLinks
#修改成只剩 FollowSymLinks
Options FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>
_______________________________
[root@rhel conf]# |
重新启动 httpd
[root@rhel conf]# service httpd restart
Stopping httpd: [ OK ]
Starting httpd: [ OK ]
[root@rhel conf]# |
虚拟目录
一般特别重要或有特别作用的目录,会放在 /var/www/html 之外的其它目录,比方说现在 /file/download/ 目录,要在使用者打入
http://www.abc.com.tw/download/ 时,可以自动对应到 /file/download/ 这个目录,就好像是在 /var/www/html
里面一样,这时我们可以使用 alias 的方式做出一个虚拟目录,使用 Alias 就可以办得到了。
[root@rhel conf]# vi httpd.conf
_______________________________
Alias /download/ "/file/download/"
_______________________________
[root@rhel conf]# service httpd restart
Stopping httpd: [ OK ]
Starting httpd: [ OK ]
[root@rhel conf]# |
注意:Alias 语法为 Alias {target} {source},在 {target}
的描述中(以 download),如果打了一个 "/" 符号,那么使用者在输入网址时只能是 http://www.abc.com.tw/download/
后面就要多带一个 "/",否则会出现 404 的找不到档案错误。
ServerName 的设定
其实 ServerName 在大部份的情况下可以不设定,但是如果有做重新导向的话,那么如果这个值不存在,Apache 在启动时会以 127.0.0.1
来做预设的 ServerName。因此等到有预到重新导向的情况时,就会出现问题。要更正这个问题,只要加入一个 ServerName 的值就可以了。
[root@rhel conf]# vi httpd.conf
_______________________________
ServerName www.abc.com.tw
_______________________________
[root@rhel conf]# service httpd restart
Stopping httpd: [ OK ]
Starting httpd: [ OK ]
[root@rhel conf]# |
修改预设目录
Apache 安装好之后,预设全是在 /var/www/html 里面,如果要修改这个目录,只要修改 DocumentRoot 就可以了。
[root@rhel conf]# vi httpd.conf
_______________________________
DocumentRoot "/project/web"
_______________________________
[root@rhel conf]# service httpd restart
Stopping httpd: [ OK ]
Starting httpd: [ OK ]
[root@rhel conf]# |
预设聆听埠
Apache 安装之后,会聆听 80 的标准 http 埠,但是当这台 http 有特别功用时,可能会修改其 Port,要修改 Port,只要修改
Listen 这个值就可以了。
[root@rhel conf]# vi httpd.conf
_______________________________
Listen 8080
_______________________________
[root@rhel conf]# service httpd restart
Stopping httpd: [ OK ]
Starting httpd: [ OK ]
[root@rhel conf]# |
注意:修改预设的聆听埠,如果在 Firewall 没有允许连入的情况下会出现连线不到的情形,所以在修改完之后还必需检查 firewall
是否允许。
保护你的目录资料 - 密码验证
是否想过,某些特别的目录,只有特别的人才可以进去,但又不想再设定 / 加装其它的功能,这时候就 Apache 可以提供你一个登入帐号密码的机制,只有通过的人才可以看到这“特别”的地区。
要编辑的档案:
- /etc/httpd/conf/httpd.conf (以 Redhat 为列)
- .htaccess (设定档)
- .htpasswd (帐号 / 密码档)
首先,要使用这个认证机制,就要允许可以做 AuthConfig,比方说我要对 http://www.abc.com.tw/secure_data/
这个 URL 做认证,那么就需要使用到 AuthConfig。
[root@rhel conf]# vi /etc/httpd/conf/httpd.conf
_______________________________________
# 做 Alias 到 /home/secure_data/
Alias "/secure_data" "/home/secure_data"
<Directory "/home/secure_data">
# 允许做 AuthConfig 的设定
AllowOverride AuthConfig </Directory>
_______________________________________
#重新启动 Apache
[root@rhel secure_data]# service httpd restart
Stopping httpd: [ OK ]
Starting httpd: [ OK ]
[root@rhel secure_data]# |
接下来,在 /home/secure_data/ 建立一个 .htaccess
[root@rhel conf]# cd /home/secure_data; touch .htaccess;
vi .htaccess
________________________________________
AuthName "ABC Secure Area"
AuthType Basic
# 指定帐号密码档的位置
AuthUserFile "/home/http_auth_users/.htpasswd"
require valid-user
________________________________________ |
最后一步,就是建立一个帐号密码档啦!承於本篇的忠旨-安全理由,所以千万不要把帐号 / 密码档这种高度敏感的档案放到 Public 的区域,尤其是可经由
http、ftp 等方式可取得的管道。本篇我们装放在 /home/http_auth_user/.htpasswd 里。
[root@rhel conf]# mkdir /home/http_auth_users; cd /home/http_auth_users;
touch .htpasswd |
档案建立好了,再来如何建立使用者呢?这时候就必需使用 htpasswd 这个指令来帮我们完成了!
htpasswd 说明:
htpasswd -{option} {passwd_file} {user}
option 可使用:
- -m 使用 MD5 编码
- -c 建立新的密码档
- -b 附带有 "密码" (不用再输入一次密码)
建立 steven 的帐号密码:
[root@mailgw http_auth_users]# htpasswd -m .htpasswd steven
New password:
Re-type new password:
Adding password for user steven
[root@mailgw http_auth_users]# |
现在开启您的 Broser,输入 http://{Your_URL}/{Need_Password_Dir} 本例为 http://www.abc.com.tw/secure_data
时就会出现认证画面了。
Last modified: 04/01/2005
03/13/2005
|