Fork me on GitHub

Apache HTTP Server 编译与安装

在使用ApacheBench做压力测试的时候出现30srequest超时的情况,由于版本老旧,ab -h中发现并没有-s timeout的参数配置,Google上找了半天也没有可用的Binaries版本,于是开始自己编译httpd源码

针对于ab timeout的问题(报错:The timeout specified has expired (70007)),加了-k 参数保证keepalived参数也无用,在2.4.4+版本中加入了-s参数控制timeout;

1
2
-s timeout
Maximum number of seconds to wait before the socket times out. Default is 30 seconds. Available in 2.4.4 and later.
SoftWare Version
Ubuntu Server x64 14.04
pcre 8.38
apr-util 1.6.1
apr 1.6.5
httpd 2.4.38
gcc & gcc-c++ 5.4.0

gcc & gcc-c++ 系统自带;如果自己本机没有需要先install下

pcre 编译

1
2
3
4
5
6
7
8
unzip pcre-8.38.zip 
cd pcre-8.38

# 指定编译目录
./configure --prefix=/usr/local/pcre

# 编译安装
sudo make & sudo make install

注意在make 失败后,一定要使用make clean命令清空环境

  • 错误1
1
xml/apr_xml.c:35:19: fatal error: expat.h: No such file or directory
  • 解决

Centos安装:yum install expat-devel

ubuntu安装:sudo apt-get install libexpat1-dev

apr & apr-util 编译

准备工作

1
2
3
4
5
6
7
tar -zxvf apr-util-1.6.1.tar.gz
tar -zxvf apr-1.6.5.tar.gz
tar -zxvf httpd-2.4.38.tar.gz

# 一定要复制到httpd的srclib目录下,且目录版本号去掉
cp -r apr-1.6.5 httpd-2.4.38/srclib/apr
cp -r apr-util-1.6.1 httpd-2.4.38/srclib/apr-util

apr 编译

1
2
3
4
5
6
7
8
# apr 编译
cd httpd-2.4.38/srclib/apr

# 指定编译目录
./configure --prefix=/usr/local/apr

# 编译安装
sudo make & sudo make install

apr-util 编译

1
2
3
4
5
6
7
8
# apr 编译
cd httpd-2.4.38/srclib/apr-util

# 指定编译目录 并 指定apr依赖
./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr

# 编译安装
sudo make & sudo make install

httpd 编译

1
2
3
4
5
6
7
cd httpd-2.4.38

# 指定编译目录 并 指定apr、apr-util、pcre依赖
./configure --prefix=/usr/local/apache-httpd --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --with-pcre=/usr/local/pcre

# 编译安装(这个过程比较长,耐心等待)
sudo make & sudo make install
  • 错误2
1
recipe for target 'htpasswd' failed
  • 解决

是因为apr、apr-util源码没有在httpd-2.4.38/srclib/目录下,cp过去就可以(注意:只需目录名,无需版本号)

测试安装成功

  • ApacheBench 测试
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# 发现已经有了 -s 参数
/usr/local/apache-httpd/bin/ab -h

Usage: ./ab [options] [http[s]://]hostname[:port]/path
Options are:
-n requests Number of requests to perform
-c concurrency Number of multiple requests to make at a time
-t timelimit Seconds to max. to spend on benchmarking
This implies -n 50000
-s timeout Seconds to max. wait for each response
Default is 30 seconds
-b windowsize Size of TCP send/receive buffer, in bytes
-B address Address to bind to when making outgoing connections
-p postfile File containing data to POST. Remember also to set -T
-u putfile File containing data to PUT. Remember also to set -T
-T content-type Content-type header to use for POST/PUT data, eg.
'application/x-www-form-urlencoded'
Default is 'text/plain'
-v verbosity How much troubleshooting info to print
-w Print out results in HTML tables
-i Use HEAD instead of GET
-x attributes String to insert as table attributes
-y attributes String to insert as tr attributes
-z attributes String to insert as td or th attributes
-C attribute Add cookie, eg. 'Apache=1234'. (repeatable)
-H attribute Add Arbitrary header line, eg. 'Accept-Encoding: gzip'
Inserted after all normal header lines. (repeatable)
-A attribute Add Basic WWW Authentication, the attributes
are a colon separated username and password.
-P attribute Add Basic Proxy Authentication, the attributes
are a colon separated username and password.
-X proxy:port Proxyserver and port number to use
-V Print version number and exit
-k Use HTTP KeepAlive feature
-d Do not show percentiles served table.
-S Do not show confidence estimators and warnings.
-q Do not show progress when doing more than 150 requests
-l Accept variable document length (use this for dynamic pages)
-g filename Output collected data to gnuplot format file.
-e filename Output CSV file with percentages served
-r Don't exit on socket receive errors.
-m method Method name
-h Display usage information (this message)
-I Disable TLS Server Name Indication (SNI) extension
-Z ciphersuite Specify SSL/TLS cipher suite (See openssl ciphers)
-f protocol Specify SSL/TLS protocol
(TLS1, TLS1.1, TLS1.2 or ALL)
-E certfile Specify optional client certificate chain and private key
  • 服务测试
1
2
3
4
5
6
7
8
9
10
11
sudovim /usr/local/apache-httpd/conf/httpd.conf

# 添加ServerName
ServerName localhost:80

sudo /usr/local/apache-httpd/bin/apachectl start

curl localhost
<html><body><h1>It works!</h1></body></html>

sudo /usr/local/apache-httpd/bin/apachectl stop

转载请注明出处:https://github.com/imperio-wxm


Thank you for your support.