升东运维是一家专业从事Linux/Freebsd Unix/Windows平台网站维护业务的公司,专业提供Linux(CentOS Redhat Ubuntu等),Unix(Freebsd),Nginx,Apache等系统及网站维护,七年从业经验

Archive for the ‘Apache’ Category

Apache2.4重大更新版发布(附压力测试)

星期三, 二月 22nd, 2012

Apache2.4今天发布,官方描述其为一次重大更新,吸取了Nginx的一些设计:

性能提升,内存利用,异步I/O的支持,动态反向代理设置,与时间驱动的Web服务器相当或更好的性能,更强大的处理资源分配能力,更友好的缓存支持,可定制的高速服务器和代理

下文在CentOS5.7 x64上安装成功Apache2.4

最近两年Nginx蚕食了apache不少份额,目前看来Apache已经坐不住了,本文将对apache2.4做一次简单测试,看看相较Nginx性能如何, (测试后面贴出,实测apache还是不行啊…)

安装步骤:

如果之前安装过httpd或者yum方式安装了apr apr-util需要卸载

yum remove apr apr-util httpd

apr,apr-util下载地址http://mirror.bjtu.edu.cn/apache//apr/apr-1.4.6.tar.gz

http://mirror.bjtu.edu.cn/apache//apr/apr-util-1.4.1.tar.gz

apache2.4 下载地址 http://mirror.bjtu.edu.cn/apache//httpd/httpd-2.4.1.tar.gz

tar -zxvf apr.tar.gz

tar -zxvf apr-util.tar.gz

tar -zxvf  httpd-2.4.1.tar.gz

# 编译和安装 apr 1.4.6

cd apr
./configure –prefix=/usr/local/apr-httpd/
make
make install

# 编译和安装 apr-util 1.4.6

cd ../apr-util
./configure –prefix=/usr/local/apr-util-httpd/ –with-apr=/usr/local/apr-httpd/
make
make install

# 编译 httpd

cd ../httpd-2.4.1
./configure –with-apr=/usr/local/apr-httpd/ –with-apr-util=/usr/local/apr-util-httpd/

 

完成后httpd的目录为  /usr/local/apache/bin/httpd

配置目录  /usr/local/apache/conf/

 

为了使用简便,我们建立一个Apache的启动脚本,把Apache加入系统SysV服务。

[编辑]建立启动脚本
# vi /etc/rc.d/init.d/httpd

将下列代码粘贴到httpd文件中:

#!/bin/bash

# Startup script for the Apache2.0.X Web Server
# Fixed by Comsenz – Nanu ([email protected])

# chkconfig: – 85 15

# Source function library.
. /etc/rc.d/init.d/functions

if [ -f /etc/sysconfig/httpd ]; then
. /etc/sysconfig/httpd
fi

INITLOG_ARGS=””

apachectl=/usr/local/apache2/bin/apachectl
httpd=${HTTPD-/usr/local/apache2/bin/httpd}
prog=httpd
RETVAL=0

start() {
echo -n $”Starting $prog: ”
daemon $httpd $OPTIONS
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch /var/lock/subsys/httpd
return $RETVAL
}
stop() {
echo -n $”Stopping $prog: ”
killproc $httpd
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f /var/lock/subsys/httpd /var/run/httpd.pid
}
reload() {
echo -n $”Reloading $prog: ”
killproc $httpd -HUP
RETVAL=$?
echo
}

case “$1″ in
start)
start
;;
stop)
stop
;;
status)
status $httpd
RETVAL=$?
;;
restart)
stop
start
;;
condrestart)
if [ -f /var/run/httpd.pid ] ; then
stop
start
fi
;;
reload)
reload
;;
graceful|help|configtest|fullstatus)
$apachectl [email protected]
RETVAL=$?
;;
*)
echo $”Usage: $prog

{start|stop|restart|condrestart|reload|status|fullstatus|graceful|help|configtest}”
exit 1
esac

exit $RETVAL

注意文件中的

apachectl=/usr/local/apache2/bin/apachectl
httpd=${HTTPD-/usr/local/apache2/bin/httpd}

两行,如果您的Apache不是安装在/usr/local/apache2,请根据情况更改以上两行中Apache的路径!

授予httpd运行权限

# chmod 755 /etc/rc.d/init.d/httpd

[编辑]将httpd加入系统SysV服务并设置其开机自启动

# chkconfig –add httpd
# chkconfig –level 3 httpd on

这样,今后如需启动、停止、重启Apache就可以用以下方式了:

# service httpd start
# service httpd stop

测试环境:

本机:酷睿2 7500 笔记本 Fedora16 x64

目标机器:奔腾4 单核2.8G CentOS x64 5.7

Apache2.4.1 纯静态index.html (以下测试CPU最大占用 不超过50%)

[[email protected] webbench-1.5]# webbench -c 100 -t 10 http://10.0.0.2/index.html

Webbench – Simple Web Benchmark 1.5
Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.

Benchmarking: GET http://10.0.0.2/index.html
100 clients, running 10 sec.

Speed=211098 pages/min, 1013328 bytes/sec.
Requests: 35183 susceed, 0 failed.

大约是Nginx的一半

[[email protected] webbench-1.5]# webbench -c 500 -t 10 http://10.0.0.2/index.html

Webbench – Simple Web Benchmark 1.5
Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.

Benchmarking: GET http://10.0.0.2/index.html
500 clients, running 10 sec.

Speed=191322 pages/min, 918604 bytes/sec.
Requests: 31887 susceed, 0 failed.

[[email protected] webbench-1.5]# webbench -c 1000 -t 10 http://10.0.0.2/index.html

Webbench – Simple Web Benchmark 1.5
Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.

Benchmarking: GET http://10.0.0.2/index.html
1000 clients, running 10 sec.

Speed=187194 pages/min, 899078 bytes/sec.
Requests: 31199 susceed, 0 failed.
[[email protected] webbench-1.5]# webbench -c 5000 -t 10 http://10.0.0.2/index.html
Webbench – Simple Web Benchmark 1.5
Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.

Benchmarking: GET http://10.0.0.2/index.html
5000 clients, running 10 sec.

Speed=171432 pages/min, 822931 bytes/sec.
Requests: 28572 susceed, 0 failed.
[[email protected] webbench-1.5]# webbench -c 10000 -t 10 http://10.0.0.2/index.html
Webbench – Simple Web Benchmark 1.5
Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.

Benchmarking: GET http://10.0.0.2/index.html
10000 clients, running 10 sec.

Speed=173844 pages/min, 836064 bytes/sec.
Requests: 28974 susceed, 0 failed.

 

[[email protected] webbench-1.5]# webbench -c 20000 -t 10 http://10.0.0.2/index.html(用时大大超过10秒)
Webbench – Simple Web Benchmark 1.5
Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.

Benchmarking: GET http://10.0.0.2/index.html
20000 clients, running 10 sec.

Speed=110328 pages/min, 530985 bytes/sec.
Requests: 18388 susceed, 0 failed.

后面的(30000并发)我的电脑已经承受不了啦

Nginx1.1.12

[[email protected] ~]$ webbench -c 100 -t 10 http://10.0.0.2:81/index.html
Webbench – Simple Web Benchmark 1.5
Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.

Benchmarking: GET http://10.0.0.2:81/index.html
100 clients, running 10 sec.

Speed=408702 pages/min, 1689152 bytes/sec.
Requests: 68117 susceed, 0 failed.

[[email protected] ~]$ webbench -c 200 -t 10 http://10.0.0.2:81/index.html
Webbench – Simple Web Benchmark 1.5
Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.

Benchmarking: GET http://10.0.0.2:81/index.html
200 clients, running 10 sec.

Speed=404562 pages/min, 1672140 bytes/sec.
Requests: 67427 susceed, 0 failed.

更大连接数我机器的webbench 测试这个之后就挂了,再跑apache也不行

 

暂时先测试到这里,动态不测了(如果都用FPM,反向服务器只是传递一下,没太大比较价值),只测试静态页面处理,apache还是不如nginx处理快,稍后再研究下。

本文地址:http://www.ppkj.net/?p=1069

apache重写,匹配主机头

星期四, 六月 2nd, 2011

#匹配主机头是否含有wap,(.*)表示后面任意,比如.ppkj.net,[NC]表示忽略大小写
RewriteCond %{HTTP_HOST} ^wap(.*)$ [NC]
#将根目录的访问定向到wap.php
RewriteRule ^$ wap\.php

RewriteCond %{HTTP_HOST} ^wap(.*)$ [NC]
#将http://wap.ppkj.net/login/中的login匹配到$1,$1代表第一个括号的内容,第二个是$2
#[QSA]表示保留原有的query_string参数 比如?a=1&b=2
RewriteRule ^(.*)\/$ wap\.php\?part=$1[QSA]

apache 重写 补全url后缀/

星期四, 六月 2nd, 2011

实现的效果

http://www.ppkj.net/abc

http://www.ppkj.net/abc/

RewriteEngine On
RewriteBase /

#排除文件
RewriteCond %{REQUEST_FILENAME} !-f

#排除已经带/的
RewriteCond %{REQUEST_URI} !(.*)/$

#301永久重定向
RewriteRule ^(.*)$ /$1/ [L,R=301]

apache开启gzip

星期三, 四月 27th, 2011

如果要开启gzip的话,一定要打开下面二个模块.

编辑 /etc/httpd.conf (Linux),开启这两个模块(如果没开启的话)

LoadModule headers_module modules/mod_headers.so
LoadModule deflate_module modules/mod_deflate.so

设置压缩比率,取值范围在 1(最低) 到 9(最高)之间,不建议设置太高,虽然有很高的压缩率,但是占用更多的CPU资源.

下面的文件放在

httpd.conf 最下面

DeflateCompressionLevel 3
AddOutputFilter DEFLATE html xml php js css

SetOutputFilter DEFLATE
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
SetEnvIfNoCase Request_URI \\.(?:gif|jpe?g|png)$ no-gzip dont-vary
SetEnvIfNoCase Request_URI .(?:exe|t?gz|zip|bz2|sit|rar)$ no-gzip dont-vary
SetEnvIfNoCase Request_URI .(?:pdf|mov|avi|mp3|mp4|rm)$ no-gzip dont-vary
#Header append Vary User-Agent env=!dont-vary #对代理的设置

禁止apache 404错误页显示服务器及操作系统版本

星期五, 一月 28th, 2011

ServerTokens OS  ← 找到这一行,将“OS”改为“Prod”(在出现错误页的时候不显示服务器操作系统的名称)

ServerTokens Prod   ← 变为此状态

ServerSignature On  ← 找到这一行,将“On”改为“Off”

ServerSignature Off  ← 在错误页中不显示Apache的版本

Redhat6发布了

星期二, 十一月 30th, 2010

才看到redhat6发布了,CentOS估计也要在一两个月内跟进了,貌似改进很大,不然红帽不会宣称十年磨一剑,CentOS6出来再试试吧