http簡介
httpd是Apache超文本傳輸協議(HTTP)服務的主程序。被設計爲一個獨立運行的後臺進程它會建立一個處理請求的子進程或線程的池
一次完整的http請求的處理過程
(1) 建立或處理連接
(2) 接收請求
(3) 處理請求
(4) 訪問資源
(5) 構建響應報文
(6) 發送響應報文
(7) 記錄於日誌中
http協議請求方法
GET 請求指定的頁面信息並返回資源主體
HEAD 類似於GET請求只不過返回的響應中沒有具體的內容用於獲取HTTP首部
POST 向指定服務器提交表單數據進行處理請求。請求報文主體種包含了要傳遞的數據信息內容。POST請求可能會導致新的資源的建立和/或已有資源的修改。
PUT 從客戶端向服務器上傳可取代指定的文檔的內容的數據資源。
DELETE 請求服務器刪除指定的資源。
CONNECT HTTP/1.1協議中預留給能夠將連接改爲管道方式的代理服務器。
OPTIONS 允許客戶端查看服務器的性能。
http協議響應
HTTP響應也是由三個部分組成分別是狀態行、消息報頭、響應正文
1、狀態行格式如下
HTTP-Version Status-Code Reason-Phrase
其中HTTP-Version表示服務器HTTP協議的版本Status-Code表示服務器發回的響應狀態代碼Reason-Phrase表示狀態代碼的文本描述。
狀態代碼有三位數字組成第一個數字定義了響應的類別且有五種可能取值
1xx指示信息--表示請求已接收繼續處理
2xx成功--表示請求已被成功接收、理解、接受
3xx重定向--要完成請求必須進行更進一步的操作
4xx客戶端錯誤--請求有語法錯誤或請求無法實現
5xx服務器端錯誤--服務器未能實現合法的請求
常見狀態代碼、狀態描述、說明
200 OK //客戶端請求成功
400 Bad Request //客戶端請求有語法錯誤不能被服務器所理解
401 Unauthorized //請求未經授權這個狀態代碼必須和WWW-Authenticate報頭域一起使用
403 Forbidden //服務器收到請求但是拒絕提供服務
404 Not Found //請求資源不存在eg輸入了錯誤的URL
500 Internal Server Error //服務器發生不可預期的錯誤
503 Server Unavailable //服務器當前不能處理客戶端的請求一段時間後可能恢復正常
httpd特點
httpd功能特性
CGI (Common Gateway Interface)
虛擬主機
基於IP
基於PORT
基於HOSTNAME
反向代理
負載均衡
路徑別名
豐富的用戶認證機制
basic
digest
支持第三方模塊
httpd-2.2虛擬主機配置
CentOS 6默認提供的httpd-2.2版本
Httpd安裝
yum –y install httpd
查看httpd安裝與否
[root@www ~]# rpm -qa | grep httpd httpd-2.2.15-54.el6.centos.x86_64 httpd-tools-2.2.15-54.el6.centos.x86_64
查看httpd安裝路徑
[root@www conf.d]# rpm -ql httpd | less /etc/httpd /etc/httpd/conf //httpd的配置文件路徑 /etc/httpd/conf.d //httpd的配置文件路徑 /etc/httpd/conf.d/README /etc/httpd/conf.d/welcome.conf /etc/httpd/conf/httpd.conf /etc/httpd/conf/magic /etc/httpd/logs //httpd的日誌文件路徑 /etc/httpd/modules //httpd的模塊路徑 /etc/httpd/run /etc/logrotate.d/httpd /etc/rc.d/init.d/htcacheclean /etc/rc.d/init.d/httpd //httpd的服務文件 /etc/sysconfig/htcacheclean /etc/sysconfig/httpd /usr/lib64/httpd /usr/lib64/httpd/modules /usr/lib64/httpd/modules/mod_actions.so /usr/lib64/httpd/modules/mod_alias.so
配置虛擬主機
[root@www conf]# grep"^NameVirtualHost" /etc/httpd/conf/httpd.conf NameVirtualHost 192.168.99.61:80
配置httpd的basic認證添加httpd認證用戶
htpasswd -c -m/etc/httpd/conf/.htpasswd tom htpasswd -m/etc/httpd/conf/.htpasswd jerry htpasswd -m/etc/httpd/conf/.htpasswd mary
也可以配置httpd的basic基於組的認證
vim /etc/httpd/conf/.htgrp mygrps: user1 user2
虛擬主機配置
vim/etc/httpd/conf.d/vhost1.conf <VirtualHost 192.168.99.61:80> DocumentRoot/data/vhosts/www1/wordpress ServerNamewww1.runner.vip ErrorLoglogs/www1.runner.vip-error_log CustomLoglogs/www1.runner.vip-access_log common <Location/server-status> SetHandlerserver-status AuthtypeBasic AuthName"For Administrators" AuthUserFile "/etc/httpd/conf/.htpasswd" #AuthGroupFile "/etc/httpd/conf/.htgrp" #Requiregroup mygrps Requireuser tom user2 </Location> </VirtualHost>
開啓httpds支持,需要安裝mod_ssl模塊
查看是否安裝ssl
httpd -M | grep ssl yum –y install mod_ssl [root@www conf]# httpd -M | grep ssl ssl_module (shared)
httpds 配置
[root@www ssl]# (umask 077; openssl genrsa -out httpd.key 2048) Generating RSA private key, 2048 bit long modulus ...............+++ ..........+++
生成證書籤署請求
CA服務器簽署證書
[root@ca CA]# openssl ca -in /tmp/httpd.csr-out /etc/pki/CA/certs/httpd.crt -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: Jul 21 04:15:40 2016 GMT Not After : Jul 21 04:15:40 2017 GMT Subject: countryName = CN stateOrProvinceName =Beijing organizationName = Runner organizationalUnitName = Ops commonName = myadmin.runner.vip emailAddress [email protected] X509v3 extensions: X509v3 Basic Constraints: CA:FALSE Netscape Comment: OpenSSL Generated Certificate X509v3 Subject Key Identifier: 3D:63:98:25:9D:9E:6E:AD:87:58:4E:6B:82:AE:90:AE:DE:77:18:B7 X509v3 Authority Key Identifier: keyid:9B:65:F4:88:E7:A7:10:0F:82:7C:1A:AB:40:83:E3:5A:AE:CA:72:0E Certificate is to be certified until Jul 2104:15:40 2017 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
虛擬主機2
/etc/httpd/conf.d/ssl.conf 主要配置 LoadModule ssl_module modules/mod_ssl.so Listen 443 <VirtualHost _default_:443> DocumentRoot"/data/vhosts/myadmin" ServerName myadmin.runner.vip:443 ErrorLog logs/ssl_error_log TransferLog logs/ssl_access_log <Directory"/data/vhosts/myadmin"> Options None AllowOverride None ###禁止192.168.99.51訪問第一種做法 #Order deny,allow #Deny from 192.168.99.51 ###禁止192.168.99.51訪問第二種做法 Order allow,deny Allow from all Deny from 192.168.99.51 </Directory> SSLCertificateFile /etc/httpd/ssl/httpd.crt SSLCertificateKeyFile /etc/httpd/ssl/httpd.key
訪問myadmin.runner.vip測試