Apache基本使用总结

一、常用配置

1、修改监听的IP和Port
Listen[IP:]PORT
Listen 12.24.34.56:80
Listen 8080
# 省略ip表示监听本机所有IP;Listen可重复出现多次;
2、持久连接
KeepAive On # 开启
MaxKeepAliveRequests 0  # 一个持久TCP最多允许的请求数,这里设置0表示不限制)
KeepAliveTimeout 15 # 表示15秒
KeepAlive是什么东西?它是http协议的一部分,让我们复习一下没有KeepAlive的http请求,从客户在浏览器输入一个有效url地址开始,浏览器就会利用socket向url对应的web服务器发送一条TCP请求,这个请求成功一次就得需要来回握三次手才能确定,成功以后,浏览器利用socket TCP连接资源向web服务器请求http协议,发送以后就等着web服务器把http返回头和body发送回来,发回来后浏览器关闭socket连接,然后做http返回头和body的解析工作,最后呈现在浏览器上的就是漂亮的页面了。这里面有什么问题呢?TCP连接需要三次握手,也就是来回请求三次方能确定一个TCP请求是否成功,然后TCP关闭呢?来回需要4次请求才能完成!每次http请求就3次握手,4次拜拜,这来来回回的不嫌累啊,多少时间和资源都被浪费在socket连接关闭上了,能不能一次socket TCP连接发送多次http请求呢?于是KeepAlive就应运而生,http/1.0里需要客户端自己在请求头加入Connection:Keep-alive方能实现,在这里我们只考虑http1.1了,只需要设置一下Apache,让它默认就是Keep-Alive持久连接模式(Apache必须1.2+才能支持Keep-Alive)。在httpd.conf里找到KeepAive配置项,果断设置为On,MaxKeepAliveRequests果断为0(一个持久TCP最多允许的请求数,如果过小,很容易在TCP未过期的情况下,达到最大连接,那下次连接就又是新的TCP连接了,这里设置0表示不限制),然后对于mysql_pconnect最重要的选项KeepAliveTimeout设置为15(表示15秒), httpd-2.4支持毫秒级持久时间。 测试:
很简单,获取当前PHP执行者(Apache)的进程号,用浏览器浏览这个页面,看到什么?对,有看到一串进程号数字,15秒内,连续刷新页面,看看进程号有无变化?木有吧?现在把手拿开,交叉在胸前,度好时间,1秒,2秒,3,...15,16。好,过了15秒了,再去刷新页面,进程号有没有变化?变了!又是一个新的Apache进程了,为什么15秒后就变成新的进程了?记得我们在Apache里设置的KeepAliveTimeout吗?它的值就是15秒。现在我们应该大致清楚了,在web服务器默认打开KeepAlive的情况下,客户端第一次http成功请求后,Apache不会立刻断开socket,而是一直监听来自这一客户端的请求,监听多久?根据KeepAliveTimeout选项配置的时间决定,一旦超过这一时间,Apache就会断开socket了,那么下次同一客户端再次请求,Apache就会新开一个进程来相应。所以我们之前15内不停的刷新页面,看到的进程号都是一致的,表明是浏览器请求给了同一个Apache进程。 浏览器是怎么知道不需要重新进行TCP连接就可以直接发送http请求呢?因为http返回头里就会带上Connection:keep-alive,Keep-alive:15两行,意思就是让客户端浏览器明白,这次socket连接我这边还没关闭呢,你可以在15内继续使用这个连接,并发送http请求,于是乎浏览器就知道应该怎么做了。   3、MPM参数 MultipathProcessModule:多道处理模块 prefork,worker,event

StartServers       8    #服务启动时启动的子进程数
MinSpareServers    5    #最少空闲子进程数
MaxSpareServers   20
ServerLimit      256    #同时启动的子进程数上限
MaxClients       256    #同时服务的客户端数上限(支持的并发数上限)
MaxRequestsPerChild  4000   #每个子进程在其生命周期内处理的请求数上限

 

StartServers         4   #服务启动时启动的子进程数
MaxClients         300
MinSpareThreads     25
MaxSpareThreads     75
ThreadsPerChild     25    #每个子进程可启动的线程数
MaxRequestsPerChild  0    #每个子进程可处理的请求数,0表示无限制
4、DSO
配置指令实现模块加载
LoadModule  
#要卸载某个模块直接将其注释掉即可,不用重读配置文件就可立即生效;模块路径为相对于ServerRoot而言的路径
显示DSO动态装载的模块:
# httpd -D DUMP_MODULES
Loaded Modules:
 core_module (static)
 mpm_prefork_module (static)
 http_module (static)
 so_module (static)
 auth_basic_module (shared)
 auth_digest_module (shared)
 authn_file_module (shared)
 authn_alias_module (shared)
 ...
# httpd -l
Compiled in modules:
  core.c
  prefork.c
  http_core.c
  mod_so.c
# httpd.worker -l
Compiled in modules:
  core.c
  worker.c
  http_core.c
  mod_so.c
5、定义'Main'server的文档页面路径
DocumentRoot "/path/to/somefile"
文档路径映射:
DocumentRoot 指向的路径为URL路径的起始位置;
DocumentRoot "/var/www/html"   #默认为这个位置
test/index.html-->http://HOST:PORT/test/index.html
6、站点访问控制 可基于两种类型的路径指明对哪些资源进行访问控制
文件系统路径:

...

[~] "/path/to/somewhere">
...

基于URL访问路径做访问控制:

...

#另外,路径可做模式匹配,但若非迫不得已不建议使用
#如果即能使用Diretoory控制,也能使用Location控制,建议使用Directory
7、Directory中“基于来源地址”实现访问控制
(1)Options
所有可用特性:Indexes,Includes,FollowSymLinks,SymLinksifOwnerMatch 
         ExecCGI,MultiViews,None,All
Indexes:索引;在无默认主页面又无欢迎页时,将所有资源以列表形式呈现给用户。
     危险,慎用;在选项前添加减号即为禁用。如-Indexes
FollowSymlinks:允许跟踪符号链接文件;
# vim /etc/httpd/conf/httpd.conf

    Options Indexes FollowSymLinks   #默认是开启的
    AllowOverride None
    Order allow,deny
    Allow from all

(2)AllowOverride
支持在每个页面目录下创建.htaccess用于实现对此目录中资源访问时的访问控制功能;
.htaccess文件会影响httpd的性能
(3)基于IP做访问控制机制
    Order:检查次序
        Order allow,deny
        Allow form 192.168.10.0/24
            form后面能够接受的地址格式:
                IP,Network Address
                网络地址格式较为灵活:
                    172.16
                    172.16.0.0
                    172.16.0.0/16
                    172.16.0.0/255.255.0.0
8、定义默认主页面
DirecotryIndex index.htm lindex.html.var
9、日志设定
ErrorLog "/path/to/error_log"   #错误日志,路径为相对于ServerRoot而言的路径
LogLevel {debug|info|notice|warn|error|crit|alert|emerg}
   指定级别及比其更高级别的信息都会被记录      
LogFormat 格式 格式名
  %h: 客户端地址
  %l: 远程登录名,通常为-
  %u: 认证时输入用户名,没有认证时为-
  %t: 服务器收到 用户请求时的时间
  %r:请求报名的起始行
  %>s: 响应状态码
  %b: 响应报文的长度,单位是字节
  %{HEADER_NAME}i: 记录指定首部对应的值
例如 LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined    
#格式中若要使用引号则要使用反斜线转义
    CustomLog "/path/to/access_log" LogFormat_Name
10、路径别名 实现URL路径的映射,从而所访问的资源不再依赖于站点根目录。 Alias /URL/ "/path/to/somewhere/" 例如 Alias /images/ "/www/tupian/" #后面映射的路径是绝对路径,而不是相对于站点根目录而言的路径;此时若站点根目录(以/var/www/html为例)下也有一个images目录,那么将无法访问/var/www/html/images中的资源,因为images已被别名征用
# mkdir test hello
# cat test/a.html 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
# cat hello/b.html 
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
# vim /etc/httpd/conf/httpd.conf
Alias /test/ "/www/html/hello/"
# service httpd restart
停止 httpd:                                               [确定]
正在启动 httpd:                                           [确定]
 
# curl http://localhost/test/a.html


404 Not Found

Not Found

The requested URL /test/a.html was not found on this server.


Apache/2.2.15 (CentOS) Server at localhost Port 80
# curl http://localhost/test/b.html bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
11、设定默认字符集
AddDefaultCharset UTF-8
# 字符集:GBK,GB2312,GB18030
12、基于用户的访问控制
用户认证类型:
  基本认证:Basic,明文发送
  摘要认证:digest
虚拟用户:仅用于访问某服务或获取某资源的凭证;
  账号和密码的存储机制:
    文本文件:.htpasswd
    SQL数据库
    dbm:数据库引擎,提供API
    ldap:
案例:基于文件做访问控制
(1)基于用户进行认证
    
        Options none
        AllowOverride AuthConfig
        AuthType Basic
        AuthName "admin area"
        AuthBasicProvider file
        AuthUserFile /etc/httpd/conf/.htpasswd
        Require valid-user
    
    # Require valid-user:文件中所有用户均可访问
    # Require user USERNAME,... 指定用户访问
(2)提供认证文件
    htpasswd [option] passwdfile username
    选项:
    -c:创建一个passwdfile,如果passwdfile已经存在,那么它会重新写入并删除原有内容
    -m:以md5的格式编码存储用户的密码信息
    -s:sha1加密用户密码;
    -D:删除指定用户
(3)基于组认证
    
        Options none
        AllowOverride AuthConfig
        AuthType Basic
        AuthName "admin area"
        AuthBasicProvider file
        AuthUserFile /etc/httpd/conf/.htpasswd
        AuthGroupFile /etc/httpd/conf/.htgroup
        Require group GROUP_NAME
    
    组文件(.htgroup)格式    组名:user1 user2 user3
例如:
# cd /var/www/html
# mkdir admin
# cat admin/admin.html  
The user is admin.  
# vim /etc/httpd/conf/httpd.conf
    
    Options none
    AllowOverride AuthConfig
    AuthType Basic
    AuthName "admin area"
    AuthBasicProvider file
    AuthUserFile /etc/httpd/conf/.htpasswd
    Require valid-user
    
# htpasswd -c -m /etc/httpd/conf/.htpasswd bjwf #创建第一个用户时必须创建文件
New password: 
Re-type new password: 
Adding password for user bjwf
# htpasswd -m /etc/httpd/conf/.htpasswd tom   #创建第二个用户
New password: 
Re-type new password: 
Adding password for user tom
# service httpd restart
[attach]1660[/attach] 13、虚拟主机
有三种实现方案:
  基于ip:为每个虚拟主机准备至少一个ip地址;
  基于port:为每个虚拟主机准备至少一个专用port;实践中很少使用;
  基于hostname:为每个虚拟主机准备至少一个专用hostname;
可混合使用上述三种方式中任意方式;
注意:一般虚拟主机莫与中心主机混用,所以,要使用虚拟主机,先禁用中心主机;
禁用中心主机:注释DocumentRoot
每个虚拟主机都有专用配置:

SeverName
DocumentRoot""

ServerAlias:虚拟主机的别名;
ErrorLog
CustomLog


 
示例1:基于ip
  
    ServerName web1.magedu.com
    DocumentRoot"/vhosts/web1/htdocs"
  
 
  
    ServerName web2.magedu.com
    DocumentRoot "/vhosts/web2/htdocs"
  
示例2:基于port
  
    ServerNameweb2.magedu.com
    DocumentRoot"/vhosts/web2/htdocs"
  
 
  
    ServerName web3.magedu.com
    DocumentRoot "/vhosts/web3/htdocs"
  
示例3:基于hostname
  
    ServerName web1.magedu.com
    DocumentRoot "/vhosts/web1/htdocs"
  
 
  
    ServerName web2.magedu.com
    DocumentRoot "/vhosts/web2/htdocs"
  
 
  
    ServerName web3.magedu.com
    DocumentRoot "/vhosts/web3/htdocs"
  
#注:一般要启用虚拟主机,最好是新建一个文档在/etc/httpd/conf.d/vhost.conf里面,这样一来虚拟主机
设定的参数可以随便修改,而且不会影响到主配置文件,而主配置文件httpd.conf中有个include的参数可以
将/etc/httpd/conf.d/*.conf都包含进来。
示例:
# mkdir /var/www/html/{a.com,b.net,c.org} -pv
mkdir: 已创建目录 "/var/www/html/a.com"
mkdir: 已创建目录 "/var/www/html/b.net"
mkdir: 已创建目录 "/var/www/html/c.org"
# echo a.com > /var/www/html/a.com/index.html
# echo b.net > /var/www/html/b.net/index.html
# echo c.org > /var/www/html/c.org/index.html
# cat vhost.conf (基于IP的虚拟主机)

    ServerName www.a.com
    DocumentRoot "/var/www/html/a.com"

 

    ServerName www.b.net
    DocumentRoot "/var/www/html/b.net"

 

    ServerName www.c.org
    DocumentRoot "/var/www/html/c.org"

# curl http://192.168.130.250
a.com
# curl http://192.168.130.251
b.net
# curl http://192.168.130.252
c.org
 
# cat vhost.conf  (基于主机名的虚拟主机)
NameVirtualHost *:80

    ServerName www.a.com
    DocumentRoot "/var/www/html/a.com"

 

    ServerName www.b.net
    DocumentRoot "/var/www/html/b.net"

 

    ServerName www.c.org
    DocumentRoot "/var/www/html/c.org"

# cat /etc/hosts
192.168.130.250 kvm.jlc.com
192.168.130.250 www.a.com
192.168.130.250 www.b.net
192.168.130.250 www.c.org
# curl http://www.a.com
a.com
# curl http://www.b.net
b.net
# curl http://www.c.org
c.org
14、内置的status页面 handler:当文件被调用时,apache的内部表现形式,一般每种文件类型都有其隐式处理器 httpd内嵌有handler,其中有一个handler(server-status)用于输出当前httpd服务相关状态信息 启用handler要使用SetHandler指定
# vim httpd.conf 

    SetHandler server-status
    Order allow,deny
    Allow from 192.168.130.0/24
    Allow from 192.168.120.0/24
    Deny from all

# httpd -t  #检查配置文件语法
Syntax OK
# service httpd reload
[attach]1661[/attach] 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 [option] URL
常用选项:
    -A/--user-agent  设置用户代理发送给服务器
    -basic 使用HTTP基本认证
    --tcp-nodelay 使用TCP_NODELAY选项
    -e/--referer  来源网址
    --cacert  CA证书 (SSL)
    --compressed 要求返回是压缩的格式
    -H/--header 自定义首部信息传递给服务器
    -I/--head 只显示响应报文首部信息
    --limit-rate  设置传输速度
    -u/--user [:password]>设置服务器的用户和密码
    -0/--http1.0 使用HTTP 1.0
16、使用mod_deflate模块压缩页面优化传输速度 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
17、https http over ssl = https 443/tcp 一般使用的协议ssl: v3  tls: v1   SSL会话的简化过程: (1) 客户端发送可供选择的加密方式,并向服务器请求证书; (2) 服务器端发送证书以及选定的加密方式给客户端; (3) 客户端取得证书并进行证书验正: 如果信任给其发证书的CA: (a) 验正证书来源的合法性;用CA的公钥解密证书上数字签名; (b) 验正证书的内容的合法性:完整性验正 (c) 检查证书的有效期限; (d) 检查证书是否被吊销; (e) 证书中拥有者的名字,与访问的目标主机要一致; (4) 客户端生成临时会话密钥(对称密钥),并使用服务器端的公钥加密此数据发送给服务器,完成密钥交换; (5) 服务用此密钥加密用户请求的资源,响应给客户端; 注意:SSL会话是基于IP地址创建;所以单IP的主机上,仅可以使用一个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]
示例:在一台主机上既当CA又有http
1、httpd上生成证书
# mkdir /etc/httpd/ssl
# cd /etc/httpd/ssl
# (umask 077;openssl genrsa -out httpd.key 1024)
Generating RSA private key, 1024 bit long modulus
..........................................++++++
..............++++++
e is 65537 (0x10001)
[root@kvm ssl]# openssl req -new -key httpd.key -out httpd.csr -days 3655
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) []:BJ
Locality Name (eg, city) [Default City]:BJ
Organization Name (eg, company) [Default Company Ltd]:JLC
Organizational Unit Name (eg, section) []:JSB
Common Name (eg, your name or your server's hostname) []:www.bjwf.com
Email Address []:admin@bjwf.com        
 
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
2、自建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)
[root@kvm CA]# openssl req -new -x509 -key private/cakey.pem -out cacert.pem -days 3655
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) []:BJ
Locality Name (eg, city) [Default City]:BJ
Organization Name (eg, company) [Default Company Ltd]:JLC
Organizational Unit Name (eg, section) []:JSB
Common Name (eg, your name or your server's hostname) []:ca.bjwf.com
Email Address []:ca@bjwf.com
# touch index.txt serial   #准备文件
# echo 01 > serial
# openssl ca -in httpd.csr -out httpd.crt -days 365  #CA签署证书
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: Jun 23 08:06:22 2016 GMT
            Not After : Jun 23 08:06:22 2017 GMT
        Subject:
            countryName               = CN
            stateOrProvinceName       = BJ
            organizationName          = JLC
            organizationalUnitName    = JSB
            commonName                = www.bjwf.com
            emailAddress              = admin@bjwf.com
        X509v3 extensions:
            X509v3 Basic Constraints: 
                CA:FALSE
            Netscape Comment: 
                OpenSSL Generated Certificate
            X509v3 Subject Key Identifier: 
                83:60:A6:B7:E5:EF:E1:50:A6:69:E2:BD:3A:60:50:AF:A6:36:1B:39
            X509v3 Authority Key Identifier: 
                keyid:B2:5F:33:01:B3:1A:B5:B9:2B:0F:C0:A5:37:48:AE:FD:3E:49:09:5F
 
Certificate is to be certified until Jun 23 08:06:22 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
3、配置https
# yum -y install mod_ssl
# cd /etc/httpd/conf.d
# vim ssl.conf 

...
DocumentRoot "/var/www/html/a.com"
ServerName a.com:443
... 
SSLCertificateFile /etc/httpd/ssl/httpd.crt   #指明服务器证书的位置
...
SSLCertificateKeyFile /etc/httpd/ssl/httpd.key    #指明服务器密钥的位置
...

# httpd -t
Syntax OK
# service httpd restart   #查看https服务端口是否监听
停止 httpd:                                               [确定]
正在启动 httpd:                                           [确定]
[root@kvm conf.d]# netstat -tnlp|grep 443
tcp        0      0 :::443        :::*       LISTEN      15378/httpd  
# openssl s_client -connect 192.168.130.250:443 -CAfile /etc/pki/CA/cacert.pem
CONNECTED(00000003)
---
New, TLSv1/SSLv3, Cipher is ECDHE-RSA-AES256-GCM-SHA384
Server public key is 1024 bit
Secure Renegotiation IS supported
Compression: NONE
Expansion: NONE
SSL-Session:
    Protocol  : TLSv1.2
    Cipher    : ECDHE-RSA-AES256-GCM-SHA384
    Session-ID: 82D9C7A1676D82AB28F73C8C0A6322A023689B8F73491E3E78B73F7E
    Session-ID-ctx: 
    Master-Key: 340D147D1F4B479A0226EC9708728C9D872BFAF1B20B02C3403C9EDA98578D4
    Key-Arg   : None
    Krb5 Principal: None
    PSK identity: None
    PSK identity hint: None
    TLS session ticket lifetime hint: 300 (seconds)
    TLS session ticket:
    0000 - 31 a3 1a 34 ba e5 11 99-77 6e e2 86 7b dc cf 3f   1..4....wn..{..?
    0010 - d0 a9 3f 22 f0 77 4e ec-31 52 6b dd a7 70 41 44   ..?".wN.1Rk..pAD
    0020 - e9 02 25 1f ee 24 56 d3-77 ac 56 10 e7 99 12 63   ..%..$V.w.V....c
    0030 - 2e 8c d9 09 15 32 59 21-46 17 8e 77 de 0f ff e8   .....2Y!F..w....
    0040 - 31 b4 e1 57 63 02 09 a1-31 42 8f f2 3f e3 49 c6   1..Wc...1B..?.I.
    0050 - e7 66 ee 95 8b 1c 6a 7f-cc 8e 9b 5e 83 c2 c9 2f   .f....j....^.../
    0060 - 8e 26 62 1b 0b 39 4b 79-50 80 5e de c6 dc 01 f9   .&b..9KyP.^.....
    0070 - 81 16 c7 3b 5a 90 3a a2-85 27 b4 7c ea 20 d5 83   ...;Z.:..'.|. ..
    0080 - 7e fe 9f 37 8b b3 66 3e-e1 e8 a6 fd 32 9b d0 82   ~..7..f>....2...
    0090 - 9f b0 be 7e a4 cf cf 6d-85 0d 93 ca 2b 70 c5 cf   ...~...m....+p..
    00a0 - 55 57 07 f7 bc fc 1d a2-df 30 d0 b6 05 6c 25 15   UW.......0...l%.
    00b0 - ee 32 51 6c 87 51 25 b9-55 3b 96 d3 50 4a 03 b6   .2Ql.Q%.U;..PJ..
 
    Start Time: 1466669780
    Timeout   : 300 (sec)
    Verify return code: 0 (ok)
---
 
GET /index.html HTTP/1.1
HOST: www.a.com
 
HTTP/1.1 200 OK
Date: Thu, 23 Jun 2016 08:16:51 GMT
Server: Apache/2.2.15 (CentOS)
Last-Modified: Thu, 23 Jun 2016 06:27:07 GMT
ETag: "260639-6-535ec24af4cd1"
Accept-Ranges: bytes
Content-Length: 6
Connection: close
Content-Type: text/html; charset=UTF-8
 
a.com
closed
[attach]1662[/attach]

二、httpd自带工具

1、httpd:apache服务器程序
  常用选项:
    -t:测试配置文件
    -l:显示静态编译进httpd的模块
    -D DUMP_MODULES:列出DSO模块
    -M:相当于-t -D DUMP_MODULES
    -D DUMP_VHOSTS:列出所有虚拟主机
2、htpasswd:为基于文件的basic认证创建和更新用户认证文件
  常用选项:
    -c:创建一个passwdfile,如果passwdfile已经存在,那么它会重新写入并删除所有内容
    -m:以md5的格式编码存储用户的密码信息
    -s:sha1加密用户密码
    -D:删除指定用户
3、apachectl:脚本,httpd服务控制工具
  常用选项:
    start:启用服务
    stop:关闭服务
    status:查看服务
    configtest:测试服务配置文件
4、ab:apache benchmark,httpd的基准性能测试工具
  用法:ab [options] [http[s]://]hostname[:port]/path/to/somefile
  常用选项:
    -c #:模拟的并发数
    -n #:模拟的请求数,-n的值一定要大于等于-c的值
  同类工具:http_load,webbench,seige
5、apxs:httpd得以扩展使用第三方模块的工具
6、htcacheclean:磁盘缓存清理工具
7、htdigest:为digest认证创建和更新用户认证文件
8、httxt2dbm:为rewrite map创建dbm格式的文件
9、rotatelogs:滚动日志,不关闭httpd而切换其使用日志文件的工具
    access_log, access_log.1, access_log.2,
10、suexec:当httpd进程需要以另外的用户的身份去访问某些资源时,可以以suexec作临时切换;
 

三、资源限定

软限定:可临时超出一定时长的上限 硬限定:绝对不可超出的上限
可以使用ulimit命令临时的修改各种资源的软限制:
  ulimit -n [#]:显示或设置能同事打开的文件句柄数
    linux内核允许普通用户最多同时打开1024个文件
  ulimit -u [#]:显示或设备能同时启动的进程数
  ulimit -a:显示当前资源限定的设定
要长期有效,需要修改配置文件
  /etc/security/limits.conf
  /etc/security/limits.d/*.conf
 
更多选项:参考博客文档收集
ulimit -a 用来显示当前的各种用户进程限制。
Linux对于每个用户,系统限制其最大进程数。为提高性能,可以根据设备资源情况,设置各linux用户
的最大进程数,下面我把某linux用户的最大进程数设为10000个:
ulimit -u 10000
其他建议设置成无限制(unlimited)的一些重要设置是:
数据段长度:ulimit -d unlimited
最大内存大小:ulimit -m unlimited
堆栈大小:ulimit -s unlimited
CPU 时间:ulimit -t unlimited
虚拟内存:ulimit -v unlimited
linux对用户有默认的ulimit限制,而这个文件可以配置用户的硬配置和软配置,硬配置是个上限。
超出上限的修改就会出“不允许的操作”这样的错误。
   
       
# ulimit -n
1024
# ulimit -n 65535
# ulimit -n
65535
 
#                 
#
 
#*               soft    core            0
#*               hard    rss             10000
#@student        hard    nproc           20
#@faculty        soft    nproc           20
#@faculty        hard    nproc           50
#ftp             hard    nproc           0
#@student        -       maxlogins       4
 
# End of file
*        soft    noproc 10240
*        hard    noproc 10240
*        soft    nofile 10240
*        hard    nofile 10240     这就可以限定用户打开的最大线程及文件为10240个
 

四、AB测试

# ab -c 300 -n 3000 http://www.a.com/index.html
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
 
Benchmarking www.a.com (be patient)
Completed 300 requests
Completed 600 requests
Completed 900 requests
Completed 1200 requests
Completed 1500 requests
Completed 1800 requests
Completed 2100 requests
Completed 2400 requests
Completed 2700 requests
Completed 3000 requests
Finished 3000 requests
 
 
Server Software:        Apache/2.2.15
Server Hostname:        www.a.com
Server Port:            80
 
Document Path:          /index.html
Document Length:        6 bytes   #请求的资源大小
 
Concurrency Level:      300     #并发数
Time taken for tests:   0.630 seconds  #测试的总时长
Complete requests:      3000    #请求总数
Failed requests:        0     #失败的请求
Write errors:           0
Total transferred:      816272 bytes   #总共传输了多少字节
HTML transferred:       18006 bytes    #整个场景中HTML内容传输量
Requests per second:    4760.79 [#/sec] (mean)  #每秒处理的请求个数
Time per request:       63.015 [ms] (mean)      #每处理一个并发单位所经过的时长
Time per request:       0.210 [ms] (mean, across all concurrent requests) #处理单个请求的时长
Transfer rate:          1265.01 [Kbytes/sec] received  #传输速率
 
Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    2   3.4      1      16
Processing:     1   47 129.2     18     608
Waiting:        1   47 129.2     18     608
Total:          8   49 131.4     19     619
 
Percentage of the requests served within a certain time (ms)
  50%     19
  66%     19
  75%     20
  80%     20
  90%     21
  95%    617
  98%    618
  99%    618
 100%    619 (longest request)
原文地址:http://79076431.blog.51cto.com/8977042/1792118  

0 个评论

要回复文章请先登录注册