首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 操作系统 > Linux > Apache 安全設定
【标  题】:Apache 安全設定
【关键字】:Apache
【来  源】:http://www.cublog.cn/u/9817/showart.php?id=199203

Apache 安全設定

預設安裝好 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 時就會出現認證畫面了

 

基于名字的虚拟主机:【上一篇】
URL重写的若干非mod_rewrite方法:【下一篇】
【相关文章】
  • Lession 2 : 跨越 IDE 的 Apache Ant
  • 【服务配置】apache+tomcat配置负载均衡的网站
  • IIS 6和Apache下Request Header的最大值
  • apache for asp
  • Apache开源项目分类列表
  • apache的内存池与内存分配(1)
  • 终于把mysql、apache、php给安装好了
  • Resin+apache出的问题
  • Apache AXIS 开发 Web Services
  • apache之configure解析
  • 【随机文章】
  • 对论坛中有关数据类型转换的整理(ZZ)
  • Win32调试API 第一部分
  • WML开发教程
  • NAT后面的pptpd不要虚拟eth0:0
  • Solaris网络配置
  • embed标签的使用(在网页中播放各种音频视频的插件的使用)
  • grep
  • SUNWEN教程之----C#进阶(十)
  • String 与wideString 的完美转换
  • 我的SCSA,part I是怎么考过的
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 软讯网络 All Rigths Reserved.