1. 程式人生 > 實用技巧 >Linux第九周作業

Linux第九周作業

1、詳細描述一次加密通訊的過程,結合圖示最佳。

A和B之間要進行加密通訊,通訊的主體內容應選擇對稱加密演算法(效率高),而對稱祕鑰自身在網路上的傳輸則通過非對稱加密實現(安全性高)

以下為A與B之間通過加密通訊傳送郵件的場景。


wKioL1gIPOrDzsS6AAEltvyrIF0901.jpg

A向B發一份郵件,用單向加密演算法提取郵件內容的特徵碼,並用A的私鑰加密特徵碼並附加在郵件的最後,A再用對稱金鑰將整個郵件內容(包括特徵碼)加密,併發送給B。A用B的公鑰將對稱金鑰加密,最後將對稱金鑰發給B

B用自己的私鑰將對稱金鑰解密。用對稱祕鑰將郵件內容和加密的特徵碼解密。用A的公鑰解密(加密的特徵碼)。再用特徵碼提取郵件的內容。最後確認郵件的完整性!


2、描述建立私有CA的過程,以及為客戶端發來的證書請求進行辦法證書。

私有CA的意義:對於一般企業來說,申請在網際網路中被信任的CA證書過於昂貴。因此,如果只是用於企業內部伺服器的認證,僅僅需要通過openssl來搭建私有ca即可。

###############################################################################

私有CA相關檔案的作用

[[email protected]~]#less/etc/pki/tls/openssl.cnf
[CA_default]#預設CA配置用於在簽發證書時,未指定相關引數時候,呼叫預設引數!
dir=/etc/pki/CA#openssl使用yum安裝後,所有證書相關的內容都在該目錄下
certs=$dir/certs#發行的證書儲存位置
crl_dir=$dir/crl#撤銷的證書儲存位置
database=$dir/index.txt#證書索引資料庫
#unique_subject=no#允許不同的證書擁有相同的subject
new_certs_dir=$dir/newcerts#新發行證書的儲存位置
certificate=$dir/cacert.pem#CA證書
serial=$dir/serial#證書籤發序列
crlnumber=$dir/crlnumber#已經撤銷的證書
crl=$dir/crl.pem#當前的撤銷證書
private_key=$dir/private/cakey.pem#CA的私鑰
RANDFILE=$dir/private/.rand#私鑰的隨機數檔案
x509_extensions=usr_cert




CA伺服器端環境的部署

[[email protected]private]#rpm-qaopenssl*#檢查opennssl軟體包是否安裝
openssl-1.0.1e-42.el6_7.4.x86_64
openssl-devel-1.0.1e-42.el6_7.4.x86_64
[[email protected]~]#cd/etc/pki/CA/private/
[[email protected]private]#(umask077;opensslgenrsa-outcakey.pem2048)
#在子shell中建立CA的私鑰
GeneratingRSAprivatekey,2048bitlongmodulus
..................+++
..........................
[
[email protected]
private]#opensslreq-new-x509-keycakey.pem-outcacert.pem-days365 #生成自簽證書 ################################################################################## 此處如使用-keyoutmyCAkey.pem引數可以指定生成ca的私鑰,而省去前一步中通過genrsa生成私鑰的步驟! 此處如使用-newkeyrsa:2048代替-new選項,此引數可以指定生成的CA私鑰的長度,同時生成一個自簽證書的請求 Youareabouttobeaskedtoenterinformationthatwillbeincorporated intoyourcertificaterequest. WhatyouareabouttoenteriswhatiscalledaDistinguishedNameoraDN. Therearequiteafewfieldsbutyoucanleavesomeblank Forsomefieldstherewillbeadefaultvalue, Ifyouenter'.',thefieldwillbeleftblank. ----- CountryName(2lettercode)[XX]:CN StateorProvinceName(fullname)[]:Shanghai LocalityName(eg,city)[DefaultCity]:Shanghai OrganizationName(eg,company)[DefaultCompanyLtd]:sbc OrganizationalUnitName(eg,section)[]:IT CommonName(eg,yournameoryourserver'shostname)[]:Caserver#此必須要填,不可省略!且必須要和CA主機名相同! EmailAddress[]:[email protected] [[email protected]CA]#touchindex.txt#建立CA縮影資料庫 [[email protected]CA]#touchserial#建立簽署證書編號檔案 [[email protected]CA]#echo01>serial#設定編號檔案的初始值 [[email protected]CA]#catserial 01


客戶端環境的部署:

[[email protected]~]#mkdir/etc/httpd/ssl
[[email protected]~]#cd/etc/httpd/ssl/
[[email protected]ssl]#rpm-qaopenssl*#檢查opennssl軟體包是否安裝
openssl-1.0.1e-42.el6_7.4.x86_64
openssl-devel-1.0.1e-42.el6_7.4.x86_64
[[email protected]ssl]#(umask077;opensslgenrsa-outhttpCert.pem)#在子shell中建立主機的私鑰
[[email protected]ssl]#opensslreq-new-keyhttpCert.pem-outhttpCert.csr-days365
#建立證書申請檔案
Youareabouttobeaskedtoenterinformationthatwillbeincorporated
intoyourcertificaterequest.
WhatyouareabouttoenteriswhatiscalledaDistinguishedNameoraDN.
Therearequiteafewfieldsbutyoucanleavesomeblank
Forsomefieldstherewillbeadefaultvalue,
Ifyouenter'.',thefieldwillbeleftblank.
-----
CountryName(2lettercode)[XX]:CN
StateorProvinceName(fullname)[]:shanghai
LocalityName(eg,city)[DefaultCity]:shanghai
OrganizationName(eg,company)[DefaultCompanyLtd]:nichiyu
OrganizationalUnitName(eg,section)[]:Manage
CommonName(eg,yournameoryourserver'shostname)[]:www.nichiyu.net
EmailAddress[]:[email protected]
Pleaseenterthefollowing'extra'attributes
tobesentwithyourcertificaterequest
Achallengepassword[]:
Anoptionalcompanyname[]:
[[email protected]~]#scp/etc/httpd/ssl/httpCert.csr192.168.230.116:/etc/pki/CA/tmp
#將證書申請傳送給CA
[email protected]'spassword:
httpCert.csr100%7080.7KB/s00:00

CA伺服器端簽署客戶端的證書申請

[[email protected]CA]#opensslca-in/etc/pki/CA/tmp/httpCert.csr-out/etc/pki/CA/newcerts/httpCert.crt-days1000#簽發收到的證書申請
#################################################################################

其餘未使用的重要引數:
-cert#手動指定CA的證書
-keyfile#手動指定CA的私鑰

Usingconfigurationfrom/etc/pki/tls/openssl.cnf
unabletoloadnumberfrom/etc/pki/CA/serial
errorwhileloadingserialnumber
140134958831432:error:0D066096:asn1encodingroutines:a2i_ASN1_INTEGER:shortline:f_int.c:215:
[[email protected]CA]#echo01>serial
[[email protected]CA]#opensslca-in/etc/pki/CA/tmp/httpCert.csr-out/etc/pki/CA/newcerts/httpCert.crt-days1000
Usingconfigurationfrom/etc/pki/tls/openssl.cnf
Checkthattherequestmatchesthesignature
Signatureok
CertificateDetails:
SerialNumber:1(0x1)
Validity
NotBefore:Oct2503:05:512016GMT
NotAfter:Jul2203:05:512019GMT
Subject:
countryName=CN
stateOrProvinceName=shanghai
organizationName=nichiyu
organizationalUnitName=Manage
commonName=www.nichiyu.net
emailAddress=[email protected]
X509v3extensions:
X509v3BasicConstraints:
CA:FALSE
NetscapeComment:
OpenSSLGeneratedCertificate
X509v3SubjectKeyIdentifier:
9F:3A:2B:E5:FC:B2:0D:D8:D9:E3:71:FD:AA:49:07:D2:20:00:B1:A4
X509v3AuthorityKeyIdentifier:
keyid:7F:49:A9:12:74:FE:0F:32:B8:57:C8:CF:6C:AE:2D:5B:3B:24:FC:A4
CertificateistobecertifieduntilJul2203:05:512019GMT(1000days)
Signthecertificate?[y/n]:y
1outof1certificaterequestscertified,commit?[y/n]y
Writeoutdatabasewith1newentries
DataBaseUpdated


CA將簽署的證書返還給申請者

[[email protected]CA]#scpnewcerts/httpCert.crt192.168.230.120:/etc/httpd/ssl/
Theauthenticityofhost'192.168.230.120(192.168.230.120)'can'tbeestablished.
RSAkeyfingerprintis84:60:12:a0:ab:1e:f5:ca:b6:6c:7f:70:8c:bf:f7:05.
Areyousureyouwanttocontinueconnecting(yes/no)?yes
Warning:Permanentlyadded'192.168.230.120'(RSA)tothelistofknownhosts.
[email protected]'spassword:
httpCert.crt100%37613.7KB/s00:00

3、搭建一套DNS伺服器,負責解析magedu.com域名(自行設定主機名及IP)

(1)、能夠對一些主機名進行正向解析和逆向解析;

(2)、對子域cdn.magedu.com進行子域授權,子域負責解析對應子域中的主機名;

(3)、為了保證DNS服務系統的高可用性,請設計一套方案,並寫出詳細的實施過程實

實驗環境:DNS:192.168.230.121 DNS(slave):192.168.230.122

webserver:192.168.230.120 CAserver:192.168.230.116 subDNS:192.168.230.123


DNS服務通過bind9實現,通過yum安裝後,主要的檔案分佈如下:


服務指令碼: /etc/rc.d/init.d/named

主要配置檔案: /etc/named.rfc1912.zones";

/etc/rndc.key

解析庫檔案: /var/named/ZONE_NAME.ZONE


############################################################################


DNS 主伺服器部署

[[email protected]~]#rpm-qabind*#檢查本地是否安裝bind9相關元件
bind-chroot-9.8.2-0.47.rc1.el6_8.2.x86_64
bind-libs-9.8.2-0.47.rc1.el6_8.2.x86_64
bind-9.8.2-0.47.rc1.el6_8.2.x86_64
bind-sdb-9.8.2-0.47.rc1.el6_8.2.x86_64
bind-dyndb-ldap-2.3-8.el6.x86_64
bind-devel-9.8.2-0.47.rc1.el6_8.2.x86_64
bind-utils-9.8.2-0.47.rc1.el6_8.2.x86_64
[[email protected]~]#vim/etc/named.conf#修改主配置檔案
options{
listen-onport53{127.0.0.1;192.168.230.121;};#新增監聽IP
listen-on-v6port53{::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";
allow-query{any;};#任何主機都可以查詢解析
recursionyes;
dnssec-enableyes;
dnssec-validationyes;
/*PathtoISCDLVkey*/
bindkeys-file"/etc/named.iscdlv.key";
managed-keys-directory"/var/named/dynamic";
};
[[email protected]~]#vim/etc/named.rfc1912.zones#在區域配置檔案中新增新區域
......................
zone"magedu.com"IN{
typemaster;
file"magedu.com.ZONE"
};#正向解析區域
zone"230.168.192.in-addr.arpa"IN{#反向解析區域
typemaster;
file"192.168.230.ZONE";
};


[[email protected] ~]# vim /var/named/magedu.com.ZONE

$ORIGINmagedu.com.#結尾的.非常重要,不能漏掉,代表頂級域!
$TTL100
@INSOAns1.magedu.com.wuuzhenliang.magedu.com.(
2016102501
#代表這個Zone的序列號。供SlaveDNS判斷是否從MasterDNS獲取新資料。每次Zone檔案更新,都需要修改Serial數值。20161025為當前日期,01代表修訂號
1H#代表slaveDNS伺服器多久向主伺服器發起一起重新整理請求,在bind的notify功能開啟時候無意義
15#代表slaveDNS在一起請求同步失敗以後重試的時間
10H#代表slaveDNS在多久未獲得祝福器響應後不再發起重試請求
1H#代表slaveDNS否定回答的持續時間
)
INNSns1
INNSns2
INMX10mail
mailINA192.168.230.130
ns1INA192.168.230.121
ns2INA192.168.230.122
wwwINA192.168.230.120
webINCNAMEwww
opsINNSns1.ops#定義子域ops.magedu.com
ns1.opsINA192.168.230.123
[[email protected]~]#vim/var/named/192.168.230.ZONE
$ORIGIN230.168.192.in-addr.arpa.
$TTL100
@INSOAns1.magedu.com.wuzhenliang.magedu.com.(
2016102501#
1H
15
10H
1H
)
INNSns1.magedu.com.#此處的域名必須要填全
120INPTRwww.magedu.com.
[[email protected]~]#named-checkconf#檢查配置是否正確
[[email protected]~]#named-checkzone"magedu.com"/var/named/magedu.com.ZONE
zonemagedu.com/IN:loadedserial2016102501
OK
[[email protected]named]#rndcreload
serverreloadsuccessful
[[email protected]~]#[email protected]#測試正向解析是否正常
;<<>>DiG9.8.2rc1-RedHat-9.8.2-0.47.rc1.el6_8.2<<>>[email protected]
;;globaloptions:+cmd
;;Gotanswer:
;;->>HEADER<<-opcode:QUERY,status:NOERROR,id:37984
;;flags:qraardra;QUERY:1,ANSWER:1,AUTHORITY:1,ADDITIONAL:1
;;QUESTIONSECTION:
;www.magedu.com.INA
;;ANSWERSECTION:
www.magedu.com.100INA192.168.230.120
;;AUTHORITYSECTION:
magedu.com.100INNSns1.magedu.com.
;;ADDITIONALSECTION:
ns1.magedu.com.100INA192.168.230.121
;;Querytime:11msec
;;SERVER:192.168.230.121#53(192.168.230.121)
;;WHEN:TueOct2515:05:042016
;;MSGSIZErcvd:82
[[email protected]~]#[email protected]#測試反向解析是否正常
;<<>>DiG9.8.2rc1-RedHat-9.8.2-0.17.rc1.el6_4.6<<>>[email protected]
;;globaloptions:+cmd
;;Gotanswer:
;;->>HEADER<<-opcode:QUERY,status:NOERROR,id:50351
;;flags:qraardra;QUERY:1,ANSWER:1,AUTHORITY:1,ADDITIONAL:1
;;QUESTIONSECTION:
;120.230.168.192.in-addr.arpa.INPTR
;;ANSWERSECTION:
120.230.168.192.in-addr.arpa.100INPTRwww.magedu.com.
;;AUTHORITYSECTION:
230.168.192.in-addr.arpa.100INNSns1.magedu.com.
;;ADDITIONALSECTION:
ns1.magedu.com.100INA192.168.230.121
;;Querytime:43msec
;;SERVER:192.168.230.121#53(192.168.230.121)
;;WHEN:WedOct2609:01:422016
;;MSGSIZErcvd:108


DNS從伺服器部署:

1.主配置檔案
options{
listen-onport53{any;};
listen-on-v6port53{::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";
allow-query{any;};
recursionyes;
dnssec-enableyes;
dnssec-validationyes;
/*PathtoISCDLVkey*/
bindkeys-file"/etc/named.iscdlv.key";
managed-keys-directory"/var/named/dynamic";
};
......

2.區域配置檔案

[[email protected]~]#vim/etc/named.rfc1912.zones
.......
zone"magedu.com"IN{
typeslave;
masters{192.168.230.121;};
file"slaves/magedu.com.ZONE";#解析庫檔名必須與主nds上的解析庫檔名相同!
};


在設定的主從dns同步時間達到後,檢視同步情況

[[email protected]~]#ll/var/named/slaves/
total8
-rw-r--r--.1namednamed388Oct2613:21192.168.230.ZONE
-rw-r--r--.1namednamed474Oct2613:21magedu.com.ZONE


DNS子伺服器部署:

[[email protected]~]#vim/var/named/ops.magedu.com.ZONE
$ORIGINops.magedu.com.
$TTL100
@INSOAns1.ops.magedu.com.wuzhenliang.ops.magedu.com.(
2016102501
10M
1H
10H
1H
)
INNSns1
INNSns2
ns1INA192.168.230.123
ns2INA192.168.230.124
wwwINA192.168.230.125
webINCNAMEwww
[[email protected]~]#[email protected]#在CA伺服器上測試解析webServer的IP地址
;<<>>DiG9.8.2rc1-RedHat-9.8.2-0.17.rc1.el6_4.6<<>>[email protected]
;;globaloptions:+cmd
;;Gotanswer:
;;->>HEADER<<-opcode:QUERY,status:NOERROR,id:40889
;;flags:qraardra;QUERY:1,ANSWER:1,AUTHORITY:2,ADDITIONAL:2
;;QUESTIONSECTION:
;www.ops.magedu.com.INA
;;ANSWERSECTION:
www.ops.magedu.com.100INA192.168.230.125
;;AUTHORITYSECTION:
ops.magedu.com.100INNSns1.ops.magedu.com.
ops.magedu.com.100INNSns2.ops.magedu.com.
;;ADDITIONALSECTION:
ns1.ops.magedu.com.100INA192.168.230.123
ns2.ops.magedu.com.100INA192.168.230.124
;;Querytime:0msec
;;SERVER:192.168.230.123#53(192.168.230.123)
;;WHEN:WedOct2614:18:452016
;;MSGSIZErcvd:120


DNS的高可用方案

  1. 對於僅用於解析企業內部資源的DNS來說,一般的主從DNS架構就能滿足需求

  2. 對於解析的資源位於不用的物理位置,特別是兩處的網路接入資源由不同的ISP來提供時,則可通過bind9的view功能來實現DNS的高可用,方案如下


部署環境拓撲

wKioL1gSzDzgBZMFAAFwcUt3JF8080.jpg

[[email protected]named]#vim/etc/named.conf
options{
listen-onport53{any;};
listen-on-v6port53{::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";
allow-query{any;};
recursionyes;
dnssec-enableno;
dnssec-validationno;
/*PathtoISCDLVkey*/
bindkeys-file"/etc/named.iscdlv.key";
managed-keys-directory"/var/named/dynamic";
};
aclinet{#定義3個acl用於view控制範圍
192.168.232.0/24;
};
aclnet229{
192.168.230.122;#為測試的方便。。此處仍定義為230網段的地址
aclnet230{
192.168.230.116;#定義為另一個230網段的地址
};
view"."{
match-clients{inet;};#只有來源是192.168.232網段的請求才會去根域查詢
zone"."IN{
typehint;
file"named.ca";
};
};
include"/etc/named.rfc1912.zones";
include"/etc/named.root.key";


#####################################################################################

[[email protected]named]#vim/etc/named.rfc1912.zones
zone"0.in-addr.arpa"IN{
typemaster;
file"named.empty";
allow-update{none;};
};
};
view"net230"{
match-clients{net230;};
zone"magedu.com"IN{
typemaster;
file"magedu.com.ZONE";
};
zone"230.168.192.in-addr.arpa"IN{
typemaster;
file"192.168.230.ZONE";
allow-transfer{192.168.230.122;};
};
};
view"net229"{#定義區域magedu.com的另一個解析檔案
match-clients{net229;};
zone"magedu.com"{
typemaster;
file"magedu.com.other.ZONE";
};
};

######################################################################################

[[email protected]named]#vim/var/named/magedu.com.other.ZONE$ORIGINmagedu.com.
$TTL100
@INSOAns1.magedu.com.wuzhenliang.magedu.com.(
2016102507
10M
1H
10H
1H
)
INNSns1
INNSns2
INMX10mail
mailINA192.168.229.130#解析到229網段的地址
ns1INA192.168.230.121
ns2INA192.168.230.122
wwwINA192.168.229.120#解析到229網段的地址
webINCNAMEwww
opsINNSns1.ops
ns1.opsINA192.168.230.123

########################################################################################

在不同終端上測試解析結果

[[email protected]~]#[email protected]
;<<>>DiG9.8.2rc1-RedHat-9.8.2-0.17.rc1.el6_4.6<<>>[email protected]
;;globaloptions:+cmd
;;Gotanswer:
;;->>HEADER<<-opcode:QUERY,status:NOERROR,id:21852
;;flags:qraardra;QUERY:1,ANSWER:1,AUTHORITY:2,ADDITIONAL:2
;;QUESTIONSECTION:
;www.magedu.com.INA
;;ANSWERSECTION:
www.magedu.com.600INA192.168.230.120#得到230網段的地址
;;AUTHORITYSECTION:
magedu.com.600INNSns1.magedu.com.
magedu.com.600INNSns2.magedu.com.
;;ADDITIONALSECTION:
ns1.magedu.com.600INA192.168.230.121
ns2.magedu.com.600INA192.168.230.122
;;Querytime:24msec
;;SERVER:192.168.230.121#53(192.168.230.121)
;;WHEN:FriOct2813:45:592016
;;MSGSIZErcvd:116
[[email protected]~]#[email protected]
;<<>>DiG9.8.2rc1-RedHat-9.8.2-0.47.rc1.el6_8.2<<>>[email protected]
;;globaloptions:+cmd
;;Gotanswer:
;;->>HEADER<<-opcode:QUERY,status:NOERROR,id:10152
;;flags:qraardra;QUERY:1,ANSWER:1,AUTHORITY:2,ADDITIONAL:2
;;QUESTIONSECTION:
;www.magedu.com.INA
;;ANSWERSECTION:
www.magedu.com.100INA192.168.229.120#得到229網段的解析地址
;;AUTHORITYSECTION:
magedu.com.100INNSns1.magedu.com.
magedu.com.100INNSns2.magedu.com.
;;ADDITIONALSECTION:
ns1.magedu.com.100INA192.168.230.121
ns2.magedu.com.100INA192.168.230.122
;;Querytime:1msec
;;SERVER:192.168.230.121#53(192.168.230.121)
;;WHEN:FriOct2813:46:532016
;;MSGSIZErcvd:116


4、請描述一次完整的http請求處理過程

域名一個完整的httpd請求和處理工程大致包括以下的幾步,

  1. 客戶端通過DNS域名解析,得到請求域名對應的web伺服器端IP地址 TCP的3次握手 --> 建立TCP連線後發起http請求 --> 伺服器響應http請求,瀏覽器得到html程式碼 --> 瀏覽器解析html程式碼,並請求html程式碼中的資源(如js、css、圖片等) --> 瀏覽器對頁面進行渲染呈現給使用者

  2. 客戶端和伺服器端建立TCP連結

    TCP連結的建立需要通過客戶端與伺服器端的三次握手來實現。首先客戶端傳送連線請求報文(SYN),伺服器端接受連線後回覆確認報文(ACK),併為這次連線分配資源。客戶端接收到ACK報文後也向伺服器端傳送確認報文(ACK),並分配資源,這樣TCP連線就建立了。

3. TCP連結建議以後,客戶端開始向伺服器端傳送http請求報文。請求報文報文的格式如下

<method> <request-URL> <version>

<headers>

<entity-body>

請求報文包括請求的方法,請求的url,還是http的版本號,一般來說請求報文不包括實體內容。


4. Web伺服器應答
客戶機向伺服器發出請求後,伺服器會響應客戶端的請求,傳送響應報文(reponse)給客戶端,

reponse報文的格式

<version> <status> <reason-phrase>

<headeres>

<entity-body>


status(狀態碼)

1XX:100-101 資訊提示

2XX:200-206 成功

3XX:300-305 重定向

4XX:400-415 錯誤類資訊,客戶端錯誤

5XX:500-505 錯誤型別。伺服器錯誤

常用的狀態碼

200:成功

301(永久重定向):請求的資源已經永久被刪除,但在響應報文中通過location指明資源現在所處的位置

302(臨時重定向):請求的資源已經永久被刪除,但在響應報文中通過location指明資源現在所處的臨時位置

303:客戶端發起條件式請求(請求的資源已在本地快取,請求伺服器端確認有無變化—),伺服器發現客戶端請求的內容自從上次以後未發生變化。傳送303給客戶端!

401: 需要輸入帳號和密碼認證訪問資源:Unauthorize

403: 請求被禁止(無許可權訪問)

404: 伺服器無法找到客戶端請求的資源:not found

500: 伺服器內部錯誤 Internal Server Error

502: 代理伺服器從後端伺服器收到了一條偽響應:bad gateway


5. 一般情況下,當客戶端需要請求的資源全部結束後,就要申請關閉TCP連線,然而如果瀏覽器或者伺服器在其頭資訊包含Connection:keep-alive,則TCP連線在傳送後將仍然保持開啟狀態,於是,瀏覽器可以繼續通過相同的連線傳送請求。保持連線節省了為每個請求建立新連線所需的時間,還節約了網路頻寬。

TCP連結的中斷是通過客戶端與伺服器端4次互動來實現。過程大致為Client端發起中斷連線請求(FIN報文)。伺服器端接到FIN報文後,回覆ACK,確認收到中斷請求。這時客戶端就進入FIN_WAIT狀態,繼續等待伺服器端的FIN報文。當伺服器端確定資料已傳送完成,則向客戶端端傳送FIN報文,客戶端收到FIN報文後,繼續等待了一段時間依然沒有收到回覆,則證明伺服器端已正常關閉,於是TCP中斷連線。



5、httpd所支援的處理模型有哪些,他們的分別使用於哪些環境。

httpd支援的處理模型包括prefork,workor,event,他們都屬於多路處理模組(MPM)其中event只有在httpd2.4才是穩定的版本



1)prefork採用預派生子程序方式,用單獨的子程序來處理不同的請求,程序之間彼此獨立。即一個程序響應一個httpd請求!

 Prefork適合於沒有執行緒安全庫,需要避免執行緒相容性問題的系統。它是要求將每個請求相互獨立的情況下最好的MPM,這樣若一個請求出現問題就不會影響到其他請求。這個MPM具有很強的自我調節能力,只需要很少的配置指令調整。最重要的是將MaxClients設定為一個足夠大的數值以處理潛在的請求高峰,同時又不能太大,以致需要使用的記憶體超出實體記憶體的大小。



httpd.conf中prefork的配置資訊:

<IfModule prefork.c>

StartServers 8 #服務啟動後httpd父程序建立的子程序數,只有子程序才響應http請求。

MinSpareServers 5 #最小的空閒程序數

MaxSpareServers 20 #最大的空閒程序數

ServerLimit 256 #在伺服器的生命週期內MaxClient的最大值,此處必須設定>=MaxClients

MaxClients 256 #可以同時相應的最大http請求數

MaxRequestsPerChild 4000 #每個程序可以處理的最大http請求數.此處如果設定為0,則代表子程序永不銷燬

</IfModule>


2.Worker
相對於prefork,worker是支援執行緒程序混合模型,使用執行緒來處理http請求,一個執行緒響應一個http請求。系統資源的開銷要小於基於程序的伺服器。

由於使用執行緒來處理請求,所以可以處理海量請求,而系統資源的開銷小於基於程序的MPM。但是,它也使用了多程序,每個程序又有多個執行緒,以獲得基於程序的MPM的穩定性。

httpd.conf中worker的配置資訊:

<IfModule worker.c>

StartServers 4 #服務啟動後httpd父程序建立的子程序數,

MaxClients 300 #可以同時相應的最大http請求數

MinSpareThreads 25 #最小的空閒執行緒

MaxSpareThreads 75 #最大的空閒執行緒

ThreadsPerChild 25 #每個程序包含的worker執行緒數

MaxRequestsPerChild 0 #每個程序可以處理的最大請求數

</IfModule>



3.event

是基於訊號驅動I/O 通知機制的模型。工作方式是:一個執行緒響應多個http請求。event的工作模型要優於prefork和worker.

EventMPM可以用來處理更高負載,它把服務程序從連線中分離出來。在伺服器處理速度很快,同時具有非常高的點選率時,可用的執行緒數量就是關鍵的資源限制,此時EventMPM方式是最有效的。但是EventMPM不能使用https。

httpd.conf中event的配置資訊:

<IfModulempm_event_module>

StartServers2 #服務啟動的時候建立的子程序數,該子程序共生成25個執行緒等待客戶端的連線請求

MinSpareThreads25 #執行緒池中保持最少25個空閒執行緒

MaxSpareThreads75 #執行緒池中保持最多75個空閒執行緒

ThreadsPerChild25 #每個程序最多可以生成25個執行緒

MaxRequestWorkers150 #可同時處理的最大的http請求數

MaxConnectionsPerChild0 #每個執行緒能夠處理的使用者請求數,0表示不做限定

</IfModule>


6、建立httpd伺服器(基於編譯的方式進行),要求:

提供兩個基於名稱的虛擬主機:

(a)www1.stuX.com,頁面檔案目錄為/web/vhosts/www1;錯誤日誌為/var/log/httpd/www1.err,訪問日誌為/var/log/httpd/www1.access;

(b)www2.stuX.com,頁面檔案目錄為/web/vhosts/www2;錯誤日誌為/var/log/httpd/www2.err,訪問日誌為/var/log/httpd/www2.access;

(c)為兩個虛擬主機建立各自的主頁檔案index.html,內容分別為其對應的主機名;

(d)通過www1.stuX.com/server-status輸出httpd工作狀態相關資訊,且只允許提供帳號密碼才能訪問(status:status);

#########################################################################################

一.檢查apache2.4安裝環境環境

apache2.4需要apr庫的支援,且版本需在1.4以上,centOS6自帶的apr只有1.39,因此在安裝apache-2.4之前,需要先安裝新版本的apr以及apr-util,此外還需要prc-deve庫的支援。

解決依賴關係

[[email protected]apr-1.5.2]#yumgrouplist|grep"Development"#檢查編譯所依賴的軟體包,如果未安裝,則yumgroupinstall“包名”安裝
Developmenttools
AdditionalDevelopment
DesktopPlatformDevelopment
ServerPlatformDevelopment


二.編譯安裝httpd2.4所依賴的軟體包


httpd2.4依賴於更高版本的apr和apr-util,apr全稱為apache portable runtime(可認為是apache的虛擬機器)。它是個能夠讓Apache跨平臺工作的工具,是個底層庫,安裝上apr後。如果在win和linux上安裝上apr後,Apache的同一個程式碼,即可以在win上執行也可在linux上執行;

  1. 安裝apr-1.5.2.tar.gz

[[email protected]~]#wgethttp://apache.fayea.com/apr/apr-1.5.2.tar.gz
[[email protected]~]#tarxfapr-1.5.2.tar.gz
[[email protected]~]#cdapr-1.5.2
[[email protected]apr-1.5.2]#./configure--prefix=/usr/local/apr
[[email protected]apr-1.5.2]#make&&makeinstall

2.安裝apr-util-1.5.4

[[email protected]~]#wgethttp://apache.fayea.com/apr/apr-util-1.5.4.tar.gz
[[email protected]~]#tarxfapr-util-1.5.4.tar.gz
[[email protected]~]#cdapr-util-1.5.4
[[email protected]apr-util-1.5.4]#./configure--prefix=/usr/local/apr-util--with-apr=/usr/local/apr
[[email protected]apr-util-1.5.4]#make&&makeinstall

三.編譯httpd-2.4.23.tar.gz

[[email protected]~]#wgethttp://apache.fayea.com/httpd/httpd-2.4.23.tar.gz
[[email protected]~]#tarxfhttpd-2.4.23.tar.gz
[[email protected]~]#cdhttpd-2.4.23
[[email protected]httpd-2.4.23]#./configure--prefix=/usr/local/apache--sysconfdir=/etc/httpd24--enable-so--enable-ssl--enable-cgi--enable-rewrite--with-zlib--with-pcre--with-apr=/usr/local/apr--with-apr-util=/usr/local/apr-util/--enable-modules=most--enable-mpms-shared=all--with-mpm=prefork
--prefix=/usr/local/apache安裝路徑
--sysconfdir=/etc/httpd24配置檔案路徑
--enable-so允許執行時載入DSO模組
--enable-ssl如果不載入將無法使用使用https
--enable-cgi允許使用cgi指令碼
--enable-rewrite支援URL重寫機制
--with-zlib支援網路通用壓縮庫
--with-pcre支援pcre
--with-apr=/usr/local/apr指定apr的安裝路徑
--with-apr-util=/usr/local/apr-util/指定apr-util的安裝路徑
--enable-modules=most啟用大多數常用的模組
--enable-mpms-shared=all啟用MPM所有支援的模式
--with-mpm=prefork預設使用prefork模式
[[email protected]httpd-2.4.23]#make&&makeinstall

四、安裝後所需配置;

1、匯出標頭檔案,以軟連線的方法實現

[[email protected]~]#ln-sv/usr/local/apache/include//usr/include/httpd
`/usr/include/httpd'->`/usr/local/apache/include/'



2、匯出二進位制程式

[[email protected]~]#vim/etc/profile.d/httpd24.sh
exportPATH=/usr/local/apache/bin:$PATH
[[email protected]~]./etc/profile.d/httpd24.sh


3、匯出man檔案

[[email protected]~]#vim/etc/man.config
MANPATH/usr/local/apache/man
[[email protected]~]#man-M/usr/local/apache/manhttpd#設定httpd的man手冊搜尋路徑


4、啟動httpd

[[email protected]~]#apachectlstart
[[email protected]~]#ss-tlun|grep80#測試80埠是否開啟監聽
tcpLISTEN0128:::80:::*

5、配置虛擬主機

[[email protected]~]#vim/etc/httpd24/httpd.conf
........
#DocumentRoot"/usr/local/apache/htdocs"#註釋掉中心主機
#Virtualhosts
Include/etc/httpd24/extra/httpd-vhosts.conf#在主配置檔案中匯入虛擬主機配置檔案!
[[email protected]~]#vim/etc/httpd24/extra/httpd-vhosts.conf
<Directory"/web/vhosts/">
OptionsIndexesFollowSymLinks#啟用索引,符號連結跟蹤
Requireallgranted#對所有的使用者授權可以訪問!httpd2.4版本必備
</Directory>
<VirtualHost192.168.230.120:80>
ServerAdmin[email protected]
DocumentRoot"/web/vhosts/www1"
ServerNamewww1.stuX.com
ErrorLog"/var/log/httpd24/www1.err"
CustomLog"/var/log/httpd24/www1.access"common
<Location/server-status>
SetHandlerserver-status
AuthTypeBasic#使用者認證的型別
AuthName"status"#安全域的名稱
AuthUserFile"/usr/local/apache/.htpasswd"#認證使用者密碼存放位置
Requirevalid-user#允許合法的使用者訪問
</Location>
</VirtualHost>
<VirtualHost192.168.230.120:80>
ServerAdmin[email protected]
DocumentRoot"/web/vhosts/www2"
ServerNamewww2.stuX.com
ErrorLog"/var/log/httpd24/www2.err"
CustomLog"/var/log/httpd24/www2.access"common
</VirtualHost>
[[email protected]~]#htpasswd-m-c/usr/local/apache/.htpasswdwuzhenliang#新增認證的使用者資訊,-c自動建立passwordfile,因此僅在建立第一個使用者時候使用。
[[email protected]~]#vim/etc/hosts#新增兩臺虛擬主機的域名對映#
127.0.0.1localhostlocalhost.localdomainlocalhost4localhost4.localdomain4
::1localhostlocalhost.localdomainlocalhost6localhost6.localdomain6
192.168.230.120www1.stuX.comwww2.stuX.com
[[email protected]~]#curlwww1.stuX.com#測試虛擬主機
websitewww1isworking!
[[email protected]~]#curlwww2.stuX.com
websitewww2isworking!


7、為第6題中的第2個虛擬主機提供https服務,使得使用者可以通過https安全的訪問此web站點;

(1)要求使用證書認證,證書中要求使用的國家(CN)、州(HA)、城市(ZZ)和組織(MageEdu);

(2)設定部門為Ops,主機名為www2.stuX.com,郵件為[email protected]


一.CA伺服器生成自簽證書,並簽署http伺服器發來的證書籤署請求,最後發還給http伺服器,存放在/etc/httpd24/extra/ssl/httpd.crt

過程同題二,此處省略。http伺服器發給CA簽署的證書的內容中,國家,州,城市,組織必須相同,common name 中除了主機名以外的內容必須相同,不然CA伺服器無法簽署證書!


二.http伺服器配置ssl相關內容,測試環境基於apache2.4

[[email protected]~]#vim/etc/httpd24/httpd.conf
LoadModulesocache_shmcb_modulemodules/mod_socache_shmcb.so#這兩項模組必須載入
LoadModulessl_modulemodules/mod_ssl.so#這兩項模組必須載入
Include/etc/httpd24/extra/httpd-ssl.conf#將ssl的配置檔案載入主配置檔案中
[[email protected]~]#vim/etc/httpd24/extra/httpd-ssl.conf
SSLCertificateFile"/etc/httpd24/extra/ssl/httpkey.crt"#設定http伺服器端的證書位置
SSLCertificateKeyFile"/etc/httpd24/extra/ssl/httpkey.pem"#設定http伺服器端的證書私鑰
[[email protected]~]#apachectlrestart
[[email protected]~]#ss-tuln#檢測埠狀態
NetidStateRecv-QSend-QLocalAddress
tcpLISTEN0128:::80:::*
......
tcpLISTEN0128:::443:::*


[[email protected] ~]# sz /etc/pki/CA/cacert.pem #將CA證書傳給測試的客戶端

最後在windows客戶端通過瀏覽器https訪問http伺服器。

wKioL1gcOC6gHIBaAAB46BcXsMQ934.png

遇到的錯誤:

AH00526: Syntax error on line 92 of /etc/httpd24/extra/httpd-ssl.conf:

SSLSessionCache: 'shmcb' session cache not supported (known names: ). Maybe you need to load the appropriate socache module (mod_socache_shmcb?).

解決的方法:在httpd.conf載入shmcb模組


8、建立samba共享,共享目錄為/data,要求:(描述完整的過程)

1)共享名為shared,工作組為magedu;

2)新增組develop,新增使用者gentoo,centos和ubuntu,其中gentoo和centos以develop為附加組,ubuntu不屬於develop組;密碼均為使用者名稱;

3)新增samba使用者gentoo,centos和ubuntu,密碼均為“mageedu”;

4)此samba共享shared僅允許develop組具有寫許可權,其他使用者只能以只讀方式訪問;

5)此samba共享服務僅允許來自於172.16.0.0/16網路的主機訪問;


[[email protected]~]#rpm-qasamba#檢查是否安裝samba相關元件
[[email protected]~]#yuminstallsamba-y
[[email protected]~]#foruseringentoocentosubuntu;douseradd$user;echo$user|passwd--stdin$user;done
[[email protected]~]#groupadddevelop
[[email protected]~]#usermod-Gdevelopgentoo
[[email protected]~]#usermod-Gdevelopcentos
[[email protected]~]#pdbedit-acentos
[[email protected]~]#pdbedit-aubuntu
[[email protected]~]#pdbedit-agentoo
[[email protected]~]#mkdir/data
[[email protected]~]#chmod777/data#設定共享目錄的檔案系統許可權為全放開
[[email protected]~]#vim/etc/samba/smb.conf
workgroup=magedu#共享組名稱
serverstring=SambaServerVersion%v
netbiosname=MYSERVER
hostsallow=172.16.0.0/16#允許訪問的ip網段
......
security=user#驗證模式為samba資料庫驗證
passdbbackend=tdbsam
[data]
comment=shared
path=/data
browseable=yes#允許所有使用者可檢視次目錄
writable=yes
writelist=+develop#允許develop組的使用者訪問
[[email protected]~]#smbclient//192.168.1.6/data-Ugentoo#驗證
Entergentoo'spassword:
Domain=[MAGEDU]OS=[Unix]Server=[Samba3.6.23-36.el6_8]
smb:\>lcd/etc
smb:\>putpasswd
puttingfilepasswdas\passwd(1123.9kb/s)(average1124.0kb/s)
smb:\>exit
[[email protected]~]#smbclient//192.168.1.6/data-Uubuntu#驗證
Enterubuntu'spassword:
Domain=[MAGEDU]OS=[Unix]Server=[Samba3.6.23-36.el6_8]
smb:\>lcd/etc
smb:\>putpasswd
NT_STATUS_ACCESS_DENIEDopeningremotefile\passwd



9、搭建一套檔案vsftp檔案共享服務,共享目錄為/ftproot,要求:(描述完整的過程)


1)基於虛擬使用者的訪問形式;

2)匿名使用者只允許下載,不允許上傳;

3)禁錮所有的使用者於其家目錄當中;

4)限制最大併發連線數為200:;

5)匿名使用者的最大傳輸速率512KB/s

6)虛擬使用者的賬號儲存在mysql資料庫當中。

7)資料庫通過NFS進行共享。


一.mysql,pam,vsftpd相關軟體包的安裝

[[email protected]~]#yuminstallvsftpd-y
[[email protected]~]#yuminstallmysqlmysql-servermypam-mysql-y
[[email protected]~]#servicevsftpdstart
[[email protected]~]#servicemysqldstart
[[email protected]~]#ss-tlun|grep"21"
tcpLISTEN032*:21*:*
[[email protected]~]#ss-tlunp|grep"3306"
;
tcpLISTEN050*:3306*:*users:(("mysqld",1824,10))


二.準備資料庫及相關表

mysql>createdatabasevsftpd;mysql>grantselectonvsftpd.*to[email protected]identifiedby'vsftpd';mysql>grantselectonvsftpd.*to[email protected]identifiedby'vsftpd';mysql>flushprivileges;mysql>usevsftpd;mysql>createtableusers(idintAUTO_INCREMENTNOTNULL,namechar(20)binaryNOTNULL,passwordchar(48)binaryNOTNULL,primarykey(id));
mysql>insertintousers(name,passwword)values('wuzhenliang',password('vsftpd'));
mysql>insertintousers(name,passwword)values('wangyan',password('vsftpd'));

三.vsftp伺服器相關配置

[[email protected]~]#mkdir-pv/ftproot#建立ftp根目錄
[[email protected]~]#chownvuser.vuser/ftproot
[[email protected]~]useraddvuser-s/sbin/nologin-d/var/ftproot#建立ftp虛擬使用者
[[email protected]~]#vim/etc/vsftpd/vsftpd.confanonymous_enable=YES#允許匿名使用者登入
anon_umask=022#匿名使用者建立的檔案許可權預設為644!此處即是之後虛擬使用者上傳檔案獲得的預設許可權,不設定的話預設為700,將導致上傳的檔案無法讀取和下載
local_enable=YES#允許系統使用者登入
write_enable=NO#系統使用者擁有寫許可權
anon_upload_enable=NO#匿名使用者不允許上傳
anon_mkdir_write_enable=NO#匿名使用者不允許建立目錄anon_other_write_enable=NO#匿名使用者不允許刪除檔案,目錄
chroot_local_user=YES#將使用者禁錮在自己的家目錄中
guest_enable=YES
guest_username=vuser#指明虛擬使用者對映到的系統使用者listen=YESpam_service_name=vsftpd.mysql#指明使用/etc/pam.d/vsftpd.mysql來認證
anon_max_rate=512000#匿名使用者的最大傳輸速率為200kb/s
max_client=200#最大的併發數為200
user_config_dir=/etc/vsftpd/vusers_config#設定虛擬使用者的配置檔案路徑(可設定某虛擬使用者擁有上傳和刪除的許可權,只需要在虛擬使用者的配置檔案中做相關設定即可
[[email protected]~]#mkdir/etc/vsftpd/vusers_config/
[[email protected]~]#cd/etc/vsftpd/vusers_config/
[[email protected]~]#vimwuzhenliang
anon_upload_enable=YES
anon_mkdir_write_enable=YES
anon_other_write_enable=YES

四.共享mysql資料庫


nfs伺服器端部署

-----------------------------------------------------------------------------

[[email protected]~]#yuminstallnfs-utilsrpcbind#選擇另一臺主機作為nfs的伺服器安裝nfs相關
的元件
[[email protected]~]#servicerpcbindstart#按照順序啟動rpcbind以及nfs服務
[[email protected]~]#servicenfsstart
[[email protected]~]#mkdir/nfs/#建立共享目錄
[[email protected]~]#chownnfsnobody.nfsnobody/nfs#更改共享目錄屬主屬組
[[email protected]~]#vi/etc/exports#編輯nfs主配置檔案
/nfs192.168.230.0/24(rw,sync,no_root_squance)#nfs伺服器引數設定,no_root_squance任何登入該共享目錄的使用者
[[email protected]~]#showmount-elocalhost#檢視本機nfs的共享目錄


nfs客戶端部署

------------------------------------------------------------------------------------------

[[email protected]~]#yuminstallnfs-utilsrpcbind#在vsftpd與mysql的同一臺主機上安裝nfs相關元件,作為nfs的客戶端
[[email protected]]#mount-tnfs192.168.230.117:/nfs/mydata/var/mysql/


五.測試部署的結果

[[email protected]~]#ftp192.168.230.123
Connectedto192.168.230.123(192.168.230.123).
220(vsFTPd2.2.2)
Name(192.168.230.123:root):wuzhenliang
331Pleasespecifythepassword.
Password:
230Loginsuccessful.
RemotesystemtypeisUNIX.
Usingbinarymodetotransferfiles.
ftp>putwww.txt#檔案下載測試
local:www.txtremote:www.txt
227EnteringPassiveMode(192,168,230,123,74,144).
150Oktosenddata.
226Transfercomplete.
118bytessentin0.0671secs(1.76Kbytes/sec)
ftp>ls
227EnteringPassiveMode(192,168,230,123,121,250).
150Herecomesthedirectorylisting.
drwx------35045054096Nov1605:46132
drwx------35045054096Nov1605:46Notes
-rw-------1504505118Nov1607:29www.txt
ftp>getwww.txt#檔案下載測試
local:www.txtremote:www.txt
227EnteringPassiveMode(192,168,230,123,21,154).
150OpeningBINARYmodedataconnectionforwww.txt(0bytes).
226Transfercomplete.






轉載於:https://blog.51cto.com/dannniswu/1865853