1http協(xié)議
一、http協(xié)議
- 應(yīng)用層協(xié)議
- 作用
- 在客戶端、web服務(wù)器傳遞數(shù)據(jù)
- Hyper Text Transfer Protocol 超文本傳輸協(xié)議
- 客戶端訪問web服務(wù)時,傳遞代碼,由瀏覽器解析代碼進(jìn)行展示
二、web服務(wù)
1、類型
- 靜態(tài)網(wǎng)站
- 網(wǎng)頁上所有資源都是固定的
- html語言,js, jquery
- 動態(tài)網(wǎng)站
- 一段程序代碼,根據(jù)傳遞的參數(shù)不同展示不同的結(jié)果
- PHP語言? ? ? ? ?*.php文件
- Java語言? ? ? ? ?*.jsp文件
- 一段程序代碼,根據(jù)傳遞的參數(shù)不同展示不同的結(jié)果
2、cookie、session
- 作用
- 識別客戶端登錄身份
?
?三、HTTP協(xié)議特性
1、http/0.9
- 只支持傳輸純文本數(shù)據(jù)
2、http/1.0
- 引用MIME機制
- 傳輸非文本數(shù)據(jù)(圖片、視頻、音頻、動畫)
- 緩存機制,加速網(wǎng)站的訪問
- 服務(wù)器緩存
- 客戶端緩存
3、http/1.1
- 長連接機制? ? keepalive
- 允許在一條連接上發(fā)送多次請求
- 限制長連接的超時時間
- 限制每個長連接的最大請求數(shù) 5000
- 允許在一條連接上發(fā)送多次請求
- 管道機制、
- 允許一次性發(fā)送多個請求,并行
- 加強了緩存的管理
- 緩存靜態(tài)數(shù)據(jù)
- 緩存過期時間
4、http/2
- 以并行的方式發(fā)送請求、響應(yīng)
四、HTTP狀態(tài)碼、請求方法
1、狀態(tài)碼
- 200
- 成功響應(yīng)
- 301、302、304
- 成功響應(yīng)
- 重定向
- 4xx
- 錯誤響應(yīng)
- 403? ? ? ? ? 權(quán)限拒絕
- 404? ? ? ? ? ?文件找不到
- 錯誤響應(yīng)
- 5xx?
- 錯誤響應(yīng)
- 服務(wù)端配置
- 錯誤響應(yīng)
2、請求方法
- GET
- 用于獲取內(nèi)容、數(shù)據(jù)
- POST
- 上傳數(shù)據(jù)
- DELETE
2httpd安裝配置
一、httpd軟件介紹
- Linux
- httpd、nginx、tomcat
- Windows server
- IIS
httpd介紹:
- 構(gòu)建web服務(wù)器
- 處理靜態(tài)頁面
- http://httpd.apache.org/
1、httpd軟件版本
- 2.2
- 2.4
2、httpd部署方式
- RPM
- 源碼編譯
3、httpd特性
- 開源、跨平臺的
- 模塊化軟件
- 支持虛擬主機功能
- 支持https虛擬主機
- 支持緩存
二、安裝啟動httpd
1、安裝httpd
[root@8 ~]# yum install -y httpd
2、啟動httpd
[root@8 ~]# systemctl start httpd
[root@8 ~]# systemctl enable httpd
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
root@8 ~]# netstat -anlp | grep http
tcp6 0 0 :::80 :::* LISTEN 8048/httpd
unix 3 [ ] STREAM CONNECTED 27734 8048/httpd
[root@8 ~]# ps -elf | grep http
4 S root 8048 1 0 80 0 - 57610 poll_s 20:14 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
5 S apache 8049 8048 0 80 0 - 57610 inet_c 20:14 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
5 S apache 8050 8048 0 80 0 - 57610 inet_c 20:14 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
5 S apache 8051 8048 0 80 0 - 57610 inet_c 20:14 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
5 S apache 8052 8048 0 80 0 - 57610 inet_c 20:14 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
5 S apache 8053 8048 0 80 0 - 57610 inet_c 20:14 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
0 S root 8075 7980 0 80 0 - 28206 pipe_w 20:16 pts/0 00:00:00 grep --color=auto http
- 父進(jìn)程
- 管理子進(jìn)程、記錄日志、讀取配置文件
- 子進(jìn)程
- 用于接收、處理客戶端請求
3、測試訪問httpd
http://192.168.50.8/
?4、刪除測試頁面
[root@8 ~]# rm -rf /etc/httpd/conf.d/welcome.conf
[root@8 ~]# systemctl restart httpd
5、建立測試網(wǎng)頁
root@8 ~]# ls /var/www/html/
index.html music.html
[root@8 ~]# vim /var/www/html/music.html
?三、httpd相關(guān)文件目錄
1、主配置文件
/etc/httpd/conf/httpd.conf
2、子配置文件
/etc/httpd/conf.d/*.conf
3、模塊路徑、配置文件
/etc/httpd/modules
/etc/httpd/conf.modules.d/*.conf
4、存放PID文件
/etc/httpd/run?
5、日志目錄、
/var/log/httpd
access_log error_log
- access_log:訪問日志
- error_log:錯誤日志
6、默認(rèn)網(wǎng)頁數(shù)據(jù)目錄
/var/www/html
四、配置文件解析——httpd.conf
1、指定httpd的工作目錄
ServerRoot "/etc/httpd"
2、指定監(jiān)聽的端口
Listen 192.168.59.8:80
3、加載子配置文件
Include conf.modules.d/*.conf
4、指定啟動進(jìn)程的用戶身份
User www
Group www
5、指定管理員郵箱
ServerAdmin root@localhost
6、指定網(wǎng)站的主機名
ServerName test1.linux.com
7、指定網(wǎng)頁目錄
DocumentRoot "/var/www/html"
8、指定默認(rèn)的網(wǎng)頁名稱
<IfModule dir_module>
DirectoryIndex index.html
</IfModule>
9、錯誤日志文件、級別
ErrorLog "logs/error_log"
# Possible values include: debug, info, notice, warn, error, crit, alert, emerg.
LogLevel error
10、訪問日志名稱、記錄格式
CustomLog "logs/access_log" combined
web訪問量:
1、PV Page View 頁面訪問量
2、UV User View 用戶訪問量
11、定義訪問日志格式
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
變量說明:
- %h 客戶端地址
- %l web認(rèn)證用戶
- %u windows用戶
- %t 訪問時間
- %r 請求方法 文件名稱 HTTP協(xié)議版本
- %s 狀態(tài)碼
- %b 響應(yīng)數(shù)據(jù)的大小
- %{Referer}i 超鏈接地址
- %{User-Agent}i 瀏覽器類型
12、長連接配置
KeepAliveTimeout 5
MaxKeepAliveRequests 100
13、針對目錄授權(quán)
<Directory "/var/www/html">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
配置說明:
- options
- 客戶端訪問對應(yīng)目錄下的文件時受什么影響
- Indexes
- 無index.html首頁時,列出所有文件
- FollowSymLinks
- 允許網(wǎng)頁目錄下的軟鏈接文件被正常訪問
- Indexes
- 客戶端訪問對應(yīng)目錄下的文件時受什么影響
- require 客戶端訪問認(rèn)
14、客戶端訪問認(rèn)證
- 基于客戶端地址進(jìn)行認(rèn)證
- 基于用戶名、密碼進(jìn)行認(rèn)證
1) 基于客戶端地址進(jìn)行認(rèn)證
Require all granted //允許所有客戶端訪問
Require ip 10.1.1.1 //僅允許10.1.1.1訪問
<RequireAll>
Require all granted
Require not ip 10.252.46.165
</RequireAll>
Require all denied
2) 基于用戶名、密碼認(rèn)證
- a) 創(chuàng)建認(rèn)證用戶
[root@localhost ~]# htpasswd -c /etc/httpd/.webuser martin
New password:
Re-type new password:
Adding password for user martin
[root@localhost ~]#
[root@localhost ~]# cat /etc/httpd/.webuser
martin:$apr1$6KV5I5w.$6iQ5ip.1bF3la2pHq9lX4/
b) 編輯httpd.conf
[root@localhost ~]# vim /etc/httpd/conf/httpd.conf
<Directory "/var/www/html">
........................
AuthType Basic
AuthName "Need to login: "
AuthUserFile "/etc/httpd/.webuser"
Require valid-user
</Directory>
c) 重啟服務(wù)
[root@localhost ~]# systemctl restart httpd
3虛擬主機
虛擬主機 Virtual Host
一、虛擬主機介紹
1、作用
- 在一臺服務(wù)器可以部署多套網(wǎng)站
-
注意:
- 虛擬主機配置后,主配置文件中部署的網(wǎng)站會失效!!!!!
2、類型
- 基于名稱的虛擬主機 【常用】
- 不同的網(wǎng)站指定不同的ServerName、DocumentRoot
- 基于IP地址的虛擬主機
- 不同的網(wǎng)站監(jiān)聽在不同的IP地址上
- 基于端口的虛擬主機
3、配置虛擬主機
<VirtualHost IP:port>
DocumentRoot xxxxx
ServerName xxxxx
ErrorLog xxxxxxxxxxxx
CustomLog xxxxxxxxxxxxxxxx
</VirtualHost>
二、基于名稱的虛擬主機
網(wǎng)站名稱: vedio.linux.com 網(wǎng)頁目錄: /var/www/html/vedio
網(wǎng)站名稱: mp3.linux.com 網(wǎng)頁目錄: /mp3
1、配置vedio網(wǎng)站
1) 創(chuàng)建網(wǎng)頁目錄、測試首頁
[root@localhost ~]# mkdir /var/www/html/vedio
[root@localhost ~]# vim /var/www/html/vedio/index.html
<h1> vedio </h1>
2) 編輯虛擬主機配置文件
[root@localhost ~]# cat /etc/httpd/conf.d/vedio.conf
<VirtualHost 192.168.140.10:80>
ServerName vedio.linux.com
DocumentRoot /var/www/html/vedio
ErrorLog /var/log/httpd/vedio_error.log
CustomLog /var/log/httpd/vedio_access.log combined
</VirtualHost>
[root@localhost ~]# httpd -t
Syntax OK
[root@localhost ~]# systemctl restart httpd
3) 測試訪問
2、配置mp3網(wǎng)站
1) 創(chuàng)建網(wǎng)頁目錄、測試首頁
[root@localhost ~]# mkdir /mp3
[root@localhost ~]# vim /mp3/index.html
<h1> mp3 </h1>
2) 編輯配置文件
[root@localhost ~]# cat /etc/httpd/conf.d/mp3.conf
<VirtualHost 192.168.140.10:80>
ServerName mp3.linux.com
DocumentRoot /mp3
ErrorLog /var/log/httpd/mp3_error.log
CustomLog /var/log/httpd/mp3_access.log combined
</VirtualHost>
<Directory "/mp3">
Require all granted
</Directory>
[root@localhost ~]# httpd -t
Syntax OK
[root@localhost ~]# systemctl restart httpd
3) 測試訪問
三、基于IP地址的虛擬主機
網(wǎng)站: python.linux.com 網(wǎng)頁目錄: /python 192.168.140.100
網(wǎng)站: shell.linux.com 網(wǎng)頁目錄: /shell 192.168.140.200
1、主機添加網(wǎng)卡、配置多個地址
[root@localhost network-scripts]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:8f:c3:68 brd ff:ff:ff:ff:ff:ff
inet 192.168.140.10/24 brd 192.168.140.255 scope global noprefixroute ens33
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe8f:c368/64 scope link
valid_lft forever preferred_lft forever
3: ens37: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:8f:c3:72 brd ff:ff:ff:ff:ff:ff
inet 192.168.140.100/24 brd 192.168.140.255 scope global noprefixroute ens37
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe8f:c372/64 scope link
valid_lft forever preferred_lft forever
4: ens38: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:8f:c3:7c brd ff:ff:ff:ff:ff:ff
inet 192.168.140.200/24 brd 192.168.140.255 scope global noprefixroute ens38
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe8f:c37c/64 scope link tentative
valid_lft forever preferred_lft forever
2、配置python網(wǎng)站
[root@localhost ~]# cat /etc/httpd/conf.d/python.conf
<VirtualHost 192.168.140.100:80>
ServerName python.linux.com
DocumentRoot /python
ErrorLog /var/log/httpd/python_error.log
CustomLog /var/log/httpd/python_access.log combined
</VirtualHost>
<Directory "/python">
Require all granted
</Directory>
3、配置shell網(wǎng)站
[root@localhost ~]# cat /etc/httpd/conf.d/shell.conf
<VirtualHost 192.168.140.200:80>
ServerName shell.linux.com
DocumentRoot /shell
ErrorLog /var/log/httpd/shell_error.log
CustomLog /var/log/httpd/shell_access.log combined
</VirtualHost>
<Directory "/shell">
Require all granted
</Directory>
4基于https協(xié)議部署虛擬主機
一、網(wǎng)站傳輸協(xié)議
- http
- 80/tcp
- 數(shù)據(jù)以明文的方式傳輸
- https
- 443/tcp
- 數(shù)據(jù)以密文的方式傳輸
二、安全性保障
- 數(shù)據(jù)安全性
- 加密、解密
- 數(shù)據(jù)完整性
- 校驗算法 MD5/SHA hash哈希算法
- 身份的真實性保障
三、數(shù)據(jù)安全性的保障
通過加密算法確保數(shù)據(jù)的安全
發(fā)送方發(fā)送數(shù)據(jù)前加密數(shù)據(jù)、接收方解密數(shù)據(jù)
1、對稱加密算法
加密數(shù)據(jù)時使用的密鑰、解密的密鑰是一樣的
典型對稱加密算法
- DES
- 3DES
- AES
加密數(shù)據(jù)
# openssl enc -e -des -in /tmp/file01 -out /tmp/file01_s
enter des-cbc encryption password:
Verifying - enter des-cbc encryption password:
解密數(shù)據(jù)
# openssl enc -d -des -in /opt/file01_s -out /opt/file01
enter des-cbc decryption password:
2、非對稱加密算法
加密、解密數(shù)據(jù)時用的密鑰是不一樣的
密鑰對 【公鑰、私鑰】
數(shù)據(jù)加密:
公鑰加密、私鑰解密
典型非對稱加密算法:
- RSA
- DSA
實際使用:
使用對稱算法加密真實的數(shù)據(jù), 使用非對稱加密算法加密對稱算法的密鑰
四、數(shù)據(jù)完整性保障
1、校驗算法
- MD5
- SHA
發(fā)送數(shù)據(jù)時,使用校驗算法對數(shù)據(jù)進(jìn)行校驗,生成校驗碼,同時將數(shù)據(jù)+校驗碼發(fā)送出去
接收方接收數(shù)據(jù),使用相同的校驗算法再次進(jìn)行校驗,對比校驗碼
五、身份的真實性
1、密鑰對
- 數(shù)據(jù)加密
- 公鑰加密、私鑰解密
- 驗證簽名
- 私鑰簽名、公鑰驗證簽名
流程:
1、電商服務(wù)器生成證書申請【.csr文件】, 同時將電商服務(wù)器的公鑰放入證書申請;將證書申請發(fā)送給CA
2、CA審批信息, 通過后CA會使用自己的私鑰進(jìn)行簽名;相當(dāng)于簽署證書【.crt】
3、客戶端訪問電商服務(wù)器時,服務(wù)器會將證書信息發(fā)送給客戶端
4、客戶端通過CA的公鑰驗證證書,驗證通過后可獲取電商服務(wù)器的公鑰
5、客戶端選取對稱算法、密鑰,使用公鑰加密,發(fā)送給服務(wù)器、服務(wù)器解密
6、進(jìn)行真實數(shù)據(jù)交互
六、配置基于https協(xié)議的虛擬主機
1、配置私有CA
- 生成密鑰對
- 生成自簽證書
1) 創(chuàng)建CA服務(wù)器需要的數(shù)據(jù)庫文件
[root@ca ~]# touch /etc/pki/CA/index.txt
[root@ca ~]# echo 01 > /etc/pki/CA/serial
2) 創(chuàng)建CA服務(wù)器需要的密鑰
[root@ca ~]# openssl genrsa -out /etc/pki/CA/private/cakey.pem 1024
Generating RSA private key, 1024 bit long modulus
..++++++
.....++++++
e is 65537 (0x10001)
3) 頒發(fā)自簽證書
[root@ca ~]# openssl req -new -x509 -key /etc/pki/CA/private/cakey.pem -out /etc/pki/CA/cacert.pem -days 3650
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]:china
string is too long, it needs to be less than 2 bytes long
Country Name (2 letter code) [XX]:cn
State or Province Name (full name) []:bj
Locality Name (eg, city) [Default City]:bj
Organization Name (eg, company) [Default Company Ltd]:bj
Organizational Unit Name (eg, section) []:bj
Common Name (eg, your name or your server's hostname) []:ca.linux.com
Email Address []:bj@qq.com
[root@ca ~]#
[root@ca ~]#
[root@ca ~]# ls /etc/pki/CA/
cacert.pem certs crl index.txt newcerts private serial
2、在web server上申請證書
1) 在web server創(chuàng)建密鑰對
[root@web_server ~]# mkdir /etc/httpd/ssl
[root@web_server ~]# openssl genrsa -out /etc/httpd/ssl/www.linux.com.key 2048
Generating RSA private key, 2048 bit long modulus
......................+++
.....................................................................+++
e is 65537 (0x10001)
[root@web_server ~]#
2) 創(chuàng)建證書申請
[root@localhost ~]# openssl req -new -key /etc/httpd/ssl/www.linux.com.key -out /etc/httpd/ssl/www.linux.com.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) []:bj
Locality Name (eg, city) [Default City]:bj
Organization Name (eg, company) [Default Company Ltd]:bj
Organizational Unit Name (eg, section) []:bj
Common Name (eg, your name or your server's hostname) []:www.linux.com
Email Address []:bj@qq.com
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
3) 將證書申請發(fā)送給CA
[root@web_server ~]# rsync -av /usr/local/nginx/ssl/www.linux.com.csr root@192.168.140.11:/tmp/
4) CA簽署證書
[root@ca ~]# openssl ca -in /tmp/www.linux.com.csr -out /etc/pki/tls/certs/www.linux.com.crt -days 3650
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: Jun 21 06:06:54 2021 GMT
Not After : Jun 19 06:06:54 2031 GMT
Subject:
countryName = cn
stateOrProvinceName = bj
organizationName = bj
organizationalUnitName = bj
commonName = www.linux.com
emailAddress = bj@qq.com
X509v3 extensions:
X509v3 Basic Constraints:
CA:FALSE
Netscape Comment:
OpenSSL Generated Certificate
X509v3 Subject Key Identifier:
67:6D:B8:84:3A:5C:BE:99:81:4A:49:F4:CD:61:5E:8B:99:3B:EF:13
X509v3 Authority Key Identifier:
keyid:C8:EF:D2:FB:E8:D5:2B:50:D8:7C:07:7A:27:72:B5:D6:33:09:D6:56
Certificate is to be certified until Jun 19 06:06:54 2031 GMT (3650 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@ca ~]# ls /etc/pki/tls/certs/
ca-bundle.crt ca-bundle.trust.crt make-dummy-cert Makefile renew-dummy-cert www.linux.com.crt
[root@ca ~]#
[root@ca ~]# cat /etc/pki/CA/serial
02
[root@ca ~]# cat /etc/pki/CA/index.txt
V 310619060654Z 01 unknown /C=cn/ST=bj/O=bj/OU=bj/CN=www.linux.com/emailAddress=bj@qq.com
5) 將證書發(fā)送給web server
[root@ca ~]# rsync -av /etc/pki/tls/certs/www.linux.com.crt root@192.168.140.10:/etc/httpd/ssl
3、安裝mod_ssl模塊
[root@localhost ~]# yum install -y mod_ssl
4、創(chuàng)建網(wǎng)頁目錄、測試文件
[root@localhost ~]# mkdir /linux
[root@localhost ~]# vim /linux/index.html
<h1> www.linux.com </h1>
5、配置ssl虛擬主機
[root@localhost ~]# vim /etc/httpd/conf.d/ssl.conf
....................
<VirtualHost _default_:443>
# General setup for the virtual host, inherited from global configuration
DocumentRoot "/linux"
ServerName www.linux.com:443
SSLCertificateFile /etc/httpd/ssl/www.linux.com.crt
SSLCertificateKeyFile /etc/httpd/ssl/www.linux.com.key
<Directory "/linux">
Require all granted
</Directory>
[root@localhost ~]# httpd -t
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain. Set the 'ServerName' directive globally to suppress this message
Syntax OK
[root@localhost ~]# systemctl restart httpd
[root@localhost ~]# netstat -antp | grep http
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 7144/httpd
tcp6 0 0 :::443 :::* LISTEN 7144/httpd
6、測試訪問
Linux.com - News For Open Source Professionals
文章來源:http://www.zghlxwxcb.cn/news/detail-845977.html
七、配置https自動跳轉(zhuǎn)文章來源地址http://www.zghlxwxcb.cn/news/detail-845977.html
RewriteEngine On
RewriteCond %{HTTP_HOST} www.linux.com [NC]
RewriteRule ^/ https://www.linux.com [L]
- %{HTTP_HOST}
- 調(diào)用客戶端所訪問的網(wǎng)站主機名
- [NC]
- 忽略大小寫
- [L]
- 立即響應(yīng)
到了這里,關(guān)于云計算Linux運維——基礎(chǔ)服務(wù)應(yīng)用——HTTPD服務(wù)(8)更新中......的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!