一.apache版本:
a patchy server =apache
httpd-2.2
httpd-2.4
httpd的特性:
1,模块化设计,DSO动态模块加载
2,MPM多个mem机制,多路访问模块
prefork:多进程模型。每个进程请求一个请求。一个主进程,负责生成N个子进程,每个子进程处理一个用户请求。用户没有请求,也会预先生成空进程,随时等待请求到达。最大不会超过1024。
worker:多线程模型。一个主进程生成多个子进程,每个子进程负责多个线程。M进程,M线程。M*N
event:事件驱动模型。每个线程响应N个请求。一个主进程生产多个子进程,每个进程直接N个请求。M*N
二.程序环境:
默认配置工作目录:/etc/httpd
配置文件:
主配置文件/etc/httpd/conf/httpd.conf
分段配置文件/etc/httpd/conf.d/*.conf
服务脚本:
/etc/rc.d/init.d/httpd
模块文件目录:
/etc/httpd/modules -> ../../usr/lib64/httpd/modules
主程序文件:
# rpm -ql httpd | grep bin
/usr/sbin/httpd (prefork模型)
/usr/sbin/httpd.worker(worker模型)
/usr/sbin/httpd.event(event模型)
日志文件目录:
# /var/log/httpd/
access_log error_log
站点文档目录:
/var/www/html
# ss -tnl 查看端口
#/etc/httpd/conf.dwelcome.conf 生成欢迎界面的
访问界面之后:查看日志
# tail /etc/httpd/logs/access_log
配置文件:
# grep "Section" httpd.conf
### Section 1: Global Environment 全局配置
### Section 2: 'Main' server configuration 中型主机
### Section 3: Virtual Hosts 虚拟主机
'Main'server和Virtual Hosts不能同时启用:默认启用的是'Main'server;
1,指定监听地址和端口
查找:/Listen
Listen 80
a,ip省略表示监听本机所有可用ip
b,listen可出现多次,指定不同端口
Listen 172.16.249.60:80
Listen 172.16.249.60:8080
2,持久连接相关
持久连接:连接建立后,每个资源获取结束不会断开连接,而继续等待其它资源请求并完成传输;
如何断开?
数量限制:如100个
时间限制:如60秒
劣势:对并发访问量较大的服务器,开持久连接会有些请求得不到服务;
改进:减短,httpd-2.4支持毫秒级
非持久连接:每个资源都是单独通过专用的连接进行获取
KeepAlive Off|On 是否开启持久连接
MaxKeepAliveRequests 100 最大持久连接时间
KeepAliveTimeout 15 连接超时时间
修改完成重新加载:#!ser重加载httpd
测试:
# telnet Server 80
GET /URL HTTP/1.1
Host: Server
实例:
# telnet 192.168.118.128 80
Trying 192.168.118.128…
Connected to 192.168.118.128.
Escape character is '^]'.
GET /index.html HTTP/1.1
Host:192.168.118.128
HTTP/1.1 200 OK
Date: Mon, 27 Apr 2015 23:09:02 GMT
Server: Apache/2.2.15 (CentOS)
Last-Modified: Mon, 27 Apr 2015 22:42:36 GMT
ETag: "407a8-59a-514bc7669ed60"
Accept-Ranges: bytes
Content-Length: 1434
Connection: close
Content-Type: text/html; charset=UTF-8
root:$6$QAHubWzJkFoJl8Ao$AM7SkY1AuOFrBaBKJZ5HMVdypcjmeC0J2DrcQJ4kUlYLVlD1oA9jPup
……
Connection closed by foreign host.
ps -aux |grep httpd| grep -v grep
3、MPM
多路处理模块:并发请求响应的不同实现
prefork, worker, event
httpd-2.2 不支持同时编译多个不同的MPM,rpm安装的httpd-2.2提供了三个文件分别用于实现提供对不同的MPM的支持;默认/etc/sysconfig/httpd . #HTTPD=/usr/sbin/httpd.worker
确认方法: # ps aux | grep httpd
默认为/usr/sbin/httpd,其为prefork;
查看模块列表:
httpd -l: 查看静态编译的主要模块
httpd -M: 查看所有模块,包括静态编译和DSO模块
httpd -t -D DUMP_MODULES 查看模块的状态
模块可以在主配置httpd.conf文件里面,手动装载和卸载.#
更换支持不同的MPM的主程序:
编辑/etc/sysconfig/httpd
启用变量: #HTTPD=/usr/sbin/httpd.worker
vi /etc/sysconfig/httpd
# prefork MPM
# StartServers: number of server processes to start
# MinSpareServers: minimum number of server processes which are kept spare
# MaxSpareServers: maximum number of server processes which are kept spare
# ServerLimit: maximum value for MaxClients for the lifetime of the server
# MaxClients: maximum number of server processes allowed to start
# MaxRequestsPerChild: maximum number of requests a server process serves
<IfModule prefork.c>
StartServers 8 服务器启动时候,默认启动8个空闲进程。
MinSpareServers 5 服务器最少空闲进程数
MaxSpareServers 20 最大空闲
ServerLimit 256 服务器最多响应请求
MaxClients 256 最大客户端请求
MaxRequestsPerChild 4000 每个子进程最多响应的请求
</IfModule>
# worker MPM
# StartServers: initial number of server processes to start
# MaxClients: maximum number of simultaneous client connections
# MinSpareThreads: minimum number of worker threads which are kept spare
# MaxSpareThreads: maximum numb er of worker threads which are kept spare
# ThreadsPerChil d: constant number of worker threads in each server process
# MaxRequestsPerChild: maximu m number of requests a server process serves
<IfModule worker.c>
StartServers 4 服务启动时,默认启动4个主进程
MaxClients 300 最大并发
MinSpareThreads 25 最少空闲
MaxSpareThreads 75 最大空闲
ThreadsPerChild 25 子进程生多少个线程
MaxRequestsPerChild 0 无限制
</IfModule>
300/25
4,Dso动态加载库模块
配置指令模块加载:
loadModule <module_name> <module_path>
模块后面的路径是相对于 ServerRoot
LoadModule auth_basic_module modules/mod_auth_basic.so
#httpd.worker -M |grep auth 查看模块
#service httpd reload !ser 重新加载
5,指定‘Main’server的文档页面路径
DocumentRoot 指定:
# mkdir /data/www/html -pv 创建一个目录
在html目录创建一个index.html文件,在主配置文件修改DocumentRoot "/data/www/html"
#httpd -t 语法检查
重加载之后测试。
6,站点路径访问控制:
访问机制两种:
基于来源地址:
基于帐号:
7,Directory 中访问控制定义
1)基于文件系统控制:
<Directory /data/www/html>
Options Indexes FollowSymLinks
AllowOverride None
</Directory>
列出网页的目录危险性极高,不建议开启. FollowSymLinks 如果某页面链接指向documentroot之外的其他文件。none,什么主机都不启用。
2)基于来源地址访问控制:
Order allow deny 只有明确allow的来源地址,才运行访问,其他的均为deny.
Order deny allow 只有明确deny的来源地址,才deny,其他均allow
Allow from:运行哪些。
deny from :拒绝哪些
order allow deny
配置的越精准优先生效。
curl htpp://www.xx.com/aa.index
8,定义默认主页面
DirectoryIndex index.html index.html.var
9,配置日志
错误日志:
ErrorLog logs/error_log
LogLevel warn
访问日志:
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
CustomLog logs/access_log combined
%h: Remote host,客户端主机
%l: Remote logname (from identd, if supplied). 客户用户通过identd登录时使用名称;一般为-;
%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,请求报文的首行<method> <url> <version>
%>s:响应状态状态码
%b: Size of response in bytes, excluding HTTP headers,响应报文的大小,单位为字节;不包含首部信息;
%{Referer}i:记录Http首部Referer对应的值;即访问入口,从哪个页面跳转至此页面;
%{User-Agent}i:记录http首部User-Agent对应的值;即浏览器类型;
详情: http://httpd.apache.org/docs/2.2/mod/mod_log_config.html#formats
10,路径别名
命令格式:Alias 别名 真实名
Alias /aa/ /vhosts/1.com/htdocs/aa/
注意,如果你在别名的未尾包含了“/”,那么在URL中也需要包含“/”。
http://192.168.118.129/aa/index.html 实则访问的主页是/aa/index.html
Alias!ok…
11,默认字符
AddDefaultCharset UTF-8
常用字符集:GBK, GB2312, GB18030
12,基于用户的访问控制
质询:服务器用401状态拒绝客户端请求,说明需要用户提供用户名和密码;弹出对话框;
认证:客户端用户填入账号密码后再次发请求至服务器;认证通过,则请求授权;
DocumentRoot "/var/www/html"
安全域,需要用户认证后方能访问。
http协议认证方式:
basic基本base64,digest摘要。
base64认证机制实现:
虚拟用户:非系统用户
实现方法:
1)在web的根目录创建一个目录it。新建文档..开始测试.
2)编辑配置文件# vi /etc/httpd/conf/httpd.conf 找一段手动添加
<Directory "/data/www/html/it">
Options None
AllowOverride None
AuthType Basic
AuthName "it info,only for it"
AuthUserFile /etc/httpd/users/.htpasswd
Require user t1
Require user t2
</Directory>
注意:valid-user 位于htpasswd中所有用户登录。 Require test1 test2 … 只允许列出的登录
3)提供用户帐号文件/users/.htpasswd
# htpasswd 命令维护此文件。
# htpasswd -c -m /users/.htpasswd test1
-c:创建添加第一个用户时候使用
-m:md5加密
-s :sha加密
-D:删除能过户
# more /users/.htpasswd
test1:$apr1$2a82MNsK$uXeRA/2bA4KSJmQze.ECp1
test2:$apr1$WvZs3czL$3p.asfh4MwTAQMeRRuYsv1
# httpd -t 检查语法没问题
!ser 重新加载
13,虚拟主机
一个物理器可以服务于多个站点,每个站点可通过一个或多个虚拟主机来实现;
httpd三种类型的虚拟主机:基于ip,基于port,基于FQDN
注意:得首先关闭'Main' server
方法:注释DocumentRoot指令即可;
mkdir -pv /vhosts/2{1.com,2.com}/htdocs/
分别在1.com和2.com的htdocs里面新建index.html写上1.com和2.com
!ser 重新加载服务。
基于ip: 注意:本地要配置上所有ip
# ifconfig eth0:0 192.168.118.130/24
<VirtualHost 192.168.118.129:80>
ServerName www.1.com
DocumentRoot /vhosts/1.com/htdocs/
</VirtualHost>
<VirtualHost 192.168.118.130:80>
ServerName www.2.com
DocumentRoot /vhosts/2.com/htdocs/
</VirtualHost>
[[email protected] ~]# curl www.1.com
1.com
[[email protected] ~]# curl www.2.com
2.com
基于port:httpd要在最上面添加监听端口
Listen 8080
<VirtualHost 192.168.118.129:80>
ServerName www.1.com
DocumentRoot /vhosts/1.com/htdocs/
</VirtualHost>
<VirtualHost 192.168.118.129:8080>
ServerName www.2.com
DocumentRoot /vhosts/2.com/htdocs/
</VirtualHost>
[[email protected] ~]# curl http://192.168.118.129:8080/
2.com
基于FQDN:
<VirtualHost *:80>
ServerName www.1.com
DocumentRoot /vhosts/1.com/htdocs/
</VirtualHost>
<VirtualHost *:80>
ServerName www.2.com
DocumentRoot /vhosts/2.com/htdocs/
</VirtualHost>
14、内置的status页面(939行左右)
<Location /server-status>
SetHandler server-status
Order deny,allow
Deny from all
Allow from 172.16.0.0/16
</Location>
15,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基本认证
–tcp-nodelay 使用TCP_NODELAY选项
-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
用法:curl [options] [URL…]
elinks需要下载
elinks -dump url 浏览文件
16,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
添加好之后,httpd -t 检查语法,然后重新加载配置文件
# cp /etc/rc.d/rc.sysinit /vhosts/1.com/htdocs/rc.txt 复制文件做测试
[[email protected] /]# curl –compress -I http://www.1.com/rc.txt
HTTP/1.1 200 OK
Date: Mon, 27 Apr 2015 09:41:14 GMT
Server: Apache/2.2.15 (CentOS)
Last-Modified: Mon, 27 Apr 2015 09:33:26 GMT
ETag: "a0445-4dca-514b170238948"
Accept-Ranges: bytes
Vary: Accept-Encoding
Content-Encoding: gzip
Content-Length: 6497
Connection: close
Content-Type: text/plain; charset=UTF-8
[[email protected] /]# curl -I http://www.1.com/rc.txt
HTTP/1.1 200 OK
Date: Mon, 27 Apr 2015 09:41:22 GMT
Server: Apache/2.2.15 (CentOS)
Last-Modified: Mon, 27 Apr 2015 09:33:26 GMT
ETag: "a0445-4dca-514b170238948"
Accept-Ranges: bytes
Content-Length: 19914
Vary: Accept-Encoding
Connection: close
Content-Type: text/plain; charset=UTF-8
17,配置https访问网站
加密方式:
ssl:v3
tls:v1
ssl会话过程:
1,客户端发送可供请求选择的加密方式,并向服务器请求证书;
2,服务器端发送整数以及选定的加密方式给客户端;
3,证书验证;
(1) 客户端发送可供选择的加密方式,并向服务器请求证书;
(2) 服务器端发送证书以及选定的加密方式给客户端;
(3) 证书验正:
如果信任给其发证书的CA:
(a) 验正证书来源的合法性;用CA的公钥解密证书上数字签名;
(b) 验正证书的内容的合法性:完整性验正
(c) 检查证书的有效期限;
(d) 检查证书是否被吊销;
(e) 证书中拥有者的名字,与访问的目标主机要一致;
(4) 客户端生成临时会话密钥(对称密钥),并使用服务器端的公钥加密此数据发送给服务器,完成密钥交换;
(5) 服务用此密钥加密用户请求的资源,响应给客户端;
注意:SSL会话是基于IP地址创建;所以单IP的主机上,仅可以使用一个https虚拟主机;
配置httpd支持https:
环境:192.168.118.128 CA服务器 ,192.168.118.129 web服务器
(1) 为服务器申请数字证书;
(a) 创建秘钥cakey.pem
#cd /etc/pki/CA
# (umask 077;openssl genrsa -out private/cakey.pem 2048)
(b) 生成自签证书
# openssl req -new -x509 -key private/cakey.pem -out cacert.pem -days 365
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:HA
Locality Name (eg, city) [Default City]:ZZ
Organization Name (eg, company) [Default Company Ltd]:QQ
Organizational Unit Name (eg, section) []:OPS
Common Name (eg, your name or your server's hostname) []:ca.1.com
Email Address []:
创建CA所需要的文件和目录
# touch index.txt
# echo 01 >serial <空格..>.
在web服务器上申请证书过程:
1.安装mod_ssl 要让客户机使用https访问,需要装mod_ssl
# yum install -y mod_ssl
2,在web服务器上创建目录/etc/httpd/ssl,并创建秘钥,
# (umask 077; openssl genrsa -out httpd.key 1024)
3,创建一个证书签署请求
# openssl req -new -key httpd.key -out httpd.csr
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:HA
Locality Name (eg, city) [Default City]:ZZ
Organization Name (eg, company) [Default Company Ltd]:QQ
Organizational Unit Name (eg, section) []:OPS
Common Name (eg, your name or your server's hostname) []:www.1.com (必须和虚拟主机定定义的要启用ssl的域名一致)
Email Address []:
在web服务器上,把证书发送给CA服务器申请签证
# scp httpd.csr [email protected]:/tmp
(c) CA签证
[[email protected] tmp]# openssl ca -in httpd.csr -out httpd.crt -days 365
把签完的证书发送给web服务器:
# scp httpd.crt 192.168.118.129:/etc/httpd/ssl/
[email protected]'s password:
httpd.crt 100% 3625 3.5KB/s 00:00
(2)在web服务器配置httpd支持使用ssl,及使用的证书;
# yum -y install mod_ssl
# rpm -ql mod_ssl 查看配置文件
切换到conf.d目录下,备份一份配置文件
[[email protected] conf.d]# cp ssl.conf{,.bak}
<VirtualHost _default_:443>改成 <VirtualHost 192.168.118.129:443>
DocumentRoot "/vhosts/1.com/htdocs/"
ServerName www.1.com:443
SSLCertificateFile /etc/pki/tls/certs/localhost.crt 服务器在会话时候的证书
/etc/httpd/ssl/httpd.crt
SSLCertificateKeyFile /etc/pki/tls/private/localhost.key私钥文件
/etc/httpd/ssl/httpd.key
(3) 测试基于https访问相应的主机;
[CA]# scp cacert.pem 192.168.118.129:/tmp 把CA的证书复制到web上
# openssl s_client -connect www.1.com:443 -CAfile /tmp/cacert.pem
# openssl s_client [-connect host:port] [-cert filename] [-CApath directory] [-CAfile filename]
mod_ssl 组件。支持ssl.
在window上做测试:
把CA上面的cacert.pem的证书导入到本地,后缀修改成crt.安装即可
https:/www.1.com
18,httpd自带的工具程序
htpasswd : basic认证文件实现
apachectl 启动脚本
apxs :有httpd-decel包提供,扩展httpd参数使用
#yum install apxs第三方扩展工具
rotateligs:日志滚动工具
suexec:访问某些有特殊权限配置的资源时,临时切换指定用户运行。
ab:aoache benchek
19,http压力测试工具
ab:ab:#ab -q 显示进度 -c 并发请求(峰值-因为http默认没有持久连接) -n 总的请求数量 [http://]域名[:端口]/路径
#ulimit -n 3000调整一次打开的文件…
webbench
http_load
tcpcopy: 网易-
编译安装-httpd-2.4.12
1,mpm支持运行dos机制。
8,基于fqdn的虚拟主机不再需要namevitualhost命令
9,支持用户自定义变量
模块:
1,mod_proxy_fcgi 跟php结合的时候三种模式
2,mod_ratelimit 速率
3,mod_remoteip
修改配置机制:
不再支持order,deny,alliow基于ip访问控制
编译安装步骤:
1.4+版的apr和apr-util
前提:
安装开发环境,安装pcre-devel
(1) apr
# ./configure –prefix=/usr/local/apr
# make && make install
(2) apr-util
# ./configure –prefix=/usr/local/apr-util –with=/usr/local/apr
# make && make install
(3)创建apache用户和组
# groupadd -r apache
# useradd -r -g apache apahce
(4)编译和安装
# ./configure –prefix=/usr/local/apache –sysconfig=/etc/httpd24 –enable-so –enable-ssl –enable-cgi –enable-rewrite –with-zlib –with-pcre –with-apr=/usr/local/apr –with-apr-util=/usr/local/apr-util/ –enable-modules=most –enable-mpms-shared=all –with-mpm=prefork
# make && make install
#apachectl 启动服务
待续….1:34
=========================命令整理=============================
# mii-tool eth0 查看网卡
# ethtool eth0 查看网卡底层信息
# ethtool -i eth0 查看网络驱动信息
# ethtool -S eth0 查看网卡底层工作状态
#ip addr add 172.25.215.40/24 dev etho label eth0:0
后面的eth0:0 表示,给eth0这块网卡增加一个IP别名,后面那个0表示别名号,第二个别名就可以写成 eth0:1
# vi ifcfg-eth0:0 添加永久ip别名
DEVICE=eth0:0 // 设备名字
IPADDR=172.25.215.31 // IP地址PREFIX=24 // 前缀是24,其实就是相当于子网掩码有24位
ONPARENT=yes // eth0:0是eth0的IP别名,yes表示附属于eth0
# ss -tnl 查看端口
#httpd -S 查看虚拟主机列表
#vi /etc/rc.d/rc.local 编辑配置文件添加下面行,让自动启动。
#apachectl 自带的服务控制脚本
#chkconfig –level 35 httpd off 让httpd服务运行在3和5系统级别上
#grep -v "#" httpd.conf | grep -v "^$" >httpd.conf
在httpd.conf 最后一行配置文件,用于设置虚拟主机的路径
Include conf/extra/httpd-vhosts.conf
#ab -q 显示进度 -c 并发请求 -n 总的请求数量 [http://]域名[:端口]/路径
==========================status状态码==============================
服务器返回客户端的请求状态:
1xx:100-101,信息提示
2xx:200-206,成功类响应
3xx:300-305,重定向类响应
4xx: 400-415, 错误类响应,客户端错误
5xx:500-505,服务器端错误。
常用状态码:
200-ok:成功。
301-moved permanently:请求url指向资源被删除。但在响应报文中通过首部localtion指定新资源所处
新位置。
302-found:与301相似,但是响应报文中用过localtion指定资源所处新位置。
304-not modified:客户端发出条件式请求,但服务器上的资源未曾发生改变。
401-unaithorized:需要输入帐号密码认证访问资源。
403-forbidden:请求被禁止。
404-Not Found:服务器无法找到客户端请求的资源。
500-internal server error:服务器内部错误。
502-bad gateway:代理服务器从后端服务器收到一条伪响应。
===============================================================