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)
- 虚拟主机
- 反向代理
- 负载均衡
- 路径别名
- 丰富的用户认证机制
- 支持第三方模块
httpd-2.2虚拟主机配置
CentOS 6默认提供的httpd-2.2版本
Httpd安装
查看httpd安装与否
1
2
3
| [iyunv@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安装路径
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
| [iyunv@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
|
配置虚拟主机
1
2
| [iyunv@www conf]# grep"^NameVirtualHost" /etc/httpd/conf/httpd.conf
NameVirtualHost 192.168.99.61:80
|
配置httpd的basic认证添加httpd认证用户
1
2
3
| htpasswd -c -m/etc/httpd/conf/.htpasswd tom
htpasswd -m/etc/httpd/conf/.htpasswd jerry
htpasswd -m/etc/httpd/conf/.htpasswd mary
|
也可以配置httpd的basic基于组的认证
1
2
| vim /etc/httpd/conf/.htgrp
mygrps: user1 user2
|
虚拟主机配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| 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
1
2
3
4
| httpd -M | grep ssl
yum –y install mod_ssl
[iyunv@www conf]# httpd -M | grep ssl
ssl_module (shared)
|
httpds 配置1
2
3
4
| [iyunv@www ssl]# (umask 077; openssl genrsa -out httpd.key 2048)
Generating RSA private key, 2048 bit long modulus
...............+++
..........+++
|
生成证书签署请求
CA服务器签署证书
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
| [iyunv@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 =myadmin@runner.vip
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
| /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测试
|