Apache-linux
http 的默认端口:80/tcphttps 的默认端口:443/tcp
http协议的版本:
http/0.9
http/1.0
http/1.1
http/2.0
socket:BSD是 socket ipc的一种实现,允许在不同的主机上的进程之间相互通信的解决方式
基于套接字有三中通信:
tcp套接字
udp套接字
裸套接字
套件字的使用格式
ipv4
ipv6
unix sock
工作的模式:
一次完整http事务:请求 -- 响应
URL 资源的标记 用于表示服务器上的资源的位置
资源的种类:
静态资源:txt、jpg ...
动态资源:
服务器端:php jsp...
客户端:.js
http请求的过程
1、建立或处理连接接收或拒绝请求
2、接收请求接收客户端发来的请求报文
3、处理请求对请求报文进行解析
4、访问资源通过存储IO获取用户请求的资源
5、构建响应报文
6、发送响应报文
7、记录于日志中
http的并发响应模型:
单进程IO模型 串行响应
多进程IO模型 启动多个进程 每个进程响应一个请求
复用Io模型 一个进程生成多个线程 每个线程响应一个请求
http请求处理的连接方式
保持连接:长连接 keepalive
非保持连接:短连接
httpd的特性:
高度模块化的设计 核心(core) + modules
modules 支持动态装载和卸载
支持MPM
MPM:multipath processing modules多路处理的模块
prefork:一个主进程,多个子进程,一个进程响应一个请求
主进程:管理子进程,创建套接字 接收用户请求,并派发给某子进程处理...
子进程:处理、响应请求
worker:多进程、多线程模型 一个线程响应一个请求;
主进程:管理子进程,创建套接字,接收用户请求,并派发给某子进程处理...
子进程:负责管理线程
线程:处理并响应请求
event:事件驱动模型,多进程模型,每个进程响应多个请求;
主进程:管理子进程,创建套接字,接收用户请求,并派发给某子进程处理...
子进程:处理并响应请求;
httpd-2.2 event为测试模式
centos 6
httpd-2.4 可以在企业中正常使用
centos 7
httpd的版本:
httpd-1.3 官方已经停止维护
httpd-2.0
httpd-2.2
httpd-2.4
官方网站:httpd.apache.org
安装httpd:
rpm包、yum base源安装:
编译安装:定制新功能、其他原因
centos 6 httpd-2.2
sysinit脚本 /etc/rc.d/sysinit.d/httpd
配置文件:
/etc/httpd/conf/httpd.conf
/etc/httpd/conf.d/*.conf
程序文件:
/usr/sbin/httpd
/usr/sbin/httpd.event
/usr/sbin/httpd.worker
脚本配置文件:/etc/sysconfig/httpd 日志文件:/var/log/httpdaccess_log 访问日志error_log 错误日志
centos 7 httpd-2.4
systemd Unit file/usr/lib/systemd/system/httpd.service
注意在做测试之间关闭iptables -F 防火墙
centos 6 httpd安装:
yum -y install httpd
service httpd restart启动httpd 服务
chkconfig httpd on| off 开机自动启动
ss -tnl 可以查看地址的80 端口是否开启
LISTEN 0 128 :::80
可以用浏览器访问一下本机的ip地址
会进入一个欢迎使用apache的页面 说明没有问题
这个欢迎页面的程序在 /etc/httpd/conf.d下的welcome.conf 这个文件
当把它改个名字 mv welcome.conf welcome.conf.bak
在重复一下服务器 service httpd> 在测试就没了不过它会到/var/www/html/下去找文件
这是修改玩的测试页面:
Index of /
Name Last modified>
Apache/2.2.15 (CentOS) Server at 172.20.10.4 Port 80
到/var/www/html/ 随便编辑一些东西吧
vim /var/www/html/chenedu.html
hello chen
测试页面冲刷一下
http://172.20.10.4/chenedu.html
hello chen
centos 7 httpd安装:
yum -y install
程序环境:
配置文件:
/etc/httpd/conf/httpd.conf
/etc/httpd/conf.modules.d/.conf
/etc/httpd/conf.d/.conf
程序文件:
/usr/sbin/httpd
MPM支持DSO机制,所以各为一个独立的模块;
日志文件:
/var/log/httpd
access_log:访问日志
error_log:错误日志
站点文档根目录:
/var/www/html
模块文件路径:
/usr/lib64/httpd/modules
systemctl restart httpd 启动httpd服务
systemctlenable httpd 开机自动启动
ss -tnl 查看80 端口
LISTEN 0 128 :::80
httpd—2.2的配置 centos 6
主配置文件: /etc/httpd/conf/httpd.conf
注意在修改配置文件之间先备份一下
cp -r httpd.conf httpd.conf.bak
在 /etc/httpd/conf/httpd.conf文件下的三种配置段
Section 全局配置
Section 1: Global Environment全局的配置段
Section 2: 'Main' server configuration 中心服务器配置段
Section 3: Virtual Hosts 虚拟主机配置段
在修改httpd 的配置文件时
httpd -t测试语法是否有问题
service httpd> /etc/httpd/conf/httpd.conf 主文件配置的内容和修改定义:
1、修改的监听的端口
Listen 80
Listen 8080 可以自己定义
2、持久连接: 说白了就是测试
tcp连接建立后,资源获取完成不会断开连接,会继续等待请求资源
KeepAlive On|Off 如何断开
MaxKeepAliveRequests 100 数量限制
KeepAliveTimeout 15 时间限制
用telnet请求测试:
# telnet 192.168.31.176 80
Trying 192.168.31.176...
Connected to 192.168.31.176.
Escape character is '^]'.
GET /chenedu.html HTTP/1.1GET:是请求方法/chenedu.html:是资源HTTP/1.1:协议的版本
Host:192.168.31.176 默认请求的地址
HTTP/1.1 200 OK
Date: Tue, 10 Jan 2017 15:24:06 GMT
Server: Apache/2.2.15 (CentOS)
Last-Modified: Mon, 30 May 2016 02:23:34 GMT
ETag: "3a0243-b-53405f178500e"
Accept-Ranges: bytes
Content-Length: 11
Connection: close
Content-Type: text/html;
hello chen
3、MPM 多路处理模块
httpd-2.2的mpm不支持DSO机制 event为测试
在centos6 上想使用其他的模块的话 /etc/sysconfig/httpd 下
#HTTPD=/usr/sbin/httpd.worker| httpd.event 自己定义
/etc/httpd/conf/httpd.conf模块
prefork MPM 预先创建
StartServers 8 预先创建的空闲子进程
MinSpareServers 5 最小空闲进程
MaxSpareServers 20 最大空闲进程
ServerLimit 256 响应用户进程的最大数量
MaxClients 256 服务器进程可以开始的最大数量
MaxRequestsPerChild4000 限制最大的响应数量
worker MPM
StartServers 4 初始启动的进程数量 (子进程)
MaxClients 300 最大连接并发数量 (线程)
MinSpareThreads 25 最少空闲线程数量
MaxSpareThreads 75 最大空闲线程数
ThreadsPerChild 25 每个子进程启动的线程
MaxRequestsPerChild0 限制最大的响应数量 0:表示不做限制
4、DOS机制: 动态模块装载
LoadModule foo_module modules/mod_foo.sofoo_modile 是文件
5、Main server主服务器
如何定义一个主机:
有两种方法:
1、编辑/etc/hosts下添加:
192.168.148.122 www.chenedu.com 我的本机ip地址 名称可自己定义
2、直接编辑主配置文件/etc/httpd/conf/httpd.conf 添加:
ServerName www.cheneduo.com
6、站点资源访问的路径: 和上面5 一样的
vim /var/www/html/index.html
index.html chenedu.com
可用用浏览器访问自己的ip地址 即可
也可以自己创建一个目录路径: 在更改一下DocumentRoot 的资源访问路径 路径是你创建的目录路径;
目录中的常用指令:
(1) Options:用于定义资源的展示方式;后跟以空白字符分隔的“选项”列表;
Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews None All
Indexes:允许索引;
FollowSymLinks:允许跟踪符号链接;
SymLinksifOwnerMatch:允许
ExecCGI:允许执行CGI脚本;
(2) AllowOverride None 一般不启动
(3) order和allow/deny from 设置访问允许、限制网段
IP地址的访问控制;
示列:
Allow from 192.168.148.121 仅允许192.168.148.121 的主机访问
order用于定义allow和deny的生效次序;
allow from IP/NETWORK/FQDN
deny from IP/NETWORK/FQDN网络 域名
请求遵循最佳匹配法则机制
orderallow deny
7、定义站点主页面
DirectoryIndex index.html index.html.var从左边开始搜索
8、定义路径别名
alias
9、日志
错误日志:ErrorLog
ErrorLog logs/error_log /var/log/httpd/error_log
访问日志:
LogFormat:定义日志信息的格式
CustomLog:指明日志文件路径和日志格式
格式:
%h Remote host 远程主机
%l Remote logname (from> %u Remote user (from auth; may be bogus if return status (%s) is 401) 用户名
%t Time the request was received (standard english format) 服务器收到的请求时的时间
%r First line of request 请求报文的首行起始行
%s Status. For requests that got internally redirected, this is the status of the original request --- %>s for the last. 记录服务器在收到请求之后 根据客户端的请求反映的状态码
%b> %i The contents of Foobar: header line(s) in the request sent to the server 请求报文的某个特定守护的值
10、用户的访问控制:
http协议的认证
认证质询:
WWW-Authenticate:响应码为401,拒绝客户端请求,说明要求客户端提供账号和密码
认证:
Authorization:客户端填入账号和密码后再次发送请求报文;认证通过后,服务端发送响应资源
虚拟的帐号:访问服务的帐号和密码
LoadModule
认证的方式:
basic 明文
digest 消息摘要认证
vim /var/www/html/admin/admin.html 创建/admin/admin.html admin root
主页面是:/var/www/html/index.html
index.html chenedu.com
下面basic认证 当访问admin下的admin.html
编辑/etc/httpd/conf/httpd.conf 下创建一个安全域
Directory /var/www/html/admin网站服务访问的路径:
Options None
AllowOverride None
AuthType basic 认证的类型
AuthName "For Administrators"
Authuserfile "/etc/httpd/conf/.httpdpasswd" 用户的密码、账户的文件
Require valid-user 允许那些人登录
创建帐号文件: htpasswd -c -m /etc/httpd/conf/.httpdpasswd chen 创建chen 用户
New password:
Re-type new password:
service httpd>
现在可以用浏览器 测试了
要是组的帐号进行认证的话该怎么做呢?
一、先创建一个安全域
vim /etc/httpd/conf/httpd.conf
Options None
AllowOverride None
AuthType basic
AuthName "For Administrators"
Authuserfile "/etc/httpd/conf/.httpdpasswd"
Authgroupfile "/etc/httpd/conf/.httpdgroup" 创建组的用户、密码等 文件 可自己定义
Require group groups 允许组中的用户访问
二、创建组的用户和目录 可自己定义
vim /etc/httpd/conf/.httpd.group
groups: user1 user2
htpasswd-m /etc/httpd/conf/.httpdpasswduser1
htpasswd-m /etc/httpd/conf/.httpdpasswduser2
service httpd> htpasswd
-c:创建文件;
-m:md5加密密码;
-s: SHA加密密码;
-D:删除指定用户
12、虚拟主机
站点标示:
ip相同,端口不同
ip不同,端口相同
FQDN不同
虚拟主机的三种实现方法:
ip的虚拟主机
每个虚拟主机使用独有的ip地址
创建一个基于ip地址的虚拟主机
一、先创建ip地址:
ip addr add 192.168.148.100/16 dev eth0
inet 192.168.148.122/24 brd 192.168.148.255 scope global eth0
inet 192.168.148.100/32 scope global eth0
二、创建虚拟主机
可以参考/etc/httpd/conf/httpd.conf 最下面虚拟主机的配置
ServerAdmin webmaster@dummy-host.example.com
DocumentRoot /www/docs/dummy-host.example.com
ServerName dummy-host.example.com
ErrorLog logs/dummy-host.example.com-error_log
CustomLog logs/dummy-host.example.com-access_log common
#
手动创建二个基于ip的虚拟主机:
第一个ip 地址 192.168.148.122
vim /etc/httpd/conf.d/vhost1.conf 可以自己定义的
基于ip的地址
ServerName www1.chenedu.com 名称
DocumentRoot /var/www/html/www1 网络服务网页
第二个ip 地址 192.168.148.100
vim /etc/httpd/conf.d/vhost2.conf
ServerName www2.chenedu.com
DocumentRoot /var/www/html/www2
三、去创建网络测试网页
mkdir -pv /var/www/html/www{1,2}
mkdir: created directory /var/www/html/www1'mkdir: created directory/var/www/html/www2'
vim /var/www/html/www1/index.html
www1.chenedu.com
vim /var/www/html/www2/index.html
www2.chenedu.com
做完之后必须要检测一下:
# httpd -t
Syntax OK
service httpd> 下面可以用浏览器测试 :
端口(PORT)的虚拟主机:
每个虚拟主机使用一个独有的端口(PORT)
创建基于端口的虚拟主机:
第一个不用修改
vim /etc/httpd/conf.d/vhost2.conf
改成原来的ip地址 8080 端口可自己到/etc/httpd/conf/httpd.conf 定义端口 Listen 8080
ServerName www2.chenedu.com
DocumentRoot /var/www/html/www2
httpd -t 检测
service httpd> 域名(FQDN)的虚拟主机:
每个虚拟主机使用一个独有的域名(FQDN)
注意:虚拟主机和主服务器 不能同时使用
创建基于域名的虚拟主机:
现将/etc/httpd/conf/httpd.conf 下的NameVirtualHost *:80 打开 它默认是注释 的
NameVirtualHost 192.168.148.122:80 当前本机的ip地址
vim /etc/httpd/conf.d/vhost1.conf
VirtualHost 192.168.148.122:80> 基于ip的地址
ServerName www1.chenedu.com 名称
DocumentRoot /var/www/html/www1 网络服务网页
CustomLog logs/www1-access_log combined 访问的日志 在 /var/log/httpd
vim /etc/httpd/conf.d/vhost2.conf
ServerName www2.chenedu.com
DocumentRoot /var/www/html/www2
CustomLog logs/www2-access_log combined 访问的日志
vim /etc/hosts
192.168.148.122 www1.chenedu.com www2.chenedu.com其实可以在DNS下解析 就不用在这里解析了
测试:
# curl http://www1.chenedu.com
www1.chenedu.com
# curl http://www2.chenedu.com
www2.chenedu.com
# curl http://192.168.148.122
www1.chenedu.com
httpd:
url:资源统一定位符
服务器地址:端口+ip地址
基本语法格式:
params:参数
http://www.chenedu.com/admin/admin.php;useranme=admin
query:查询
http://www.magedu.com/admin/admin.php?logintime=10/12/2017
frag:
http://www.chenedu.com/admin/redme.html#ch1
httpd协议报文格式:
method:请求方法,客户端希望服务器对资源执行的动作
GET:从服务器获取一个资源
HEAD:仅从服务器获取资源响应的报文的首部
POST:向服务器提交表单 表单就是把表单中的数据发送给服务器
status :响应状态码
请求处理过程的结果状态
显示的结果提示:
1xx 100-101:信息提示
2xx 200-206:成功类的响应码 如200
3xx 300-305:重定向类的响应码如301, 302, 304...
4xx 400-415:错误类信息客户端错误如 401, 404, 403...
5xx 500-505:服务器端错误如500,502...
headers:请求响应报文的首部
首部分类:
通用首部:请求、响应报文都可以使用
请求首部
响应首部
实体首部
扩展首部
httpd-2.2 基本配置:
一、status 页面启用 status 页面
vim /etc/sysconfig/httpd
HTTPD=/usr/sbin/httpd.worker将它注释掉 默认是注释掉
service httpd restart 重启
vim /etc/httpd/conf/httpd.conf默认是注释掉的
SetHandler server-status 内部插件 启用
Order deny,allow
Deny from all
Allow from 192.168
httpd -t 检测语法
service httpd>
用浏览器 访问本机的ip地址
192.168.148.122/server-status 会显示一下信息
Apache Server Status for 192.168.148.122
Server Version: Apache/2.2.15 (Unix) DAV/2
Server Built: Jul 24 2015 11:52:28
Current Time: Friday, 13-Jan-2017 19:06:16 CST
Restart Time: Wednesday, 01-Jun-2016 22:45:02 CST
Parent Server Generation: 1
Server uptime: 225 days 20 hours 21 minutes 14 seconds
1 requests currently being processed, 7>
_W______........................................................
................................................................
................................................................
................................................................
Scoreboard Key:
"_" Waiting for Connection, "S" Starting up, "R" Reading Request,
"W" Sending Reply, "K" Keepalive (read), "D" DNS Lookup,
"C" Closing connection, "L" Logging, "G" Gracefully finishing,
"I">
PID Key:
5056 in state: _ , 5057 in state: W , 5058 in state: _
5059 in state: _ , 5060 in state: _ , 5061 in state: _
5062 in state: _ , 5063 in state: _ ,
To obtain a full report with current status information you need to use the ExtendedStatus On directive.
启动 /etc/httpd/conf/httpd.conf
ExtendedStatus On将注释# 去掉
service httpd>
重新刷新一下页面就多出更多的信息
二、user/group
指定用户的身份运行httpd服务进程;
User apache
Group apache
可以用
ps aux查看
三、mod_deflate模块压缩页面优化传输速度
适用场景:
1、节约带宽,额外消耗CPU;同时,可能有些较老浏览器不支持
2、压缩适于压缩的资源,例如文件文件
https:
SSL会话的简化过程
(1) 客户端发送可供选择的加密方式,并向服务器请求证书;
(2) 服务器端发送证书以及选定的加密方式给客户端;
(3) 客户端取得证书并进行证书验正:
如果信任给其发证书的CA:
(a) 验正证书来源的合法性;用CA的公钥解密证书上数字签名;
(b) 验正证书的内容的合法性:完整性验正
(c) 检查证书的有效期限;
(d) 检查证书是否被吊销;
(e) 证书中拥有者的名字,与访问的目标主机要一致;
(4) 客户端生成临时会话密钥(对称密钥),并使用服务器端的公钥加密此数据发送给服务器,完成密钥交换;
(5) 服务用此密钥加密用户请求的资源,响应给客户端;
注意:SSL会话是基于IP地址创建;所以单IP的主机上,仅可以使用一个https虚拟主机;
创建一个私有CA认证 在为服务端申请证书
192.168.148.121 当CAcentos 7
一、先创建一个私有CA
cd /etc/pki/CA/先切到这个目录下创建
# (umask 077; openssl genrsa -out private/cakey.pem 2048)
Generating RSA private key, 2048 bit long modulus
...............+++
.........+++
e is 65537 (0x10001)
在生成一个自签证书
# openssl req -new -x509 -key private/cakey.pem -out cacert.pem
Country Name (2 letter code) :CN 国家
State or Province Name (full name) []:Shanghai省的名字
Locality Name (eg, city) :Shanghai 地方
Organization Name (eg, company) :ChenEdu 组织名
Organizational Unit Name (eg, section) []:Ops 组织单位名称
Common Name (eg, your name or your server's hostname) []:ca.chenedu.com
Email Address []:ca@chenedu.com 邮箱
在多创建两个文件 自己定义
touch index.txt
echo 01 > serial
私有CA 已创建完成
二、为服务端申请证书:
192.168.148.122 centos 6
centos 6 上面默认是没有mod_ssl 模块
yum -y install mod_sll 安装mod_sll
下面先创建一个私钥文件
mkdir /etc/httpd/ssl
在ssl下创建私钥
# (umask 077; openssl genrsa -out httpd.key 1024)
Generating RSA private key, 1024 bit long modulus
...++++++
...++++++
e is 65537 (0x10001)
注意:httpd.key的权限 最好是600
在生成签署请求
# openssl req -new -key httpd.key -out httpd.csr
Country Name (2 letter code) :CN
State or Province Name (full name) []:Shanghai
Locality Name (eg, city) :Shanghai
Organization Name (eg, company) :ChenEdu
Organizational Unit Name (eg, section) []:Ops
Common Name (eg, your name or your server's hostname) []:www1.chenedu.com
Email Address []:www1@chenedu.com
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
# scp httpd.csr 192.168.148.121:/tmp将httpd.csr 复制到192.168.148.121 的tpm目录下 进行签署
在centos 7上:openssl ca -in /tmp/httpd.csr -out /etc/pki/CA/certs/httpd.crt 两个y 就签署成功了
在/etc/pki/CA/newcerts/ 会生成签署的文件名为 01.pem
再将文件复制回:scp certs/httpd.crt 192.168.148.122:/etc/httpd/ssl/ centos 6
vim /etc/httpd/conf.d/ssl.conf启用的配置文件
在下面的两行
DocumentRoot "/var/www/html/www1" 路径要用配置的虚拟主机上
ServerName www1.chenedu.com:443 虚拟主机的名称及端口
Server Certificate: 服务器证书文件
Point SSLCertificateFile at a PEM encoded certificate.If
the certificate is encrypted, then you will be prompted for a
pass phrase.Note that a kill -HUP will prompt again.A new
certificate can be generated using the genkey(1) command.
SSLCertificateFile /etc/httpd/ssl/httpd.crt 创建证书的摆放路径
Server Private Key: 服务器的私钥文件
If the key is not combined with the certificate, use this
directive to point at the key file.Keep in mind that if
you've both a RSA and a DSA private key you can configure
both in parallel (to also allow the use of DSA ciphers, etc.)
SSLCertificateKeyFile /etc/httpd/ssl/httpd.key 创建私钥的摆放路径
在改完之后 :httpd -t 检测语法
service httpd> ss -tnl 查看端口 443 是否启用
下面测试即可;
用ab工具对httpd进行压力测试:
ab
-n 总请求数
-c 模拟的并发数
-k 以持久连接模式 测试
上线之前做压测 线上是绝对不能压测的
httpd-2.4特性 centso 7
(1) MPM支持DSO机制,以模块方式加载;
(2) event MPM生产可用;
(3) 异步读写机制;
(4) 支持每模块及每目录单独日志级别定义;
(5) 每请求相关的专用配置;
(6) 增强版的表达式分析器;
(7) 毫秒级的持久连接时长;
(8) 基于FQDN的虚拟主机不再需要专门的指令NameVirtualHost;
(9) 支持用户自定义变量;
(10) 更低内在的开销;
新模块:
(1) mod_proxy_fcgi
(2) mod_proxy_scgi
(3) mod_remoteip
安装centos 7的httpd
yum -y install httpd
主配置文件:
/etc/httpd/conf/httpd.conf
模块配置文件:
/etc/httpd/conf.modules.d/.conf
分段配置文件:
/etc/httpd/conf.d/.conf
选择MPM 时用不同的模块时
在/etc/httpd/conf.modules.d/00-mpm.conf 下选择模块
2、基于ip地址的访问时:
和centos6 httpd-2.2 一样 不过不支持allow deny. . .
允许所有主机访问 Require all granted
拒绝所有主机访问 Require all denied
vim /vat/www/html/index.html 测试页面
Centos 7 httpd-2.4
如何拒绝物理主机的访问
vim /etc/httpd/conf/httpd.conf
Require all granted
Require not ip 192.168.148.122 拒绝 192.168.148.122 这个主机访问
httpd -t 检测
systemctl> 在192.168.148.122 centos 6 的主机上可以测试
# curl http://192.168.148.121/
403 Forbidden
Forbidden
You don't have permission to access /
on this server.
3、创建虚拟主机
FQDN的虚拟主机不再需要专门的指令NameVirtualHost;
vim /etc/httpd/conf.d/www1.conf 创建www1 的虚拟主机
ServerName www1.chenedu.com
DocumentRoot /var/www/html/www1
Options None
AllowOverride None
Require all granted
vim /etc/httpd/conf.d/www2.conf 创建www2 的虚拟主机
ServerName www2.chenedu.com
DocumentRoot /var/www/html/www2
Options None
AllowOverride None
Require all granted
mkdir -pv /var/www/html/www{1,2}
vim /var/www/html/www1/index.html 创建网站测试www1
www1.chenedu.com
vim /var/www/html/www1/index.html 创建网站测试www2
www2.chenedu.com
4、毫秒级长连接
KeepAlive
vim /etc/httpd/conf.d/keepalive 创建时间连接
KeepAlive On
KeepAliveTimeout 300ms 请求超时
MaxKeepAliveRequests 100 最大的请求数
httpd -t 检测
systemctl> 5、https
和httpd-2.2 一样一样的
页:
[1]