Linux http服務
網絡服務通信基礎:
1、端口號就是進程標識,每個用戶最多只能打開1024個進程。
2、MAC地址僅用于局域網內部通信(或本地通信),ip地址用于實現從源主機到目標主機的跨網絡通信。
3、端口分配:
0-1023:永久的分配給固定的應用使用;
例:80/http,21/ftp,25/smtp,110/pop3,143/lmap4等;僅root有權限使用特權端口。
1024-41951:為注冊端口,但不嚴格,例:3306/mysql, 11211/memcached等。
41952-65535:客戶端程序使用的隨機端口,又被稱為“動態端口”,或稱為私有端口。
/proc/sys/net/ipv4/ip_local_port_range (定義本地端口范圍)
4、http協議:
應用層協議:超文本傳輸
http/0.9
http/1.0:cache, MIME
MIME: multipurpose internet mail extensions (多用途因特網郵件擴展)
http/1.1:緩存功能,條件式請求;
http/2.0:
html語言:
<html>
<head>
<title>MageEdu</title>
</head>
<body>
<h1> NI HAO </h>
</body>
</html>
http協議的實現:
開源實現:httpd(apache), nginx, lighttpd, …
C/S:
C: browser, user agent(用戶代理),
圖形瀏覽器:chrome, ie, firefox, safari, opera, …
字符瀏覽器:elinks, curl, wget, …
S:httpd(apache), nginx, lighttpd, …
通信模型:
請求/響應
無狀態連接stateless;追蹤用戶身份:cookie;胖cookie安裝在客戶端;
一次完整的Http請求處理過程:
(1) 建立或處理連接請求;
(2) 接收請求;
(3) 解析請求,處理請求;
(4) 加載用戶請求的資源;
(5) 構建響應報文;
(6) 發送響應報文;
(7) 記錄訪問于日志中;
web資源:
url:統一資源定位符;
shceme://host[:port]/URL
URL的根通常要映射為文件系統上的某路徑;
DocumentRoot /var/www/html/
/index.html –> /var/www/html/index.html
Alias /images/ /data/imgs/
/images/logo.jpg –> /data/imgs/logo.jpg
衡量網站活躍度的指標:
pv:page view (頁面瀏覽量)
uv:unique view (獨立IP對網站的瀏覽量)
http頭部信息事務:request/response
request:
<method> <url> <version>
HEADERS
<body>
response:
<version> <status> <reason-phrase>
HEADERS
<body>
HEADERS:
name: value
name: value
<method>:GET,HEAD,POST, PUT, DELETE, OPTIONS, TRACE, …
<status>:
1xx:消息
2xx: 成功響應
3xx: 重定向響應
4xx: 客戶端錯誤
5xx: 服務端錯誤
httpd特性:
高度模塊化設計:core modules + standard modules + 3rd party modules
DSO: Dynamic Shared Object
MPM: multipath process modules (多路處理模塊)
prefork:process(進程)
每進程響應一個請求;
worker: thread(線程)
每線程響應一個請求;
event: thread(線程)
每進程響應多個請求;
豐富功能:
CGI:動態網站;
虛擬主機:IP,PORT,ServerName
反向代理:http, fcgi, wsgi, ajp, …
負載均衡:
…
版本:
httpd程序版本:
httpd-1.x
httpd-2.0
httpd-2.2
httpd-2.4
安裝使用httpd:
base(安裝軟件包優先使用隨光盤發行的rpm包)
epel
查看http服務器軟件包是否安裝:
[root@centos7 ipv4]# yum info httpd
程序環境:
主程序文件:
/usr/sbin/httpd
模塊文件:
/usr/lib64/httpd/modules/*.so
主配置文件:
/etc/httpd/conf/httpd.conf
/etc/httpd/conf.d/*.conf
/etc/httpd/conf.modules.d/*.conf
站點文檔路徑:
/var/www/html
日志文件路徑:
/var/log/httpd/
access_log:訪問日志
error_log:錯誤日志
Unit File:
/usr/lib/systemd/system/httpd.service
自帶腳本:
/usr/sbin/apachectl
啟動http服務:systemctl start httpd.service
開機啟動http服務:systemctl enable httpd.service
~]# ss -tnlp | grep ":80\>"
打開firefox瀏覽器訪問http服務:
使用IP地址訪問我們會看到http服務的歡迎測試頁。
配置文件修改完成后:
(1)測試語法:httpd -t
(2)讓服務程序重載配置文件
centos6~]# service httpd reload
centos7~]# systemctl reload httpd.service
監聽端口:
監聽的地址和端口
Listen [ip:]port
Listen可重復監聽多個端口,添加端口時,只需reload服務就可以,如果是更改原有端口需restart服務。
保持連接:
persistent connection:tcp連接建立后,資源獲取完成之后不會斷開連接,而是繼續等待請求其它資源;
如何斷開?
服務器發起斷開連接;
數量限制
時間限制
KeepAlive On|Off 表示是否啟用保持連接;
MaxKeepAliveRequests 100 表示一次可以請求多少個資源;
KeepAliveTimeout 10 表示保持連接多久斷開;(默認為時間單位:秒 ms:毫秒)
示例:
關閉keepalive保持連接:
vim /etc/httpd/conf.d/keepalive.conf
KeepAlive on
MaxKeepAliveRequests 50
KeepAliveTimeout 5
使用httpd -M查看已加載的所有模塊:
-t -D DUMP_MODULES : show all loaded modules
-M : a synonym for -t -D DUMP_MODULES
示例:
[root@centos7 conf.modules.d]# httpd -M
如果想禁用某模塊加載,可以去 vim /etc/httpd/conf.modules.d/00-base.conf配置文件中使用“#”注釋掉,然后重新加載httpd服務即可:
示例:
[root@centos7 conf.modules.d]# vim /etc/httpd/conf.modules.d/00-base.conf
#LoadModule suexec_module modules/mod_suexec.so //注釋掉這個模塊
[root@centos7 conf.modules.d]# httpd -M | grep "suexec"
[root@centos7 conf.modules.d]#
Main Server(主服務)相關配置:
(1) DocumentRoot
站點文檔根路徑;
(2) ServerName
服務器名稱;
站點文檔訪問授權及眾多服務特性的配置:
基于文件系統路徑:
<Directory "/PATH/TO/DIR">
</Directory>
示例:
更改站點文檔根路徑:
創建對應的站點文檔根路徑:
[root@centos7 conf]# mkdir -pv /web/htdocs
編輯網站測試頁:
[root@centos7 conf]# vim /web/htdocs/test.html
nihao
重新加載httpd服務:
[root@centos7 conf]# systemctl reload httpd
刪除httpd服務默認歡迎頁:
[root@centos7 conf.d]# pwd
/etc/httpd/conf.d
[root@centos7 conf.d]# mv welcome.conf welcome.conf.bak
使用links訪問:
[root@centos7 conf]# links 10.1.253.56/test.html
Options
Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
None
All
Indexes:索引
FollowSymLinks:允許跟蹤符號鏈接
ExecCGI:允許執行CGI腳本
AllowOverride
httpd的訪問控制配置,允許每目錄單獨進行;在每個目錄下建立一個.htaccess文件;
AllowOverride表示是否允許目錄中的.htaccess文件中的配置來覆蓋當前配置段中的配置;
Options FileInfo AuthConfig Limit
All
None
基于源地址的訪問控制
允許所有地址訪問:Require all granted
拒絕所有地址訪問:Require all denied
<RequireAll>
</RequireAll>
基于IP控制:
Require ip ADDRESS
Require not ip ADDRESS
ADDRESS:
ip
network:
10.1.0.0/255.255.0.0
10.1.0.0/16
10.1
基于主機名控制:
Require host HOSTNAME
Require not host HOSTNAME
HOSTNAME:
FQDN
DOMAIN.TLD
示例:
在httpd服務主配置文件中設置拒絕IP10.1.252.238的訪問:
vim /etc/httpd//conf/httpd.conf
User/Group
進程的運行者身份
httpd服務運行屬主、屬組;
User apache
Group apache
httpd-manual :安裝httpd自帶的官方文檔包
# yum -y install httpd-manual
配置文件:conf.d/manual.conf
systemctl reload httpd
查看Apache server status
vim /etc/httpd/conf.d/status.conf
1 <Location /status>
2 SetHandler server-status
3 <RequireAll>
4 Require ip 127.0.0.1
5 Require all denied
6 </RequireAll>
7 </Location>
http://127.0.0.1/status
日志設定:
錯誤日志:
ErrorLog "/var/log/httpd/error_log"
LogLevel warn
Possible values include: debug, info, notice, warn, error, crit, alert, emerg.
訪問日志:
LogFormat "FORMAT_STRINGS" LOG_FORMAT_NAME
CustomLog "/PATH/TO/LOG_FILE" LOG_FORMAT_NAME
format_strings:
%h:Remote hostname. Will log the IP address if HostnameLookups is set to Off, which is the default.
%l:Remote logname (from identd, if supplied). 無有效值時,使用“–”表示;
%u: Remote user if the request was authenticated. May be bogus if return status (%s) is 401 (unauthorized). http協議認證時由客戶端輸入的用戶名;
%t:Time the request was received, in the format [18/Sep/2011:19:18:28 -0400].
%r:First line of request.
%>s:Status. For requests that have been internally redirected, this is the status of the original request. Use %>s for the final status.
%b:Size of response in bytes, excluding HTTP headers.
%{VARNAME}i:記錄由VARNAME所表示的請求報文首部的值,例如%{Referer}i,則表示記錄請求報文中Referer首部的值;
虛擬主機:
虛擬主機標識方式:
基于IP地址;
基于主機名(ServerName);
基于端口(port);
實驗:
搭建基于ip、port、ServerName的虛擬主機:
首先創建“DocumentRoot”站點文檔根目錄及index.html索引文件;
[root@centos7 httpd]# mkdir -pv /vhost/{,www1,www2,www3}
[root@centos7 httpd]# vim /vhost/www1/index.html (this is www1)
[root@centos7 httpd]# vim /vhost/www2/index.html (this is www2)
[root@centos7 httpd]# vim /vhost/www3/index.html (this is www3)
示例:
基于端口的虛擬主機:
注意:基于端口的虛擬主機需要在/etc/httpd/conf/httpd.conf配置文件中添加監聽端口;
Listen 80
Listen 8080
Listen 8088
[root@centos7 httpd]# vim /etc/httpd/conf.d/virtualhost.conf
1 <VirtualHost *:80>
2 ServerName www1.magedu.com
3 DocumentRoot "/vhost/www1"
4 <Directory "/vhost/www1">
5 Options none
6 AllowOverride none
7 Require all granted
8 </Directory>
9 ErrorLog "logs/www1_error_log"
10 CustomLog "logs/www1_access_log" combined
11 </VirtualHost>
12
13 <VirtualHost *:8080>
14 ServerName www2.magedu.com
15 DocumentRoot "/vhost/www2"
16 <Directory "/vhost/www2">
17 Options none
18 AllowOverride none
19 Require all granted
20 </Directory>
21 ErrorLog "logs/www2_error_log"
22 CustomLog "logs/www2_accees_log" combined
23 </VirtualHost>
24
25 <VirtualHost *:8088>
26 ServerName www3.magedu.com
27 DocumentRoot "/vhost/www3"
28 <Directory "/vhost/www3">
29 Options none
30 AllowOverride none
31 Require all granted
32 </Directory>
33 ErrorLog "logs/www3_error_log"
34 CustomLog "logs/www3_accees_log" combined
35 </VirtualHost>
示例:
基于IP地址的虛擬主機:
注意:基于IP地址的虛擬主機,需添加2個ip地址;
[root@centos7 httpd]# ip a add 10.1.253.21 dev eno16777736
[root@centos7 httpd]# ip a add 10.1.253.22 dev eno16777736
[root@centos7 httpd]# vim /etc/httpd/conf.d/virtualhost.conf
1 <VirtualHost *:80>
2 ServerName www1.magedu.com
3 DocumentRoot "/vhost/www1"
4 <Directory "/vhost/www1">
5 Options none
6 AllowOverride none
7 Require all granted
8 </Directory>
9 ErrorLog "logs/www1_error_log"
10 CustomLog "logs/www1_access_log" combined
11 </VirtualHost>
12
13 <VirtualHost 10.1.253.21:80>
14 ServerName www2.magedu.com
15 DocumentRoot "/vhost/www2"
16 <Directory "/vhost/www2">
17 Options none
18 AllowOverride none
19 Require all granted
20 </Directory>
21 ErrorLog "logs/www2_error_log"
22 CustomLog "logs/www2_accees_log" combined
23 </VirtualHost>
24
25 <VirtualHost 10.1.253.22:80>
26 ServerName www3.magedu.com
27 DocumentRoot "/vhost/www3"
28 <Directory "/vhost/www3">
29 Options none
30 AllowOverride none
31 Require all granted
32 </Directory>
33 ErrorLog "logs/www3_error_log"
34 CustomLog "logs/www3_accees_log" combined
35 </VirtualHost>
示例:
基于主機名(ServerName)的虛擬主機:
注意:設置基于主機名的虛擬機時,需有dns如沒有,可以寫在/etc/hosts中;
[root@centos7 httpd]# vim /etc/hosts
10.1.253.56 www1.magedu.com www2.magedu.com www3.magedu.com
[root@centos7 httpd]# vim /etc/httpd/conf.d/virtualhost.conf
1 <VirtualHost *:80>
2 ServerName www1.magedu.com
3 DocumentRoot "/vhost/www1"
4 <Directory "/vhost/www1">
5 Options none
6 AllowOverride none
7 Require all granted
8 </Directory>
9 ErrorLog "logs/www1_error_log"
10 CustomLog "logs/www1_access_log" combined
11 </VirtualHost>
12
13 <VirtualHost *:80>
14 ServerName www2.magedu.com
15 DocumentRoot "/vhost/www2"
16 <Directory "/vhost/www2">
17 Options none
18 AllowOverride none
19 Require all granted
20 </Directory>
21 ErrorLog "logs/www2_error_log"
22 CustomLog "logs/www2_accees_log" combined
23 </VirtualHost>
24
25 <VirtualHost *:80>
26 ServerName www3.magedu.com
27 DocumentRoot "/vhost/www3"
28 <Directory "/vhost/www3">
29 Options none
30 AllowOverride none
31 Require all granted
32 </Directory>
33 ErrorLog "logs/www3_error_log"
34 CustomLog "logs/www3_accees_log" combined
35 </VirtualHost>
基于用戶的訪問控制:
Require user USERLIST
Require group GRPLIST
虛擬用戶:
認證方式:
basic
digest
http協議認證過程 :
認證質詢:
WWW-Authencate:響應碼為401,拒絕客戶端請求,并說明用戶需要輸入正確的賬號和密碼之后方可訪問;
認證:
Authorization:客戶端填入賬號和密碼,再次發送請求報文;認證通過,服務器發送響應內容;
用戶訪問認證授權控制:
<Directory "">
Options None
AllowOverride None
AuthType Basic
AuthName "STRING"
AuthUserFile ""
Require user USER1 USER2 … (valid-user)
</Directory>
賬號文件生成工具htpasswd
htpasswd [options] "/PATH/TO/HT_PASSWD_FILE" username
-c:創建此文件;
-m:md5加密密碼存放;
-s:sha加密
-D: 刪除指定用戶
Require的使用方式:
(1) Require valid-user (所有用戶)
(2) Require user USER1 USER2 … (指定用戶)
實驗:
對虛擬主機ServerName www1.magedu.com;做用戶授權訪問控制。
使用htpasswd命令工具生成認證授權文件和授權用戶;
[root@centos7 httpd]# htpasswd -c -m /etc/httpd/conf/.htpasswd zheng
[root@centos7 httpd]# htpasswd -m /etc/httpd/conf/.htpasswd mage
1 <VirtualHost *:80>
2 ServerName www1.magedu.com
3 DocumentRoot "/vhost/www1"
4 <Directory "/vhost/www1">
5 Options none
6 AllowOverride none
7 AuthType basic
8 AuthName "please input you are name/passwwd"
9 AuthUserFile "/etc/httpd/conf/.htpasswd"
10 Require valid-user
11 </Directory>
12 ErrorLog "logs/www1_error_log"
13 CustomLog "logs/www1_access_log" combined
14 </VirtualHost>
進行測試:
[root@centos7 httpd]# httpd -t
Syntax OK
[root@centos7httpd]#systemctl reload httpd
基于組賬號用戶訪問認證授權控制:
組賬號文件中每行定義一個組;
使用htpasswd命令工具生成認證授權文件和授權用戶;
[root@centos7 httpd]# htpasswd -m /etc/httpd/conf/.htpasswd xiaofang
[root@centos7 httpd]# htpasswd -m /etc/httpd/conf/.htpasswd xiaoming
編寫用戶訪問認證授權控制組賬號文件:
[root@centos7 httpd]# vim /etc/httpd/conf/.htgroup
1 admins:xiaoming xiaofang
對虛擬主機ServerName www2.magedu.com;做組賬號授權訪問控制
16 <VirtualHost *:80>
17 ServerName www2.magedu.com
18 DocumentRoot "/vhost/www2"
19 <Directory "/vhost/www2">
20 Options none
21 AllowOverride none
22 AuthType basic
23 AuthName "please input you are name/passwwd"
24 AuthUserFile "/etc/httpd/conf/.htpasswd"
25 AuthGroupFile "/etc/httpd/conf/.htgroup"
26 Require group admins
27 </Directory>
28 ErrorLog "logs/www2_error_log"
29 CustomLog "logs/www2_accees_log" combined
30 </VirtualHost>
進行測試:
[root@centos7 httpd]# httpd -t
Syntax OK
[root@centos7httpd]#systemctl reload httpd
實驗:
示例:
拒絕IP:10.1.253.56訪問www1.magedu.com:
1 <VirtualHost *:80>
2 ServerName www1.magedu.com
3 DocumentRoot "/vhost/www1"
4 <Directory "/vhost/www1">
5 Options none
6 AllowOverride none
7 AuthType basic
8 AuthName "please input you are name/passwwd"
9 AuthUserFile "/etc/httpd/conf/.htpasswd"
10 Require valid-user
11 <RequireAll>
12 Require not ip 10.1.253.56
13 Require all granted
14 </RequireAll>
15 </Directory>
16 ErrorLog "logs/www1_error_log"
17 CustomLog "logs/www1_access_log" combined
18 </VirtualHost>
curl命令
curl是基于URL語法在命令行方式下工作的文件傳輸工具,它支持FTP, FTPS, HTTP, HTTPS, GOPHER, TELNET, DICT, FILE及LDAP等協議。curl支持HTTPS認證,并且支持HTTP的POST、PUT等方法, FTP上傳, kerberos認證,HTTP上傳,代理服務器, cookies, 用戶名/密碼認證, 下載文件斷點續傳,上載文件斷點續傳, http代理服務器管道( proxy tunneling), 甚至它還支持IPv6, socks5代理服務器,,通過http代理服務器上傳文件到FTP服務器等等,功能十分強大。
curl [options] [URL…]
curl的常用選項:
-A/–user-agent <string> 設置用戶代理發送給服務器
–basic 使用HTTP基本認證
-e/–referer <URL> 來源網址
–cacert <file> CA證書 (SSL)
–compressed 要求返回是壓縮的格式
-H/–header <line>自定義首部信息傳遞給服務器
-I/–head 只顯示響應報文首部信息
–limit-rate <rate> 設置傳輸速度
-u/–user <user[:password]>設置服務器的用戶和密碼
-0/–http1.0 使用HTTP 1.0
-X, –request <command>:自定義請求方法
elinks命令:
elinks [OPTION]… [URL]…
-dump: 不進入交互式模式,而直接將URL的內容輸出至標準輸出;
使用mod_deflate模塊頁面壓縮優化傳輸速度;
適用場景:
(1) 節約帶寬,額外消耗CPU;同時,可能有些較老瀏覽器不支持;
(2) 壓縮適于壓縮的資源,例如文件文件;
SetOutputFilter DEFLATE
# mod_deflate configuration
# Restrict compression to these MIME types
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE text/css
# Level of compression (Highest 9 – Lowest 1)
DeflateCompressionLevel 9
# Netscape 4.x has some problems.
BrowserMatch ^Mozilla/4 gzip-only-text/html
# Netscape 4.06-4.08 have some more problems
BrowserMatch ^Mozilla/4\.0[678] no-gzip
# MSIE masquerades as Netscape, but it is fine
BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html
測試實驗:
示例
使用mod_deflate模塊頁面壓縮,優化傳輸速度,節省帶寬。
1)找個大點的文本文件copy到/var/www/html/text.txt文件中。
[root@centos7 log]# cp -a /var/log/messages /var/www/html/text.tx
[root@centos7 log]# ll /var/www/html/text.txt
-rw——- 1 root root 76765 oct 13 10:20 /var/www/html/text.txt
[root@centos7 log]#
2)由于/var/www/html/text.txt文件是600權限,所以要給普通用戶加讀權限。
[root@centos7 log]# chmod +r /var/www/html/text.txt
[root@centos7 log]# ll /var/www/html/text.txt
-rw-r–r– 1 root root 76765 oct 13 10:20 /var/www/html/text.txt
[root@centos7 log]#
3)使用curl -I請求http報文首部信息;
[root@centos7 html]# curl -I http://192.168.3.11/text.txt
HTTP/1.1 200 OK
Date: Thu, 13 Oct 2016 14:49:39 GMT
Server: Apache/2.4.6 (CentOS)
Last-Modified: Thu, 13 Oct 2016 14:20:01 GMT
ETag: "1bba90-53ebfce1bf0b9"
Accept-Ranges: bytes
Content-Length: 76765 //內容長度為:76765
Content-Type: text/plain; charset=UTF-8
[root@centos7 html]#
4)這時我們啟用頁面壓縮mod_deflate模塊功能;
[root@centos7 html]# httpd -M | grep "deflate"
deflate_module (shared)
[root@centos7 html]#
[root@centos7 html]# vim /etc/httpd/conf.d/deflate.conf
[root@centos7 html]# systemctl reload httpd
5)再次進行curl 命令并且使用compressed壓縮選項測試;
[root@centos7 html]# curl –compressed -I http://192.168.3.11/text.txt
HTTP/1.1 200 OK
Date: Thu, 13 Oct 2016 15:29:15 GMT
Server: Apache/2.4.6 (CentOS)
Last-Modified: Thu, 13 Oct 2016 15:23:14 GMT
ETag: "12bdd-53ec0b02e05ae-gzip"
Accept-Ranges: bytes
Vary: Accept-Encoding
Content-Encoding: gzip
Content-Length: 7056
Content-Type: text/plain; charset=UTF-8
[root@centos7 html]#
配置httpd支持https:
OpenSSL:
libcrpyto, libssl (ssl/tls), openssl
PKI:
CA,
SSL會話的簡化過程
(1) 客戶端發送可供選擇的加密方式,并向服務器請求證書;
(2) 服務器端發送證書以及選定的加密方式給客戶端;
(3) 客戶端取得證書并進行證書驗正:
如果信任給其發證書的CA:
(a) 驗正證書來源的合法性;用CA的公鑰解密證書上數字簽名;
(b) 驗正證書的內容的合法性:完整性驗正
(c) 檢查證書的有效期限;
(d) 檢查證書是否被吊銷;
(e) 證書中擁有者的名字,與訪問的目標主機要一致;
(4) 客戶端生成臨時會話密鑰(對稱密鑰),并使用服務器端的公鑰加密此數據發送給服務器,完成密鑰交換;
(5) 服務用此密鑰加密用戶請求的資源,響應給客戶端;
注意:SSL會話是基于IP地址創建;所以單IP的主機上,僅可以使用一個https虛擬主機;
配置httpd支持https:
(1) 為服務器申請數字證書;
測試:通過私建CA發證書
(a) 創建私有CA
(b) 在服務器創建證書簽署請求
(c) CA簽證
(2) 配置httpd支持使用ssl,及使用的證書;
# yum -y install mod_ssl
配置文件:/etc/httpd/conf.d/ssl.conf
DocumentRoot
ServerName
SSLCertificateFile
SSLCertificateKeyFile
(3) 測試基于https訪問相應的主機;
# openssl s_client [-connect host:port] [-cert filename] [-CApath directory] [-CAfile filename]
實驗測試:
搭建httpd支持https:
*根據openssl的配置文件:/etc/pki/tls/openssl.cnf中定義的文件路徑來創建所需文件。
(1)創建所需的文件:
[root@centos7 ~]# touch /etc/pki/CA/index.txt //創建CA數據庫文件
[root@centos7 ~]# echo "01" > /etc/pki/CA/serial //創建CA數據庫索引編號文件
[root@centos7 ~]# cat /etc/pki/CA/serial
01
[root@centos7 ~]#
(2)CA自簽證書:
1)生成私鑰;
[root@centos7 ~]# (umask 066;openssl genrsa -out /etc/pki/CA/private/cakey.pem 2048)
Generating RSA private key, 2048 bit long modulus
…………………………………………………………………………………………………………………………+++
……….+++
e is 65537 (0x10001)
[root@centos7 ~]#
2)生成自簽名證書;
1)[root@centos7 ~]# openssl req -new -x509 -key /etc/pki/CA/private/cakey.pem -days 7300 -out /etc/pki/CA/cacert.pem
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) []:beijing
Locality Name (eg, city) [Default City]:bj
Organization Name (eg, company) [Default Company Ltd]:magedu.com
Organizational Unit Name (eg, section) []:m20-1
Common Name (eg, your name or your server's hostname) []:centos7.1
Email Address []:admin@magedu.com
root@centos7 ~]#
3)給www.magedu.com主機頒發證書:
(1)首先在/etc/httpd/創建建一個certs目錄。以便存放網站證書文件;
[root@centos7 httpd]# mkdir -p /etc/httpd/certs
(2)給www.magedu.com主機創建私鑰文件;
[root@centos7 httpd]# (umask 077;openssl genrsa -out /etc/httpd/certs/httpd.key 2048)
Generating RSA private key, 2048 bit long modulus
……+++
……………….+++
e is 65537 (0x10001)
[root@centos7 httpd]#
(3)生成證書申請文件:
[root@centos6 Desktop]# openssl req -new -key /etc/httpd/certs/httpd.key -days 365 -out /etc/httpd/certs/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) []:beijing
Locality Name (eg, city) [Default City]:bj
Organization Name (eg, company) [Default Company Ltd]:magedu.com
Organizational Unit Name (eg, section) []:m20-1
Common Name (eg, your name or your server's hostname) []:www.magedu.com
Email Address []:root@magedu.com
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
[root@centos6 Desktop]#
(4)CA簽署證書,并將證書頒發給請求者;
[root@centos7 CA]# openssl ca -in /etc/httpd/certs/httpd.csr -out/etc/httpd/certs/httpd.crt
-days 365
查看:
[root@centos7 certs]# cd /etc/httpd/certs/
[root@centos7 certs]# ls
httpd.crt httpd.csr httpd.key
[root@centos7 certs]#
(5)安裝mod_ssl模塊:
配置httpd支持使用ssl,及使用的證書;
# yum -y install mod_ssl
配置文件:/etc/httpd/conf.d/ssl.conf
DocumentRoot
ServerName
SSLCertificateFile
SSLCertificateKeyFile
示例:
重啟httpd服務:
systemctl restart httpd
在/etc/hosts文件中添加IP與域名的對應關系:
[root@centos7 conf.d]# vim /etc/hosts
10.1.253.56 www.magedu.com
192.168.3.11 www.magedu.com
我們也可以使用IE瀏覽器進行測試:
我們需要編輯windows的hosts文件
Windows–>system32—>drivers—–>etc—->hosts;添加IP對應的域名。
我們把根CA的證書導到IE瀏覽器的“受信任的根證書頒發機構”中。
工具—->internet選項—–>內容——->證書——->受信任的根證書頒發機構—導入
使用IE瀏覽器進行測試:
httpd自帶的應用程序:
htpasswd:basic認證基于文件實現,用于生成賬號和密碼的程序;
htdbm
htdigest
apachectl:httpd自帶的服務控制腳本,支持start和stop等子命令;
apxs:– APache eXtenSion tool
apxs:由httpd-devel程序包提供;
為httpd增添模塊的;
rotatelogs:
access_log,
access_log, access_log.1, …
ab – web service的壓力測試工具
ab [OPTIONS] [http[s]://]hostname[:port]/path
請求數:[ -n requests ]
并發數:[ -c concurrency ]
長連接:[ -k ] 執行倍數請求,特別快;
示例:
ab -n 1000 -c 100 https://www.magedu.com/index.html
ab -n 1000 -c 100 http://192.168.3.11/index.html
httpd-2.2與httpd-2.4的不同之處:
httpd-2.4的MPM模塊為shared模塊;
MPM:多路處理模塊;
prefork:進程模型,兩級結構,master/worker, 每worker處理一個請求;
worker:線程模型,三級結構,master/worker/thread,每thread處理一個請求;
event:事件驅動的線程模型,兩級結構,master/worker,每worker響應多個請求;
httpd-2.2的MPM模塊為static模塊,而非shared模塊;
要更改centos6上的MPM模塊需編輯/etc/sysconfig/httpd配置文件:
[root@centos6 ~]# vim /etc/sysconfig/httpd
HTTPD=/usr/sbin/{httpd|httpd.worker|httpd.event}
注意:centos6 httpd-2.2 httpd.event不可用 httpd.worker不可取。
<IfModule prefork.c>
StartServers 8
MinSpareServers 5
MaxSpareServers 20
ServerLimit 256
MaxClients 256
MaxRequestsPerChild 4000
</IfModule>
<IfModule worker.c>
StartServers 4
MaxClients 300
MinSpareThreads 25
MaxSpareThreads 75
ThreadsPerChild 25
MaxRequestsPerChild 0
</IfModule>
基于IP的訪問控制機制:
httpd-2.4:
require ip, require not ip, require host, require not host
httpd-2.2:
allow from, deny from
order allow,deny, order deny,allow
注意:order allow,deny 第二項表示是默認的。
示例:httpd-2.2拒絕ip192.168.3.2訪問網站:
基于主機名的虛擬主機:
httpd-2.2:須使用NameVirtualHost;
httpd-2.4:無須使用;
各映射的本地文件系統路徑內的資源:
httpd-2.4:須做顯式授權
httpd-2.2:無須顯式授權
示例:
在httpd-2.2上做的基于主機名的虛擬主機
注意:在httpd-2.2上做基于域名的虛擬主機時需指定NameVirtualHost *:80
示例:
在httpd-2.2上做的基于ip的虛擬主機:
示例:
在httpd-2.2上做的基于端口的虛擬主機:
編輯/etc/httpd/conf/httpd.conf主配置文件監聽8080端口
資源類型:
靜態資源:原始形式與響應給客戶端的結果一致;
動態資源:原始形式通常為程序文件(為某種編程語言開發),需要運行后將生成的結果展示給客戶端;
客戶端技術:javascript
服務端技術:php, jsp, …
CGI:Common Gateway Interface(通用網關接口協議)
CGI是一種協議,定義了客戶端(web服務器程序)與服務端(特定的應用程序服務進程)進行數據交換的一種規范;
php:編程語言,嵌入式編程語言,高度模塊化(extensions),配置文件(/etc/php.ini, /etc/php.d/*.ini);
<html>
…
<?php
phpinfo();
?>
…
</html>
httpd+php:
CGI
Module
prefork:libphp
worker, event:libphp-zts
示例:安裝php
yum -y install php
vim /var/www/html/php.php
原創文章,作者:zhengyibo,如若轉載,請注明出處:http://www.www58058.com/59598