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

  前文咱們聊到了httpd的虛擬主機實現,狀態頁的實現,回顧請參考http://www.javashuo.com/article/p-aznndviz-hv.html;今天咱們來聊一聊後面的經常使用基礎配置;html

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

  示例:apache

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

   提示:最上面的那個httpd進程是httpd的主控進程,主要管控下面的子進程的,因此它的用戶必須是root;瀏覽器

  二、使用mod_deflate模塊壓縮頁面優化傳輸速度;安全

  在啓用壓縮功能的前提是咱們httpd服務器上必需要加載mod_deflate模塊,其次咱們還須要定義一個過濾器,而後明確的把那一類資源類型進行壓縮,一般狀況文件和文本心裏的資源壓縮比例最高,效果最好,圖片和一些二進制文件都不建議壓縮,圖片原本就是一個高度壓縮的,若是再壓縮可能存在圖片不可用的狀態;bash

  示例:服務器

  首先確認mod_deflate模塊是否啓用,若是沒有啓用使用loadmodule指令啓用ide

   提示:若是可以過濾出來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壓縮,其餘類型的文件不啓用壓縮傳輸,谷歌瀏覽器無論訪問什麼類型的資源都不予壓縮;

 

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

  三、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服務器詳細搭建能夠參考本人博客http://www.javashuo.com/article/p-eqvaoxip-bq.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服務起不來;

相關文章
相關標籤/搜索