WEB應用之httpd基礎入門(四)

  • 2020 年 3 月 29 日
  • 筆記

  前文我們聊到了httpd的虛擬主機實現,狀態頁的實現,回顧請參考https://www.cnblogs.com/qiuhom-1874/p/12570900.html;今天我們來聊一聊後面的常用基礎配置;

  1、user/group:這兩個指令用於指定httpd進程的運行用戶和組

  示例:

   提示:以上配置就表示httpd啟動進程的用戶是身份是apache用戶和apache組;眾所周知一個進程要訪問某一個文件,它能對文件有哪些許可權,取決於進程是以那個用戶或屬組啟動的,以及該文件的屬主和屬組以及other許可權,如果啟動該進程的用戶和被訪問的文件用戶相同,那麼該進程對文件的許可權就是該文件中屬主的許可權,如果啟動進程的用戶和被訪問的文件用戶不同是,則就會看該進程啟動的屬組是否和文件屬組相同,相同則應用文件屬組的許可權,不同時則應用文件other許可權;如果文件有acl許可權,那麼也是同樣的道理,如果啟動進程的用戶在acl許可權列表中,就應用acl許可權列表中的許可權;

   提示:最上面的那個httpd進程是httpd的主控進程,主要管控下面的子進程的,所以它的用戶必須是root;

  2、使用mod_deflate模組壓縮頁面優化傳輸速度;

  在啟用壓縮功能的前提是我們httpd伺服器上必須要載入mod_deflate模組,其次我們還需要定義一個過濾器,然後明確的把那一類資源類型進行壓縮,通常情況文件和文本內心的資源壓縮比例最高,效果最好,圖片和一些二進位文件都不建議壓縮,圖片本來就是一個高度壓縮的,如果再壓縮可能存在圖片不可用的狀態;

  示例:

  首先確認mod_deflate模組是否啟用,如果沒有啟用使用loadmodule指令啟用

   提示:如果能夠過濾出來deflate_module,說明httpd是載入了該模組

  定義一個過濾器,定義過濾器的指令是setoutputfilter FILTER_NAME   過濾器的名稱可以說任意合法名稱,通常情況下我們使用DEFLATE作為過濾器的名稱,如下所示

   提示:該指令表示設置一個輸出文件過濾器,該指令可以用在server配置段中,虛擬主機配置段中,directory 和.htaccess中

  把某一資源類型(mime類型)添加到過濾器中,表示如果用戶訪問該類型資源啟用壓縮,這裡還需要注意一點的是,壓縮資源不是說一個字元也壓縮,通常情況頁面資源要達到某一大小後才可壓縮,因為資源太小,沒必要啟動壓縮,如果資源太小啟用壓縮,不但沒有起到加速的效果反而會降低響應速度;httpd里配置某一資源加入到過濾器中用addoutputfilterbytype指令指定過濾器名稱,以及資源的mime類型名稱;如下

   deflatecompressionlevel number:該指令表示指定deflate壓縮級別,範圍是1-9 ,9級別最高,壓縮後的文件更小,當然消耗的CPU資源就越多;

   提示:以上表示指定壓縮級別為5,配置了以上三個指令後,客戶端訪問伺服器就應該可以啟動壓縮功能了(資源必須要達到一定的大小才能壓縮,太小即便我們配置了壓縮功能,也不會壓縮)

  在沒有重載配置文件前,用瀏覽器訪問伺服器資源響應首部是這樣的

   提示:可以看到在沒有重載配置文件前,我們訪問伺服器資源是不壓縮的,伺服器靜態資源是多少位元組,響應報文對應的content-lenght的值就是多少;

  重新載入配置文件生效後,我們再用瀏覽器訪問同樣的資源,對應資源響應首部就沒有content-lenght首部,卻有了content-encoding首部,並告訴我們是使用的gzip壓縮;如下

   當然壓縮傳輸了,對應傳輸大小在哪裡呢?如下

   提示:可以看到壓縮後的資源傳輸只用了93.77KB,而該資源在伺服器上存儲是785.48KB;從上面的數據來看,壓縮傳輸在一定程度上給我們節省了頻寬;但是還有一個問題,不是所有的客戶端瀏覽器都支援壓縮功能,比如對於一些比較古老的瀏覽器,或者我們刻意讓某一種瀏覽器訪問伺服器資源時,不啟用壓縮功能;如下

   提示:以上配置表示如果匹配到用戶請求報文User-Agent中包含Firefox的字樣,僅對訪問文本格式和html格式的資源用gzip壓縮,匹配到Chrome字樣,不壓縮,意思就是火狐瀏覽器訪問伺服器上的.html的文件,僅gzip壓縮,其他類型的文件不啟用壓縮傳輸,Google瀏覽器不管訪問什麼類型的資源都不予壓縮;

 

   提示:可以看到Google瀏覽器訪問/mes和訪問/mes.html資源都沒有啟動壓縮功能,而火狐瀏覽器訪問/mes沒有啟動壓縮功能,而訪問/mes.html時就啟用了壓縮功能;這裡還需要注意一點,mime類型資源的區分是靠文件名後綴來區分的,不同的文件名後綴表示不同的mime類型資源;

  3、httpd啟用https對外提供訪問

  什麼是https?所謂https就是httpd+ssl,簡單講就是加密版的http,眾所周知http協議是明文傳輸,不加密,而https是加密傳輸的,相對於http協議,它更加安全,但同時它處理流程更多,響應相比http要慢一些;

  首先我們來說一下ssl會話的過程吧!

  (1) 客戶端發送可供選擇的加密方式,並向伺服器請求證書;

  (2) 伺服器端發送證書以及選定的加密方式給客戶端;

  (3) 客戶端取得證書並進行證書驗正,如果信任給其發證書的CA:

    (a) 驗正證書來源的合法性;用CA的公鑰解密證書上數字簽名;

    (b) 驗正證書的內容的合法性:完整性驗正;

    (c) 檢查證書的有效期限;

    (d) 檢查證書是否被吊銷;

    (e) 證書中擁有者的名字,與訪問的目標主機要一致;

  (4) 客戶端生成臨時會話密鑰(對稱密鑰),並使用伺服器端的公鑰加密此數據發送給伺服器,完成密鑰交換;

  (5) 伺服器用此密鑰加密用戶請求的資源,響應給客戶端;

  這裡需要注意一點,ssl會話是基於ip地址創建,所以單ip地址的主機上,僅可以使用一個https虛擬主機;

  示例:httpd實現https

  首先我們要去申請一證書,而申請證書又需要CA,所以我們不打算去網上買證書,就需要主機搭建CA伺服器,有關CA伺服器詳細搭建可以參考本人部落格https://www.cnblogs.com/qiuhom-1874/p/12237944.html;我這裡只是簡單說下過程,CA伺服器最主要的就兩步,第一步是生成私鑰,第二步就是生成自簽名證書;接下來我們先生成CA私鑰和自簽名證書;

[root@test_node1-centos7 CA]# pwd  /etc/pki/CA  [root@test_node1-centos7 CA]# ll  total 0  drwxr-xr-x. 2 root root 6 Aug  4  2017 certs  drwxr-xr-x. 2 root root 6 Aug  4  2017 crl  drwxr-xr-x. 2 root root 6 Aug  4  2017 newcerts  drwx------. 2 root root 6 Aug  4  2017 private  [root@test_node1-centos7 CA]# (umask 077;openssl genrsa -out cakey.pem 1024)  Generating RSA private key, 1024 bit long modulus  .......++++++  .......................++++++  e is 65537 (0x10001)  [root@test_node1-centos7 CA]# ll  total 4  -rw-------  1 root root 891 Mar 29 18:24 cakey.pem  drwxr-xr-x. 2 root root   6 Aug  4  2017 certs  drwxr-xr-x. 2 root root   6 Aug  4  2017 crl  drwxr-xr-x. 2 root root   6 Aug  4  2017 newcerts  drwx------. 2 root root   6 Aug  4  2017 private  [root@test_node1-centos7 CA]# openssl req -new -x509 -key cakey.pem  -out cacert.pem -days 365  You are about to be asked to enter information that will be incorporated  into your certificate request.  What you are about to enter is what is called a Distinguished Name or a DN.  There are quite a few fields but you can leave some blank  For some fields there will be a default value,  If you enter '.', the field will be left blank.  -----  Country Name (2 letter code) [XX]:CN  State or Province Name (full name) []:SC  Locality Name (eg, city) [Default City]:GY  Organization Name (eg, company) [Default Company Ltd]:TEST  Organizational Unit Name (eg, section) []:OPS  Common Name (eg, your name or your server's hostname) []:ca.test.com  Email Address []:  [root@test_node1-centos7 CA]# ll  total 8  -rw-r--r--  1 root root 932 Mar 29 18:25 cacert.pem  -rw-------  1 root root 891 Mar 29 18:24 cakey.pem  drwxr-xr-x. 2 root root   6 Aug  4  2017 certs  drwxr-xr-x. 2 root root   6 Aug  4  2017 crl  drwxr-xr-x. 2 root root   6 Aug  4  2017 newcerts  drwx------. 2 root root   6 Aug  4  2017 private  [root@test_node1-centos7 CA]# 

  提示:CA可以和httpd在同一伺服器

  生成httpd的應用程式私鑰和證書籤發文件

[root@test_node1-centos7 CA]# ll  total 8  -rw-r--r--  1 root root 932 Mar 29 18:25 cacert.pem  -rw-------  1 root root 891 Mar 29 18:24 cakey.pem  drwxr-xr-x. 2 root root   6 Aug  4  2017 certs  drwxr-xr-x. 2 root root   6 Aug  4  2017 crl  drwxr-xr-x. 2 root root   6 Aug  4  2017 newcerts  drwx------. 2 root root   6 Aug  4  2017 private  [root@test_node1-centos7 CA]# (umask 077;openssl genrsa -out httpd.pem 1024)  Generating RSA private key, 1024 bit long modulus  ..............++++++  ....++++++  e is 65537 (0x10001)  [root@test_node1-centos7 CA]# openssl req -new -key httpd.pem -out httpd.csr  You are about to be asked to enter information that will be incorporated  into your certificate request.  What you are about to enter is what is called a Distinguished Name or a DN.  There are quite a few fields but you can leave some blank  For some fields there will be a default value,  If you enter '.', the field will be left blank.  -----  Country Name (2 letter code) [XX]:CN  State or Province Name (full name) []:SC  Locality Name (eg, city) [Default City]:GY  Organization Name (eg, company) [Default Company Ltd]:TEST  Organizational Unit Name (eg, section) []:OPS  Common Name (eg, your name or your server's hostname) []:www.test.com  Email Address []:    Please enter the following 'extra' attributes  to be sent with your certificate request  A challenge password []:  An optional company name []:  [root@test_node1-centos7 CA]# ll  total 16  -rw-r--r--  1 root root 932 Mar 29 18:25 cacert.pem  -rw-------  1 root root 891 Mar 29 18:24 cakey.pem  drwxr-xr-x. 2 root root   6 Aug  4  2017 certs  drwxr-xr-x. 2 root root   6 Aug  4  2017 crl  -rw-r--r--  1 root root 635 Mar 29 18:30 httpd.csr  -rw-------  1 root root 887 Mar 29 18:26 httpd.pem  drwxr-xr-x. 2 root root   6 Aug  4  2017 newcerts  drwx------. 2 root root   6 Aug  4  2017 private  [root@test_node1-centos7 CA]#  

  簽發證書,生成httpd應用證書

[root@test_node1-centos7 CA]# touch index.txt  [root@test_node1-centos7 CA]# echo "01" >serial  [root@test_node1-centos7 CA]# openssl ca -in httpd.csr -out httpd.crt.pem -days 365  Using configuration from /etc/pki/tls/openssl.cnf  Check that the request matches the signature  Signature ok  Certificate Details:          Serial Number: 1 (0x1)          Validity              Not Before: Mar 29 10:36:47 2020 GMT              Not After : Mar 29 10:36:47 2021 GMT          Subject:              countryName               = CN              stateOrProvinceName       = SC              organizationName          = TEST              organizationalUnitName    = OPS              commonName                = www.test.com          X509v3 extensions:              X509v3 Basic Constraints:                  CA:FALSE              Netscape Comment:                  OpenSSL Generated Certificate              X509v3 Subject Key Identifier:                  38:DE:20:C0:0F:F7:CB:C2:2F:F4:1E:2D:FD:BB:A0:54:EA:DE:61:FE              X509v3 Authority Key Identifier:                  keyid:6C:33:A5:78:22:17:22:73:34:45:5B:95:AC:0D:B9:BD:33:B6:B3:1D    Certificate is to be certified until Mar 29 10:36:47 2021 GMT (365 days)  Sign the certificate? [y/n]:y      1 out of 1 certificate requests certified, commit? [y/n]y  Write out database with 1 new entries  Data Base Updated  [root@test_node1-centos7 CA]# ll  total 36  -rw-r--r--  1 root root  932 Mar 29 18:25 cacert.pem  -rw-------  1 root root  891 Mar 29 18:24 cakey.pem  drwxr-xr-x. 2 root root    6 Aug  4  2017 certs  drwxr-xr-x. 2 root root    6 Aug  4  2017 crl  -rw-r--r--  1 root root 3022 Mar 29 18:36 httpd.crt.pem  -rw-r--r--  1 root root  635 Mar 29 18:30 httpd.csr  -rw-------  1 root root  887 Mar 29 18:26 httpd.pem  -rw-r--r--  1 root root   70 Mar 29 18:36 index.txt  -rw-r--r--  1 root root   21 Mar 29 18:36 index.txt.attr  -rw-r--r--  1 root root    0 Mar 29 18:36 index.txt.old  drwxr-xr-x. 2 root root   20 Mar 29 18:36 newcerts  drwx------. 2 root root    6 Aug  4  2017 private  -rw-r--r--  1 root root    3 Mar 29 18:36 serial  -rw-r--r--  1 root root    3 Mar 29 18:36 serial.old  [root@test_node1-centos7 CA]# pwd  /etc/pki/CA  [root@test_node1-centos7 CA]#  

  證書生成好了,接下就是配置httpd支援https訪問即可,在這之前,我們需要在伺服器上確認httpd是否裝載了mod_ssl模組,默認httpd是沒有這個模組的,我們需要手動安裝才行

[root@test_node1-centos7 CA]# httpd -M |grep ssl  [root@test_node1-centos7 CA]# yum info mod_ssl  Loaded plugins: fastestmirror  Loading mirror speeds from cached hostfile   * base: mirrors.aliyun.com   * extras: mirrors.aliyun.com   * updates: mirrors.cn99.com  Available Packages  Name        : mod_ssl  Arch        : x86_64  Epoch       : 1  Version     : 2.4.6  Release     : 90.el7.centos  Size        : 112 k  Repo        : base/7/x86_64  Summary     : SSL/TLS module for the Apache HTTP Server  URL         : http://httpd.apache.org/  License     : ASL 2.0  Description : The mod_ssl module provides strong cryptography for the Apache Web              : server via the Secure Sockets Layer (SSL) and Transport Layer              : Security (TLS) protocols.    [root@test_node1-centos7 CA]# yum install -y mod_ssl  Loaded plugins: fastestmirror  base                                                                             | 3.6 kB  00:00:00  epel                                                                             | 4.7 kB  00:00:00  extras                                                                           | 2.9 kB  00:00:00  updates                                                                          | 2.9 kB  00:00:00  (1/5): epel/x86_64/group_gz                                                      |  95 kB  00:00:00  (2/5): extras/7/x86_64/primary_db                                                | 164 kB  00:00:00  (3/5): epel/x86_64/updateinfo                                                    | 1.0 MB  00:00:00  (4/5): updates/7/x86_64/primary_db                                               | 7.6 MB  00:00:01  (5/5): epel/x86_64/primary_db                                                    | 6.8 MB  00:00:01  Loading mirror speeds from cached hostfile   * base: mirrors.aliyun.com   * extras: mirrors.aliyun.com   * updates: mirrors.cn99.com  Resolving Dependencies  --> Running transaction check  ---> Package mod_ssl.x86_64 1:2.4.6-90.el7.centos will be installed  --> Finished Dependency Resolution    Dependencies Resolved    ========================================================================================================   Package              Arch                Version                               Repository         Size  ========================================================================================================  Installing:   mod_ssl              x86_64              1:2.4.6-90.el7.centos                 base              112 k    Transaction Summary  ========================================================================================================  Install  1 Package    Total download size: 112 k  Installed size: 224 k  Downloading packages:  mod_ssl-2.4.6-90.el7.centos.x86_64.rpm                                           | 112 kB  00:00:00  Running transaction check  Running transaction test  Transaction test succeeded  Running transaction    Installing : 1:mod_ssl-2.4.6-90.el7.centos.x86_64                                                 1/1    Verifying  : 1:mod_ssl-2.4.6-90.el7.centos.x86_64                                                 1/1    Installed:    mod_ssl.x86_64 1:2.4.6-90.el7.centos    Complete!  [root@test_node1-centos7 CA]# rpm -ql mod_ssl  /etc/httpd/conf.d/ssl.conf  /etc/httpd/conf.modules.d/00-ssl.conf  /usr/lib64/httpd/modules/mod_ssl.so  /usr/libexec/httpd-ssl-pass-dialog  /var/cache/httpd/ssl  [root@test_node1-centos7 CA]#  

  提示:可以看到安裝mod_ssl生成了一個ssl.conf的配置文件和00-ssl.conf、mod_ssl.so  ,安裝這個包後,httpd就支援https了,接下來配置www.test.com虛擬站點支援https訪問

   提示:我們可以自己新建一個配置文件,或者直接修改ssl.conf文件,把對應的內容修改修改也是可以的;

  測試,用瀏覽器訪問https://www.test.com,看看是否可以正常訪問

   提示:通過測試,我們可以正常訪問www.test.com虛擬主機提供的主頁,這裡需要注意,如果我們自己寫配置文件,且單獨一配置文件,如果自己寫的有listen 443 https,那麼ssl.conf里的就需要注釋,否則重載配置文件httpd服務起不來;