18、curl命令

  • 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命令常用选项

    -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

  • 使用举例

只显示响应报文首部信息:

[root@example.com ~]# curl -I 
http://www.example.com
HTTP/1.1 200 OK
Date: Mon, 03 Aug 2015 09:33:12 GMT
Server: Apache/2.2.15 (CentOS)
Last-Modified: Sun, 02 Aug 2015 16:59:17 GMT
ETag: "5ff52-13-51c56f93b72c4"
Accept-Ranges: bytes
Content-Length: 19
Connection: close
Content-Type: text/html; charset=UTF-8

设置用户代理发送给服务器:

[root@example.com ~]# curl -A testagent http://www.example.com
[root@example.com ~]# tail -2 /var/log/httpd/access_log 
192.168.1.8 - - [03/Aug/2015:17:34:00 +0800] "GET / HTTP/1.1" 200 19 "-" "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.15.3 zlib/1.2.3 libidn/1.18 libssh2/1.4.2"
192.168.1.8 - - [03/Aug/2015:17:35:42 +0800] "GET / HTTP/1.1" 200 19 "-" "testagent"

指定来源网址进行访问:

[root@example.com ~]# curl -e   http://www.example.com
[root@example.com ~]# tail -2 /var/log/httpd/access_log 
192.168.1.8 - - [03/Aug/2015:17:35:42 +0800] "GET / HTTP/1.1" 200 19 "-" "testagent"
192.168.1.8 - - [03/Aug/2015:17:40:14 +0800] "GET / HTTP/1.1" 200 19 "
http://www.baidu.com/index.html" "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.15.3 zlib/1.2.3 libidn/1.18 libssh2/1.4.2"

指定的CA证书访问:

[root@example.com ~]# curl --cacert /etc/pki/CA/cacert.pem 
 [root@example.com ~]# curl  https://www.example.com //若没有证书则不能访问https网站
curl: (60) Peer certificate cannot be authenticated with known CA certificatesMore details here: 
***************


19、使用mod_deflate模块压缩页面从而优化传输速度

   在使用此模块时一点要针对实际情况进行使用,若服务器带宽有限且CPU压力不大,则可以使用此模块,然而若CPU压力较大且带宽充足则使用此模块就得不偿失了,同时选项压缩的项目也要做好限定,对于难以压缩的文件就尽量不压缩,最后在定义是还要将有些不能支持压缩传输的几款浏览器就不要做限定了。

查看httpsd是否是否装载此模块,默认安装时就安装了:

[root@example.com ~]# httpd -M |grep deflate
 deflate_module (shared)
Syntax OK
[root@example.com ~]# cat /etc/httpd/conf.d/deflate.conf
SetOutputFilter DEFLATE

# mod_deflate configuration
<IfModule mod_deflate.c>
 
# 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)默认为6
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

</IfModule>
[root@example.com ~]# httpd -t
Syntax OK
[root@example.com ~]# service httpd reload
[root@example.com ~]# cp /var/log/messages /www/a.com/htdoc/test.html   //找个大点的文件进行浏览可以突出压缩的效果
[root@example.com ~]# chmod +rx /www/a.com/htdoc/test.html

在chrome浏览器下按F12打开测试窗口,然后在浏览器输入http://www.a.com/test.html,下面就可以看到是否压缩了。

wKiom1Xb7HayCgPlAAGZ3Cnw1JI816.jpg


20、资源限定

   资源限定分为:软限制,可以超出的限制,但仅能超出一定时长。 硬限制,绝对不能超出的限制;资源限定的命令为ulimit(软限制),以及其配置文件/etc/security/limits.conf,扩展配置etc/security/limits.d/*.conf(硬限制)。

常用选项:

       -n [N]:显示或限制能打开的最大的文件句柄数;

       -u [N]:所能够打开的最大进程数;

使用举例:

可以在配置文件中手动配置访问的最大进程和文件数,但是不能立即生效,若想生效就要用ulimit命令了。

[root@example.com ~]# vim /etc/security/limits.conf 
apache          hard     nofile         65535
apache          hard     nproc          33333

使用ulimit命令进行设置可以立即生效但是一旦机器重启就不能生效了。

[root@example.com ~]# ulimit -n 65535
[root@example.com ~]# ulimit -u 33333
[root@example.com ~]# ulimit -n
65535
[root@example.com ~]# ulimit -u
33333

21、ab工具的初步使用

  • ab命令简介

   ab(ApacheBench)命令会创建很多的并发访问线程,模拟多个访问者同时对某一URL地址进行访问。它的测试目标是基于URL的,因此,既可以用来测试Apache的负载压力,也可以测试nginx、lighthttp、tomcat、IIS等其它Web服务器的压力。ab命令对发出负载的计算机要求很低,既不会占用很高CPU,也不会占用很多内存,但却会给目标服务器造成巨大的负载,其原理类似CC***。自己测试使用也须注意,否则一次上太多的负载,可能造成目标服务器因资源耗完,严重时甚至导致死机。类是这样的软件还有:http_load webbench seige、tcp_copy。这种命令的执行结果不能全部说明服务器性能,但可以做为参考。

  • 常用选项

    -c 模拟的并发数

    -n 模拟的总请求数

注意:在使用时一般并发数(-c)应该小于等于请求数(-n);

 

命令安装包:

[root@example.com ~]# rpm -qf /usr/bin/ab
httpd-tools-2.2.15-45.el6.centos.x86_64

使用ab命令进行测试:

[root@example.com ~]# ab -c 100 -n 100 
http://www.b.org/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,

Benchmarking www.b.org (be patient).....done


Server Software:        Apache
Server Hostname:        www.b.org
Server Port:            80

Document Path:          /index.html
#测试的页面
Document Length:        12160 bytes
#页面大小

Concurrency Level:      100
#测试的并发数
Time taken for tests:   3.372 seconds
#整个测试持续的时间
Complete requests:      100
#完成的请求数量
Failed requests:        0
#失败的请求数量
Write errors:           0
Total transferred:      1317151 bytes
#整个过程中的网络传输量
HTML transferred:       1300888 bytes
#整个过程中的HTML内容传输量
Requests per second:    29.66 [#/sec] (mean)
#最重要的指标之一,相当于LR中的每秒事务数,后面括号中的mean表示这是一个平均值
Time per request:       3371.979 [ms] (mean)
#最重要的指标之二,相当于LR中的平均事务响应时间,后面括号中的mean表示这是一个平均值
Time per request:       33.720 [ms] (mean, across all concurrent requests)
#每个连接请求实际运行时间的平均值
Transfer rate:          381.46 [Kbytes/sec] received
#平均每秒网络上的流量,可以帮助排除是否存在网络流量过大导致响应时间延长的问题

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:      248  378 290.7    263    1293
Processing:   280 1126 649.5   1064    2547
Waiting:      271  865 405.0    797    1514
Total:        530 1504 600.0   1436    2812
#网络上消耗的时间的分解,各项数据的具体算法还不是很清楚

Percentage of the requests served within a certain time (ms)
  50%   1436
  66%   1687
  75%   1782
  80%   1925
  90%   2635
  95%   2664
  98%   2680
  99%   2812
 100%   2812 (longest request)

#整个场景中所有请求的响应情况。在场景中每个请求都有一个响应时间,其中50%的用户响应时间小于1436毫秒,66%的用户响应时间小于1687毫秒,最大的响应时间小于2812毫秒。对于并发请求,cpu实际上并不是同时处理的,而是按照每个请求获得的时间片逐个轮转处理的,所以基本上第一个Time per request时间约等于第二个Time per request时间乘以并发请求数。

22、扩展功能

  •  apachectl :Apache HTTP服务控制工具 

  •  ab :    Apache HTTP服务器性能测试工具 

  •  apxs :Apache 扩展工具 

  •  configure :配置源代码 

  •  dbmmanage :为基本认证创建和更新DBM格式的用户认证文件 

  •  htcacheclean :清理磁盘缓存 

  •  htdigest :为摘要认证创建和更新用户认证文件。 

  •  htdbm : 操作 DBM 密码数据库。 

  •  htpasswd :为基本认证创建和更新用户认证文件。 

  •  httxt2dbm :为 RewriteMap 创建 dbm 文件。 

  •  logresolve :将 Apache 日志文件中的 IP 地址解析到主机名称。 

  •  rotatelogs :不关闭 Apache 而切换日志文件。 

  •  suexec :执行外部程序前切换用户。

23、httpd-2.4编译安装

  • 下载相关软件

[root@example.com ~]# wget http://mirrors.aliyun.com/apache/apr/apr-1.5.2.tar.gz
[root@example.com ~]# wget http://mirrors.aliyun.com/apache/apr/apr-util-1.5.4.tar.gz
[root@example.com ~]# wget http://mirrors.aliyun.com/apache/httpd/httpd-2.4.16.tar.gz
  • 安装httpd-2.4所需要依赖的包

[root@example.com ~]# yum install -y gcc pcre-devel
  • 安装apr、apr-util工具

[root@example.com ~]# tar xf apr-1.5.2.tar.gz 
[root@example.com ~]# cd apr-1.5.2
[root@example.com apr-1.5.2]# ./configure --prefix=/usr/local/apr
[root@example.com apr-1.5.2]# make && make install
[root@example.com apr-1.5.2]# ls /usr/local/apr
bin  build-1  include  lib
[root@example.com ~]# tar -xf apr-util-1.5.4.tar.gz 
[root@example.com ~]# cd apr-util-1.5.4
[root@example.com apr-util-1.5.4]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr  (注意:--with-apr=/usr/local/apr是上面安装apr的路径)
[root@example.com apr-util-1.5.4]# make && make instal
[root@example.com apr-util-1.5.4]# ls /usr/local/apr-util/
bin  include  lib

(解释:apr,全称为Apache portable Run-time libraries,叫Apache可移植运行库,主要为上层的应用程序提供一个可以跨越多操作系统平台使用的底层支持接口库,安装apache都需要先安装这个库的)

  • httpd-2.4新特性

        1)MPM支持在运行时装载

        2)支持event

        3)异步读写

        4)在每模块及每目录上指定日志级别

        5)每请求配置:<If> <Elseif>

        6)增强版的表达式分析器

        7)毫秒级的keepalive timeout

        8)支持主机名的虚拟主机不在需要NameVirtualHost指令

        9)支持使用自定义变量

   新增一些模块:mod_proxy_fcgi、mod_ratelimit、mod_request、mod_remoteip。 对于基于IP的访问做了修改,不在使用order、allow、deny这些机制,而是统一使用require进行。

  • 编译httpd-2.4

[root@example.com ~]# tar xf httpd-2.4.16.tar.gz 
[root@example.com ~]# cd httpd-2.4.16
[root@example.com httpd-2.4.16]# ./configure --prefix=/usr/local/apache --sysconfdir=/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-modeles=most(加载常用的模块) --enable-mpms-shared=all --with-mpm=event
[root@example.com httpd-2.4.16]# ls /usr/local/apache/
bin  build  cgi-bin  error  htdocs  icons  include  logs  man  manual  modules

   注意:我在编译安装httpd-2.4之前已经安装过基于rpm包的httpd-2.2软件,所以在定义文件路径时一定不要和httpd-2.2的文件路径一致。

# 各编译参数详解
--prefix:#安装路径
--sysconfdir:#指定配置文件路径
--enable-so:#DSO兼容,DSO=Dynamic Shared Object,动态共享对象,可实现模块动态生效
--enable-ssl:#支持SSL/TLS,可以实现https访问
--enable-cgi:#支持CGI脚本(默认对非线程的MPM模式开启)
--enable-rewrite:#启用Rewrite功能
--enable-deflate:#支持压缩
--with-z:#使用指定的zlib库,不指定路径会自动寻找
--with-pcre:#使用指定的PCRE库,不指定路径会自动寻找
--with-apr:#指定apr安装路径
--with-apr-util:#指定apr-util安装路径
--enable-modules:#支持动态启用的模块,可选参数有“all”,“most”,“few”,“reallyall”
--enable-mpms-shared:#支持动态加载的MPM模块,可选“all”
--with-mpm:#设置默认启用的MPM模式
  • 后续配置

导出头文件:

[root@example.com ~]# ln -sv /usr/local/appache/include /usr/include/httpd

导出帮助文件:

[root@example.com ~]# vim /etc/man.config   //在配置文件中添加下面的配置
MANPATH /usr/local/apache/man

由于我们在源码编译安装前以及安装过的httpd-2.2的软件包,若想查看httpd-2.4的帮助文档则要需要用-M选项加man文件路径。

[root@example.com ~]# man httpd
Apache HTTP Server                2007-08-01                          HTTPD(8)
[root@example.com ~]# man -M /usr/local/apache/man httpd
Apache HTTP Server                2012-02-10                          HTTPD(8)

查看系统所有的库文件:

[root@example.com ~]# ldconfig -p

修改环境变量:

[root@example.com ~]# vim /etc/profile.d/httpd.sh
export PATH=/usr/local/apache/bin:$PATH
[root@example.com ~]# .  /etc/profile.d/httpd.sh
[root@example.com ~]# httpd -V
Server version: Apache/2.4.16 (Unix)
Server built:   Aug  3 2015 20:28:13
Server's Module Magic Number: 20120211:47
Server loaded:  APR 1.5.2, APR-UTIL 1.5.4
Compiled using: APR 1.5.2, APR-UTIL 1.5.4
****************************
  • 为httpd-2.4提供service服务脚本

添加启动脚本:

[root@example.com~]# cat /etc/init.d/httpd24 
#!/bin/bash
#
# httpd        Startup script for the Apache HTTP Server
#
# chkconfig: - 85 15
# description: Apache is a World Wide Web server.  It is used to serve \
#      HTML files and CGI.
# processname: httpd
# config: /etc/httpd24/conf/httpd.conf
# config: /etc/sysconfig/httpd
# pidfile: /var/run/httpd24.pid
# Source function library.
. /etc/rc.d/init.d/functions
 
if [ -f /etc/sysconfig/httpd ]; then
        . /etc/sysconfig/httpd
fi

# Start httpd in the C locale by default.
HTTPD_LANG=${HTTPD_LANG-"C"}

# This will prevent initlog from swallowing up a pass-phrase prompt if
# mod_ssl needs a pass-phrase from the user.
INITLOG_ARGS=""

# Set HTTPD=/usr/sbin/httpd.worker in /etc/sysconfig/httpd to use a server
# with the thread-based "worker" MPM; BE WARNED that some modules may not
# work correctly with a thread-based MPM; notably PHP will refuse to start.

# Path to the apachectl script, server binary, and short-form for messages.
apachectl=/usr/local/apache/bin/apachectl
httpd=${HTTPD-/usr/local/apache/bin/httpd}
prog=httpd
pidfile=${PIDFILE-/var/run/httpd24.pid}
lockfile=${LOCKFILE-/var/lock/subsys/httpd24}
RETVAL=0
start() {
        echo -n $"Starting $prog: "
        LANG=$HTTPD_LANG daemon --pidfile=${pidfile} $httpd $OPTIONS
        RETVAL=$?
        echo
        [ $RETVAL = 0 ] && touch ${lockfile}
        return $RETVAL
}
 
stop() {
echo -n $"Stopping $prog: "
killproc -p ${pidfile} -d 10 $httpd
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}
}
reload() {
    echo -n $"Reloading $prog: "
    if ! LANG=$HTTPD_LANG $httpd $OPTIONS -t >&/dev/null; then
        RETVAL=$?
        echo $"not reloading due to configuration syntax error"
        failure $"not reloading $httpd due to configuration syntax error"
    else
        killproc -p ${pidfile} $httpd -HUP
        RETVAL=$?
    fi
    echo
}

# See how we were called.
case "$1" in
  start)
start
;;
  stop)
stop
;;
  status)
        status -p ${pidfile} $httpd
RETVAL=$?
;;
  restart)
stop
start
;;
  condrestart)
if [ -f ${pidfile} ] ; then
stop
start
fi
;;
  reload)
        reload
;;
  graceful|help|configtest|fullstatus)
$apachectl $@
RETVAL=$?
;;
  *)
echo $"Usage: $prog {start|stop|restart|condrestart|reload|status|fullstatus|graceful|help|configtest}"
exit 1
esac
exit $RETVAL
[root@example.com ~]# chmod +x /etc/init.d/httpd24

 

把httpd服务添加为开机自启动:

[root@example.com ~]# chkconfig --add httpd 24
[root@example.com ~]# chkconfig httpd24 on

修改httpd主配置文件/etc/httpd24/httpd.conf,指定httpd服务的PID文件,直接在配置文件添加如下字段:

PidFile "/var/run/httpd24.pid"

启动服务:

[root@example.com init.d]# service httpd24 start
正在启动 httpd:               [确定]

浏览器测试:

wKiom1Xb7LHyDrt-AABy36eZKJA672.jpg