詳述CentOS 7中Apache配置與應用(一)

Apache連接保持

Apache連接保持相關參數

  • KeepAlive

    • 是否打開連接保持,OFF關閉,ON打開
  • KeepAlive' Timeout

    • 一次連接多次請求之間的最大間隔時間,兩次請求超過該時間連接斷開
  • MaxKeepAliveRequests
    • 一次連接能夠 傳輸的最大請求數量

Apache訪問控制

  • 作用

    • 控制對網站資源的訪問
    • 爲特定的網站目錄添加訪問授權
  • 常用訪問控制方式
    • 客戶機地址限制
    • 用戶授權限制

基於客戶端地址的訪問控制

  • 使用Require配置項實現訪問控制,按先後順序限制

  • 可用於<Location>、<Directory>、<Files>、 <Limit>配置段中

  • Require配置項的常見語法
Require all granted
Require all denied
Require local
Require [not] host <主機名或域名列表>
//使用not禁止訪問時要將其置於<RequireAll> </RequireAll>容器中並在容器中指定相應的限制策略
Require [not] ip <IP地址或網段列表>

配置實例

Linux系統中安裝DNS、HTTP服務,並設置DNS服務。

[root@localhost ~]# yum install bind httpd -y   //安裝服務
已加載插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
...//省略部分內容...
已安裝:
  bind.x86_64 32:9.11.4-9.P2.el7                    httpd.x86_64 0:2.4.6-90.el7.centos 
...//省略部分內容...  
完畢!
[root@localhost conf]# vim /etc/named.conf    //編輯DNS配置文件
...//省略部分內容...
options {
        listen-on port 53 { any; };            //更改IP地址爲any
        listen-on-v6 port 53 { ::1; };
        directory       "/var/named";
        dump-file       "/var/named/data/cache_dump.db";
        statistics-file "/var/named/data/named_stats.txt";
        memstatistics-file "/var/named/data/named_mem_stats.txt";
        recursing-file  "/var/named/data/named.recursing";
        secroots-file   "/var/named/data/named.secroots";
        allow-query     { any; };             //更改監聽主機爲any
...//省略部分內容... 
:wq
[root@localhost conf]# vim /etc/named.rfc1912.zones     //編輯區域配置文件
...//省略部分內容...
zone "kgc.com" IN {                      //更改域名
        type master;
        file "kgc.com.zone";         //更改區域數據文件名
        allow-update { none; };
};
...//省略部分內容...
:wq
[root@localhost conf]# cd /var/named/           //進入區域數據文件目錄
[root@localhost named]# ls                     //查看目錄
data  dynamic  named.ca  named.empty  named.localhost  named.loopback  slaves
[root@localhost named]# cp -p named.localhost kgc.com.zone     //複製區域數據文件
[root@localhost named]# vim kgc.com.zone               //進入編輯文件
$TTL 1D 
@       IN SOA  @ rname.invalid. (
                                        0       ; serial
                                        1D      ; refresh
                                        1H      ; retry
                                        1W      ; expire
                                        3H )    ; minimum
        NS      @
        A       127.0.0.1
www IN  A       192.168.144.133                   //設置域名解析
:wq                                        //保存退出

開啓兩臺win 10客戶機,並查看客戶機IP地址

詳述CentOS 7中Apache配置與應用(一)詳述CentOS 7中Apache配置與應用(一)

在Linux系統中進入http服務站點目錄,編輯主頁內容,並開啓DNS、HTTP服務,關閉防火牆及增強性安全功能

[root@localhost named]# cd /var/www/html/    //進入http服務站點目錄
[root@localhost html]# vim index.html             //編輯默認主頁
<h1>this is kgc web</h1>             //編輯內容
:wq
[root@localhost html]# ls           //查看
index.html              
[root@localhost html]# cat index.html    //查看網頁內容
<h1>this is kgc web</h1>
[root@localhost html]# systemctl start httpd.service         //啓動http服務
[root@localhost html]# systemctl start named                   //啓動DNS服務
[root@localhost html]# systemctl stop firewalld.service         //關閉防火牆
[root@localhost html]# setenforce 0                            //關閉增強性安全功能

使用兩臺win 10客戶機分別訪問往網站信息,看服務是否正常提供

詳述CentOS 7中Apache配置與應用(一)詳述CentOS 7中Apache配置與應用(一)詳述CentOS 7中Apache配置與應用(一)詳述CentOS 7中Apache配置與應用(一)

在Linux系統中配置HTTP服務配置文件,設置客戶機訪問權限

[root@localhost html]# vim /etc/httpd/conf/httpd.conf  
//編輯主配置文件內容(現網中不建議直接修改主配置文件內容,可以重新添加子配置文件進行限制)
...//省略部分內容...
<Directory "/var/www/html">
    #
    # Possible values for the Options directive are "None", "All",
    # or any combination of:
    #   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
    #
    # Note that "MultiViews" must be named *explicitly* --- "Options All"
    # doesn't give it to you.
    #
    # The Options directive is both complicated and important.  Please see
    # http://httpd.apache.org/docs/2.4/mod/core.html#options
    # for more information.
    #
    Options Indexes FollowSymLinks

    #
    # AllowOverride controls what directives may be placed in .htaccess files.
    # It can be "All", "None", or any combination of the keywords:
    #   Options FileInfo AuthConfig Limit
    #
    AllowOverride None

    #
    # Controls who can get stuff from this server.
    #
     <RequireALL>                          //在此容器下添加子容器
       Require not ip 192.168.144.128             
       //添加限制訪問主機的IP地址(如若限制網段直接添加192.168.144.0/24即可,注意限制網段需填寫子網掩碼)
       Require all granted
    </RequireALL>
</Directory>
...//省略部分內容...
:wq
[root@localhost html]# systemctl restart httpd.service 

查看限制的第一臺win 10客戶端是否還可以訪問網站

詳述CentOS 7中Apache配置與應用(一)詳述CentOS 7中Apache配置與應用(一)

用戶授權限制

配置實例

創建用戶認證數據庫

[root@localhost html]# htpasswd -c /etc/httpd/conf/pwd test01  
//創建用戶認證數據庫(-c爲創建,如果已經存在數據認證文件可以不用-c,直接就可以使用命令添加進認證文件中)
New password:               //輸入設置的密碼
Re-type new password:         //再次輸入密碼
Adding password for user test01  //成功創建
[root@localhost html]# cd /etc/httpd/conf        //進入目錄
[root@localhost conf]# ls        //查看
httpd.conf  magic  pwd            //成功創建文件
[root@localhost conf]# cat pwd     //查看文件內容
test01:$apr1$zDZ/54yz$rUCXaWixaltHE6ZBvjv0h/    //創建的用戶及密碼

添加用戶授權配置

[root@localhost conf]# vim httpd.conf
...//省略部分內容...
<Directory "/var/www/html">
    #
    # Possible values for the Options directive are "None", "All",
    # or any combination of:
    #   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
    #
    # Note that "MultiViews" must be named *explicitly* --- "Options All"
    # doesn't give it to you.
    #
    # The Options directive is both complicated and important.  Please see
    # http://httpd.apache.org/docs/2.4/mod/core.html#options
    # for more information.
    #
    Options Indexes FollowSymLinks

    #
    # AllowOverride controls what directives may be placed in .htaccess files.
    # It can be "All", "None", or any combination of the keywords:
    #   Options FileInfo AuthConfig Limit
    #
    AllowOverride None

    #
    # Controls who can get stuff from this server.
    #
   AuthName "DocumentRoot"                      //更改上面的訪問控制條目,此條目聲明驗證信息
   AuthType Basic                               //驗證類型爲基本驗證
   AuthUserFile /etc/httpd/conf/pwd             //驗證文件路徑
   Require valid-user                           //設置爲授權用戶驗證
</Directory>
...//省略部分內容...
:wq                                          //保存退出
[root@localhost conf]# systemctl restart httpd.service  //重新啓動服務

在客戶機中驗證配置

詳述CentOS 7中Apache配置與應用(一)詳述CentOS 7中Apache配置與應用(一)詳述CentOS 7中Apache配置與應用(一)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章