安泰信息

2008年12月16日

apache静态文件压缩,提高网络流量

归档在: apache, jboss — JACKEYJ @ 4:28 PM

    本文主要介绍如何通过对页面进行压缩从而节省网站的带宽以及提升用户的访问速度。

网站的访问速度是由多个因素所共同决定的,这些因素例如应用程序的响应速度、网络带宽、服务器性能、与客户端之间的网络传输速度等等。其中最重要的一个因素是应用程序本身的响应速度,因此当你为网站性能所苦恼时,你第一个需要着手进行处理的便是尽可能的提升应用程序的执行速度,你可以使用缓存或者是优化代码的执行效率来提升应用程序的速度。

但是,本文并不是介绍如何来提升应用程序的执行效率,前面提到的只不过是为了防止您病急乱投医。在确保您的应用程序的性能已经达到足够好,同时服务器的性能也完全满足的情况下,不妨来试试网页压缩来进一步提升网页的浏览速度,而且非常重要的是,它完全不需要任何的成本,只不过是会让您的服务器CPU 占用率稍微提升一两个百分点而已或者更少。

网页压缩是一项由 WEB 服务器和浏览器之间共同遵守的协议,也就是说 WEB 服务器和浏览器都必须支持该技术,所幸的是现在流行的浏览器都是支持的,包括 IE、FireFox、Opera 等;服务器有 Apache 和 IIS 等。双方的协商过程如下:

   1. 首先浏览器请求某个 URL 地址,并在请求的头 (head) 中设置属性 accept-encoding 值为 gzip, deflate,表明浏览器支持 gzip 和 deflate 这两种压缩方式(事实上 deflate 也是使用 gzip 压缩协议,下面我们会介绍二者之间的区别);
   2. WEB 服务器接收到请求后判断浏览器是否支持压缩,如果支持就传送压缩后的响应内容,否则传送不经过压缩的内容;
   3. 浏览器获取响应内容后,判断内容是否被压缩,如果是则解压缩,然后显示响应页面的内容。

在实际的应用中我们发现压缩的比率往往在 3 到 10 倍,也就是本来 50k 大小的页面,采用压缩后实际传输的内容大小只有 5 至 15k 大小,这可以大大节省服务器的网络带宽,同时如果应用程序的响应足够快时,网站的速度瓶颈就转到了网络的传输速度上,因此内容压缩后就可以大大的提升页面的浏览速度。

接下来我们介绍几种常用的环境下如何启用网页压缩功能。

纯 Tomcat 服务器

如果您的 WEB 应用程序是跑在 Tomcat 服务器下的,而且直接使用 Tomcat 所提供的 HTTP 服务,那建议你马上动手,因为实在是太简单了,你只需要在 server.xml 配置文件中给 HTTP Connector 增加一个 compression 的参数值为 on 并重启 Tomcat 服务器就立刻生效,配置如下:

    <Connector port=”8080″ protocol=”HTTP/1.1″
               maxThreads=”150″ connectionTimeout=”20000″
               redirectPort=”8443″ compression=”on”/>

Tomcat 采用的是 HTTP/1.1 的 GZIP 压缩协议,它会根据浏览器送过来的请求中的 accept-encoding 值是否包含 gzip 来判断浏览器是否支持 gzip 压缩协议,如果浏览器支持就启用 gzip 压缩,否则就不进行任何压缩处理。Tomcat 中还有另外一个参数 compressableMimeType,这个参数可以用来指定压缩哪种类型的内容,例如可以指定该配置值为:text/html,text /plain ,则只压缩 contentType 为 text/html 和 text/plain 的页面,不过您最好也将 css 和 javascript 文件也算在压缩的文件类型中,因为这两者的压缩效果也十分的明显。

    回页首

Apache 服务器

在 apache 1.3 版本,大家常用 mod_gzip 对输出内容进行压缩,现在主流的浏览器都支持 gzip 解压缩。在 apache2 下,这个模块换名为 mod_deflate,对应的模块文件名是 mod_deflate.so。mod_gzip 本文不做介绍,下面描述一下在 Apache 2 下如何启用并配置 mod_deflate 模块。默认安装的 Apache 不管是 Windows 还是 Linux/Unix,都是不启用该模块的, Linux/Unix 下甚至不带该模块,你需要手工编译这个模块。

下面我们分别介绍在 Windows 和 Linux 操作系统下如何启用并配置 mod_deflate 模块。

在 Windows 下采用安装程序安装的 Apache 服务器已经带有 deflate 所需要的模块 mod_deflate.so 和 mod_headers.so,我们只需要在 httpd.conf 配置文件中启用并进行相关的配置即可,配置如下:

LoadModule deflate_module modules/mod_deflate.so
LoadModule headers_module modules/mod_headers.so
<Location />
# Insert filter
SetOutputFilter DEFLATE
# 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 \bMSIE !no-gzip !gzip-only-text/html
# NOTE: Due to a bug in mod_setenvif up to Apache 2.0.48
# the above regex won’t work. You can use the following
# workaround to get the desired effect:
BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html
# Don’t compress images
SetEnvIfNoCase Request_URI .(?:gif|jpe?g|png)$ no-gzip dont-vary
# Make sure proxies don’t deliver the wrong content
Header append Vary User-Agent env=!dont-vary
</Location>

而如果是 Linux/Unix 操作系统,如果你没有在编译安装的过程中将所需要的两个模块 mod_deflate 和 mod_headers 编译进去的话,那就稍微有点麻烦,首先我们先看如何在编译安装 Apache 过程中也同时编译这两个模块,请在执行 configure 程序时增加两个参数分别是:

# ./configure –enable-deflate –enable-headers

这样在编译完 Apache 后就可以直接在 httpd.conf 中启用并配置 deflate 模块了,配置的方法跟 Windows 平台下是相同的。

如果说您的 Apache 已经在运行了,不想再重新编译一次,那也可以选择只编译 deflate 模块所需的文件 mod_deflate.c 和 mod_headers.c。这两个文件位于 {apache-src}/modules/filters/ 目录下(其中 {apache-src} 为 apache 源文件所在的目录)。使用如下命令来单独编译这两个源文件。

# {apache-bin}/apxs -i -a -c {apache-src}/modules/filters/mod_deflate.c
# {apache-bin}/apxs –i –a –c {apache-src}/modules/filters/mod_headers.c

其中 {apache-bin} 为 Apache 安装目录下的 bin 目录,接下来在 httpd.conf 直接配置该模块即可。

很多时候你在单独编译 deflate 模块的时候可能会碰到编译错误,提示是:

Cannot load /opt/apache/modules/mod_deflate.so into server: /opt/apache/modules/mod_deflate.so: undefined symbol: deflate

解决的方法如下:

编辑 /usr/local/apache2/bin/apr-config 文件修改其中的 LDFLAGS 值为 “-lz”,然后再重新编译 mod_deflate 模块,apxs -ica mod_deflate.c 即可。

为了省却不必要的麻烦,请尽量在编译安装时直接加上 –enable-deflate –enable-headers 参数。

我的实施内容:

#run cmd add mod to apache
/infowarelab/apache2/bin/apxs -i -a -c /infowarelab/src/httpd-2.2.3/modules/filters/mod_deflate.c
/infowarelab/apache2/bin/apxs -i -a -c /infowarelab/src/httpd-2.2.3/modules/metadata/mod_headers.c

#add conf in httpd.conf

<IfModule mod_deflate.c>
 DeflateCompressionLevel 7
 AddOutputFilterByType DEFLATE text/html text/plain text/xml text/javascript text/css application/x-httpd-php
 AddOutputFilter DEFLATE css js
</IfModule>

参考文档:
http://www.ibm.com/developerworks/cn/web/wa-lo-webcompress/
http://www.hbcms.org.cn/cms/9d/1322.html

2008年11月18日

Optimal mod_jk configuration

归档在: Linux, apache, jboss — JACKEYJ @ 9:03 AM

Optimal mod_jk configuration

 

There are many potential problems associated with the default configuration of mod_jk.  Let’s say it is perfectly adequate for a very low traffic website, but when pushing any moderate to high load to mod_jk, there will be connection problems.  This is not due to any bug in mod_jk whatsoever, however, it is because the default configuration makes no assumption about your existing hardware or potential load, so, therefore, it is not tuned accordingly.

 

Note that the configuration recommendations here are optimal as a base configuration to avoid many of the common problems users experience with mod_jk. There exist many other useful optimizations, but these depend on the environment and web application in use. See http://tomcat.apache.org/connectors-doc/reference/workers.html for details on all available mod_jk properties.

 

Let’s take a look at a typical default configuration for Apache/Tomcat/mod_jk:
workers.properties

worker.list=loadbalancer,status

worker.node1.port=8009
worker.node1.host=node1.mydomain.com
worker.node1.type=ajp13
worker.node1.lbfactor=1

worker.node2.port=8009
worker.node2.host= node2.mydomain.com
worker.node2.type=ajp13
worker.node2.lbfactor=1

worker.loadbalancer.type=lb
worker.loadbalancer.balance_workers=node1,node2

worker.status.type=status

 
JBoss Web’s (Tomcat) server.xml AJP snippet:

<Connector port=”8009″ address=”${jboss.bind.address}” protocol=”AJP/1.3″
         emptySessionPath=”true” enableLookups=”false” redirectPort=”8443″ ></Connector>

 
Apache’s httpd.conf:

<IfModule prefork.c>
StartServers       8
MinSpareServers    5
MaxSpareServers   20
ServerLimit      256
MaxClients       256
MaxRequestsPerChild  4000
</IfModule>

 

 

The above configuration, under load, may cause mod_jk to be very slow and unresponsive, cause http errors, and cause half-closed connections.   These problems can arise because there are no connection timeouts specified to take care of orphaned connections, no error handling properties defined in workers.properties, and no connection limits set in Apache and Tomcat.

 

 

First off, lets take care of Tomcat:

 
Configuring server.xml:

 

The main concern with server.xml is setting the connectionTimeout which

sets the SO_TIMEOUT of the underlying socket.  So when a connection in

Tomcat hasn’t had a request in the amount of time specified by

connectionTimeout, then the connection dies off.  This is necessary because if the connection hasn’t been used for a certain period of

time then there is the chance that it is half-close on the mod_jk end.

If the connection isn’t closed there will be an inflation of threads

which can over time hit the maxThreads count in Tomcat then Tomcat will

not be able to accept any new connections.  A connectionTimeout of 600000 (10 minutes) is a good number to start out with.  There may be a situation where the connections are not being recycled fast enough, in this instance the connectionTimeout could be lowered to 60000 or 1 minute.

 

When setting connectionTimeout in Tomcat, mod_jk should also have

connect_timeout/prepost_timeout set, which allows detection that the

Tomcat connection has been closed and preventing a retry request.

 

The recommended value of maxThreads is 200 per CPU, so here we assume the server is a single core machine.  If it has been quad core, we could push that value to 800, and more depending on RAM and other machine specs.

 

<Connector port=”8009″
           address=”${jboss.bind.address}”
           emptySessionPath=”true”
           enableLookups=”false”
           redirectPort=”8443″
           protocol=”AJP/1.3″
           maxThreads=”200″
           connectionTimeout=”600000″></Connector>

 

 

 

 
Configuring workers.properties:

 

See comments inline.

 

worker.list=loadbalancer,status

worker.template.port=8009
worker.template.type=ajp13
worker.template.lbfactor=1
worker.template.ping_timeout=1000
#ping_mode was introduced in 1.2.27, if not using 1.2.27 please specify connect_timeout=10000 and prepost_timeout=10000 as an alternative

worker.template.ping_mode=A
worker.template.socket_timeout=10
#It is not necessary to specify connection_pool_timeout if you are running the worker mpm
worker.connection_pool_timeout=600

#Referencing the template worker properties makes the workers.properties shorter and more concise
worker.node1.reference=worker.template
worker.node1.host=192.168.1.2

worker.node2.reference=worker.template
worker.node2.host=192.168.1.3

worker.loadbalancer.type=lb
worker.loadbalancer.balance_workers=node1,node2

worker.status.type=status

 

The key points in the above workers.properties is we’ve added limits for the connections mod_jk makes.  With the base configuration, socket timeouts default to infinite. The other important properties are ping_mode and ping_timeout which handle probing a connection for errors and connection_pool_timeout which must be set to equal server.xml’s connectionTimeout when using the prefork mpm.  When these two values are the same, after a connection has been inactive for x amount of time, the connection in mod_jk and Tomcat will be closed at the same time, preventing a half-closed connection.

 

 

 
Configuring Apache

 

Make note that maxThreads for the AJP connection should coincide with

the MaxClients set in Apache’s httpd.conf.  MaxClients needs to be set

in the correct module in Apache.

 

 

This can be determined by running httpd -V:

 

# httpd -V

Server version: Apache/2.2.3
Server built:   Sep 11 2006 09:43:05
Server’s Module Magic Number: 20051115:3
Server loaded:  APR 1.2.7, APR-Util 1.2.8
Compiled using: APR 1.2.7, APR-Util 1.2.7
Architecture:   32-bit
Server MPM:     Prefork
  threaded:     no
    forked:     yes (variable process count)
Server compiled with….
-D APACHE_MPM_DIR=”server/mpm/prefork”
-D APR_HAS_SENDFILE
-D APR_HAS_MMAP
-D APR_HAVE_IPV6 (IPv4-mapped addresses enabled)
-D APR_USE_SYSVSEM_SERIALIZE
-D APR_USE_PTHREAD_SERIALIZE
-D SINGLE_LISTEN_UNSERIALIZED_ACCEPT
-D APR_HAS_OTHER_CHILD
-D AP_HAVE_RELIABLE_PIPED_LOGS
-D DYNAMIC_MODULE_LIMIT=128
-D HTTPD_ROOT=”/etc/httpd”
-D SUEXEC_BIN=”/usr/sbin/suexec”
-D DEFAULT_PIDLOG=”logs/httpd.pid”
-D DEFAULT_SCOREBOARD=”logs/apache_runtime_status”
-D DEFAULT_LOCKFILE=”logs/accept.lock”
-D DEFAULT_ERRORLOG=”logs/error_log”
-D AP_TYPES_CONFIG_FILE=”conf/mime.types”
-D SERVER_CONFIG_FILE=”conf/httpd.conf”

 

Which tells me the Server MPM is Prefork.  This is not always 100% accurate so you should also view the output of /etc/sysconfig/httpd to see if the following line is there: HTTPD=/usr/sbin/httpd.worker.  If it is commented out you are running prefork, otherwise if uncommented worker.

 

httpd.conf:

<IfModule prefork.c>
StartServers       8
MinSpareServers    5
MaxSpareServers   20
MaxClients       200
MaxRequestsPerChild  0
</IfModule>

 

Or if Apache is using worker, it is
<IfModule worker.c>
StartServers         2
MaxClients         200
MinSpareThreads     25
MaxSpareThreads     75
ThreadsPerChild     25
MaxRequestsPerChild  0
</IfModule>

 

MaxRequestsPerChild is 0, this is the recommended value when using

mod_jk as mod_jk keeps open persistent connections.  The key values in

the above configuration are MaxClients and MaxRequestsPerChild, the rest

of the values are left as default.  Note that MaxRequestsPerChild is

recommended to be 0 however the value may need to be greater than 0

depending on if Apache is used for other modules also, especially in the

case of resource leakage.

 

 
Advanced worker-mpm Configuration

 

To get the most out of your mod_jk setup you should be using Apache’s worker mpm which provides a definite performance improvement over the prefork mpm.  The following section will detail how to configure Apache/mod_jk/Tomcat with the worker mpm and the math behind the configuration.

 

Let’s start out with the worker mpm configuration

<IfModule mpm_worker_module>

ThreadLimit 100
StartServers 5
MaxClients 1000
MinSpareThreads 100
MaxSpareThreads 1000
ThreadsPerChild 100
MaxRequestsPerChild 0
</IfModule>

 

The optimal configuration completely depends on the hardware being used and the load requirements.  But a general rule of thumb, keep processes low and thread count high.  To determine the number of processes Apache will use simply divide MaxClients by ThreadPerChild.  So in this case MaxClients (1000) / ThreadsPerChild (100) = Processes (10), so Apache will allocate a maximum of 100 threads per each 10 child processes resulting in a total of 1000 possible clients.

 

Now to translate this to mod_jk, mod_jk maintains a connection pool for each worker defined in workers.properties.  By default with Apache mod_jk sets connection_pool_size to ThreadsPerChild, so in the above case that would translate to 100, giving 1000 possible connections to JBoss.  This may or may not be desired.

 

Let’s take a common example, there will be 3 JBoss servers that combined needed to be able to handle 900 concurrent connections

 

worker.list=loadbalancer,status
 

worker.template.type=ajp13
worker.template.port=8009
worker.template.ping_mode=A
worker.template.connection_pool_size=30
worker.template.socket_timeout=10
worker.template.retries=20
 

worker.node1.reference=worker.template
worker.node1.host=192.168.0.101

worker.node2.reference=worker.template
worker.node2.host=192.168.0.102

worker.node3.reference=worker.template
worker.node3.host=192.168.0.103
 

worker.loadbalancer.type=lb
worker.loadbalancer.balance_workers=node1,node2, node3
worker.sticky_session=True

worker.status.type=status

 

The above configuration tells mod_jk to multiplex 30 connections to the available Apache processes, which is 10 processes.  So that means 30 connections multiplexed over 10 processes gives 300 possible connections to each backend worker.  Furthermore, the total connections able to be used in this configuration from Apache is 900 which means  100 connections will be left over for static content or whatnot.

 

 

Next configure maxThreads in each ajp connector to match the above.

 

Node 1 ajp connector:

<Connector port=”8009″
           address=”${jboss.bind.address}”
           emptySessionPath=”true”
           enableLookups=”false”
           redirectPort=”8443″
           protocol=”AJP/1.3″
           maxThreads=”300″
           connectionTimeout=”600000″></Connector>

 

Node 2 ajp connector:

<Connector port=”8009″
           address=”${jboss.bind.address}”
           emptySessionPath=”true”
           enableLookups=”false”
           redirectPort=”8443″
           protocol=”AJP/1.3″
           maxThreads=”300″
           connectionTimeout=”600000″></Connector>

 

Node 3 ajp connector:

<Connector port=”8009″
           address=”${jboss.bind.address}”
           emptySessionPath=”true”
           enableLookups=”false”
           redirectPort=”8443″
           protocol=”AJP/1.3″
           maxThreads=”300″
           connectionTimeout=”600000″></Connector>

 

Remember when using connectionTimeout which is always recommended, prepost_timeout and connect_timeout also need to be set, which is done.  I’m not showing sticky session configuration, but that covered in the main mod_jk article in using mod_jk with JBoss.

 reference from :http://www.jboss.org/community/docs/DOC-11543

UsingMod_jk1.2WithAFirewall

归档在: Linux, apache, jboss — JACKEYJ @ 8:38 AM

Setting up mod_jk with a firewall:

 
Configuring workers.properties:

# Define list of workers that will be used
# for mapping requests
# The configuration directives are valid
# for the mod_jk version 1.2.18 and later
#
worker.list=loadbalancer,status

# Define Node1
# modify the host as your host IP or DNS name.
worker.node1.port=8009
#Using an IP prevents a DNS lookup
worker.node1.host=192.168.1.2
worker.node1.type=ajp13
worker.node1.lbfactor=1
worker.node1.connect_timeout=10000
worker.node1.prepost_timeout=10000
worker.node1.socket_keepalive=True

# Define Node2
# modify the host as your host IP or DNS name.
worker.node2.port=8009
worker.node2.host=192.168.1.3
worker.node2.type=ajp13
worker.node2.lbfactor=1
worker.node2.connect_timeout=10000
worker.node2.prepost_timeout=10000
worker.node2.socket_keepalive=True

# Load-balancing behaviour
worker.loadbalancer.type=lb
worker.loadbalancer.balance_workers=node1,node2

# Status worker for managing load balancer
worker.status.type=status

 

 

socket_keepalive=true is the most important setting. connect_timeout and

prepost_timeout are to “work-around” firewalls that

ignore keepalives or close the connection for unknown reasons.

 

 

 
Configuring server.xml:

The main concern with server.xml is setting the connectionTimeout which

sets the SO_TIMEOUT of the underlying socket.  So when a connection in

Tomcat hasn’t had a request in the amount of time specified by

connectionTimeout, then the connection dies off.  Why is this a good

thing?…because if the connection hasn’t been used for a certain period of

time then there is the chance that it is half-close on the mod_jk end.

If the connection isn’t closed there will be an inflation of threads

which can over time hit the maxThreads count in Tomcat then Tomcat will

not be able to accept any new connections.

 

When setting connectionTimeout in Tomcat, mod_jk should also have

connect_timeout/prepost_timeout set, which allows detection that the

Tomcat connection has been closed and preventing a retry request.

 

 

<Connector port=”8009″
           address=”${jboss.bind.address}”
           emptySessionPath=”true”
           enableLookups=”false”
           redirectPort=”8443″
           protocol=”AJP/1.3″
           maxThreads=”200″
           connectionTimeout=”60000″></Connector>

 
Configuring Apache

 

Make note that maxThreads for the AJP connection should coincide with

the MaxClients set in Apache’s httpd.conf.  MaxClients needs to be set

in the correct module in Apache. 

 

 

This can be determined by running httpd -V:

 

# httpd -V

Server version: Apache/2.2.3
Server built:   Sep 11 2006 09:43:05
Server’s Module Magic Number: 20051115:3
Server loaded:  APR 1.2.7, APR-Util 1.2.8
Compiled using: APR 1.2.7, APR-Util 1.2.7
Architecture:   32-bit
Server MPM:     Prefork
  threaded:     no
    forked:     yes (variable process count)
Server compiled with….
-D APACHE_MPM_DIR=”server/mpm/prefork”
-D APR_HAS_SENDFILE
-D APR_HAS_MMAP
-D APR_HAVE_IPV6 (IPv4-mapped addresses enabled)
-D APR_USE_SYSVSEM_SERIALIZE
-D APR_USE_PTHREAD_SERIALIZE
-D SINGLE_LISTEN_UNSERIALIZED_ACCEPT
-D APR_HAS_OTHER_CHILD
-D AP_HAVE_RELIABLE_PIPED_LOGS
-D DYNAMIC_MODULE_LIMIT=128
-D HTTPD_ROOT=”/etc/httpd”
-D SUEXEC_BIN=”/usr/sbin/suexec”
-D DEFAULT_PIDLOG=”logs/httpd.pid”
-D DEFAULT_SCOREBOARD=”logs/apache_runtime_status”
-D DEFAULT_LOCKFILE=”logs/accept.lock”
-D DEFAULT_ERRORLOG=”logs/error_log”
-D AP_TYPES_CONFIG_FILE=”conf/mime.types”
-D SERVER_CONFIG_FILE=”conf/httpd.conf”

 

Which tells me the Server MPM is Prefork

 

httpd.conf:

<IfModule prefork.c>
StartServers       8
MinSpareServers    5
MaxSpareServers   20
MaxClients       200
MaxRequestsPerChild  0
</IfModule>

 

Or if Apache is using worker, it is
<IfModule worker.c>
StartServers         2
MaxClients         200
MinSpareThreads     25
MaxSpareThreads     75
ThreadsPerChild     25
MaxRequestsPerChild  0
</IfModule>

 

MaxRequestsPerChild is 0, this is the recommended value when using

mod_jk as mod_jk keeps open persistent connections.  The key values in

the above configuration are MaxClients and MaxRequestsPerChild, the rest

of the values are left as default.  Note that MaxRequestsPerChild is

recommended to be 0 however the value may need to be greater than 0

depending on if Apache is used for other modules also, especially in the

case of resource leakage.

2008年11月06日

apache openssl 制作ssl证书

归档在: apache — JACKEYJ @ 11:41 AM

在centos 4.1/apache2.2.3/openssl0.98
参考:www.viiving.com

步骤1:生成密钥
命令:openssl genrsa 1024 > server.key
说明:这是用128位rsa算法生成密钥,得到server.key文件

步骤2: 生成证书请求
命令:openssl req -new -key server.key > server.csr
说明:这是用步骤1的密钥生成证书请求文件server.csr, 这一步提很多问题,一一输入

步骤2: 生成证书请求
命令:openssl req -x509 -days 365 -key server.key -in server.csr > server.crt
说明:这是用步骤1,2的的密钥和证书请求生成证书server.crt,-days参数指明证书有效期,单位为天

把得到的server.key和server.crt文件拷贝到apache的对应目录

然后修改httpd.conf和httpd-ssl.conf两个文件中的配置就可以了.
在开发和测试过程中使用还是不错的.

2008年09月25日

安泰资讯:主流web server对比

归档在: Linux, apache, ubuntu — JACKEYJ @ 4:58 PM

RoR的部署方式从架构上来说分为前端和后端:

一、前端
前端的作用就是处理静态资源,将动态请求分发到后端,有时候也带有一些额外的功能,例如对特定URL进行rewrite和redirect,对HTTP输出进行gzip压缩等等。

前端目前已知的可以选择apache, lighttpd, litespeed, nginx, haproxy

1、apache2.2
apache是全球市场占有率最高的web server,超过全球互联网网站50%的网站都用apache。apache2.2 + mod_proxy_balancer是一个非常流行,非常稳定的方案。

使用apache2.2唯一的问题就是apache的性能和后面那些轻量级web server相比,差太远了。一方面在处理静态请求方面apache要比lighttpd慢3-5倍,内存消耗和CPU消耗也高出一个数量级,另一方面 mod_proxy_balancer的分发性能也不高,比haproxy差很远。

2、lighttpd
lighttpd是一个轻量级高性能web server,一个在MySQL Inc工作的德国人写的。性能很好,内存和CPU资源消耗很低,支持绝大多数apache的功能,是apache的绝好替代者。目前lighttpd已经 上升到全球互联网第四大web server,市场占有率仅此于apache,IIS和Sun。

lighttpd唯一的问题是proxy功能不完善,因此不适合搭配mongrel来使用。lighttpd下一个版本1.5.0的proxy模块重写过了,将会解决这个问题。

3、litespeed
和lighttpd差不多,商业产品,收费的。比lighttpd来说,多一个web管理界面,不用写配置文件了。litespeed专门为单机 运行的RoR开发了一个lsapi协议,号称性能最好,比httpd和fcgi都要好。他的proxy功能比lighttpd完善。

litespeed的缺点我却认为恰恰是这个lsapi。因为lsapi不是web server启动的时候启动固定数目的ruby进程,而是根据请求繁忙程度,动态创建和销毁ruby进程,貌似节省资源,实则和apache2.2进程模 型一样,留下很大的黑客攻击漏洞。只要黑客瞬时发起大量动态请求,就会让服务器忙于创建ruby进程而导致CPU资源耗尽,失去响应。

当然,litespeed也支持httpd和fcgi,这个和lighttpd用法一样的,到没有这种问题。

4、nginx
一个俄国人开发的轻量级高性能web server,特点是做proxy性能很好,因此被推荐取代apache2.2的mod_proxy_balancer,来和mongrel cluster搭配。其他方面和lighttpd到差不多。

要说缺点,可能就是发展的时间比较短,至今没有正式版本,还是beta版。没有经过足够网站的验证。

5、haproxy
就是一个纯粹的高性能proxy,不处理静态资源的,所有请求统统分发到后端。

二、后端
后端就是跑ruby进程,处理RoR动态请求了。运行后端ruby进程有两种方式:

1、fcgi方式
准确的说,不能叫做fcgi方式,其实就是启动一个ruby进程,让这个ruby进程监听一个tcp/unix socket,以fcgi协议和前端通讯。所以fcgi不是指ruby进程的运行方式,而是ruby进程使用的通讯协议。这就好比你tomcat可以用 http,ajp通讯一样,tomcat自己的运行方式都一样的,只是通讯方式不一样。

fcgi方式启动ruby进程,可以使用lighttpd带的一个spawn-fcgi工具来启动(JavaEye目前采用这种方式)。

值得一提的是,apache2.2的mod_fastcgi的方式也上面还不太一样,由apache动态创建fcgi进程和管理fcgi进程,这 种方式和litespeed的lsapi面临的问题是一样的,此外apache的mod_fastcgi自己也有很多严重的bug,是一种很糟糕的部署方 式。这种糟糕的部署方式也败坏了fcgi的名声。

fastcgi只是一种协议,虽然古老,但并不是不好用,http协议也很古老。没有必要因为apache的mod_fastcgi的运行方式的问题而连带把fastcgi都一同否定了。

2、http方式
也就是用mongrel去跑ruby进程,由于mongrel实际上已经是一个简单的http server,所以也可以单独作为web server使用。mongrel现在越来越受欢迎了。

用fcgi方式还是http方式,我个人觉得区别不大,关键还是看应用的场合,一般而言,推荐的搭配是:

lighttpd + fcgi 或者 nginx +mongrel,而apache因为性能差距,而不被推荐。

JavaEye为什么用lighttpd + fcgi呢?原因如下:

1) lighttpd发展了好几年了,市场占有率也相当高,是一个经过实践检验的server,它的文档也很全;而nginx还没有经过足够的市场检验,文档也很缺乏
2) JavaEye的ruby进程和web server在一台机器上面跑,通过unix socket使用fcgi协议通讯可以避免tcp的网络开销,其通讯速度比使用tcp socket使用http协议通讯要快一些。

什么场合使用haproxy?

大规模部署,例如你的RoR应用到十几台服务器上面去,你用haproxy会更好,可以方便的添加删除应用服务器节点,proxy性能更好。

安泰信息:Debian Linux Apahe2.0.63 JBoss 4.2.2 Java 1.5 集群安装

归档在: Linux, apache, jboss, java, ubuntu — JACKEYJ @ 11:43 AM

Apahe2/JBoss/Java Cluster Guide

1 Download needed software

1.1 Jboss4.2.2 GA

Please go http://www.jboss.org/download/ to find the jboss-4.2.3.GA.zip to download.

 

1.2 Java SE 5.0 updated 16

Please go
https://cds.sun.com/is-bin/INTERSHOP.enfinity/WFS/CDS-CDS_Developer-Site/en_US/-/USD/ViewProductDetail-Start?ProductRef=jdk-1.5.0_16-oth-JPR@CDS-CDS_Developer

And select Platform with Linux, Language with Multi-language, then download
jdk-1_5_0_16-linux-i586.bin.

 

1.3 Apache httpd 2.0.63

Please go http://httpd.apache.org/download.cgi to find httpd-2.0.63.tar.gz to download.

 

1.4 Tomcat Connectors 1.2.9

Please go http://apache.deathculture.net/tomcat/tomcat-connectors/jk/source/jk-1.2.26/ to find tomcat-connectors-1.2.26-src.tar.gz to download

 

2 Java installation

Login debian OS with root user, then create a linux user. E.g ‘deploy’.

Login with ‘deploy’ account, then upload jdk-1_5_0_16-linux-i586.bin to home directory.

chmod 755 jdk-1_5_0_16-linux-i586.bin

./ jdk-1_5_0_16-linux-i586.bin

ln –s jdk-1_5_0_16-linux-i586 jdk

Extract java and link it to jdk as above. Then modify envirement variable on profile.

cd /home/deploy

vi .bash_profile

#add for Jboss and jdk begin

JAVA_HOME=/home/deploy/jdk;export JAVA_HOME

CLASSPATH=./:/home/deploy/jdk/lib/dt.jar:/home/deploy/jdk/lib/tools.jar;export CLASSPATH

PATH=$PATH:/home/deploy/jdk/bin:/home/deploy/jdk/jre/bin;export PATH

alias l=”/bin/ls -al”

export HOME=/home/deploy

export PS1=’$PWD>’

set EDITOR=vi

set -o vi

after modify the .bash_profile and save it, then logout.

3 JBoss installation

Login with deploy account, then upload jboss-4.2.3.GA.zip to home directory.

3.1 Extract Jboss package

unzip jboss-4.2.3.zip

move jboss-4.2.3 jboss

Extract jboss-4.2.3.GA.zip to jboss-4.2.3.GA and rename the directory to jboss.

 

3.2 Copy need LIB and jar to default deployment

If Jboss need to run on cluster, copy some LIB and jar file from its ‘all’ directory to ‘default’ directory. Please execute those commands as follows:

cd ~/jboss/server/default/lib/

cp ~/jboss/server/all/lib/jgroups.jar .

cp ~/jboss/server/all/lib/jbossha.jar .

cd ~/jboss /server/default/deploy

cp -r ~/jboss/server/all/deploy/jboss-web-cluster.sar/ .

cp ~/jboss/server/all/deploy/cluster-service.xml .

cd jboss/server/default/deploy/jboss-web.deployer/META-INF/

 

3.3 Configure cluster parameter

vi jboss-server.xml

Then, for each JBoss Tomcat instance in the cluster, we need to tell it to add the jvmRoute value to its session cookies so that mod_jk can route incoming requests. Locate the <attribute> element with a name of UseJK, and set its value to true line 114 on jboss-server.xml.

 

cd ~/jboss/server/default/deploy/jboss-web.deployer/

vi server.xml

Please find the line as <Engine name=”jboss.web” defaultHost=”localhost”>, and add jvmRoute=”debian01″ to this line, different nodes should different jvmRoute name, result as follows:

<Engine name=”jboss.web” jvmRoute=”debian01″ defaultHost=”localhost”>

 

cd ~/jboss/server/default/deploy

vi cluster-service.xml

Cluster might apply UDP and TCP communication protocol, mask UDP configure node from line 39 to 80, then unmask TCP configure node.

locate line 85, find bind_addr=”thishost” and change thishost to ip address of the current host.

find start_port=”7800″ and change 7800 to 7820.

find TCPPING initial_hosts=”thishost[7800],otherhost[7800]”, change thishost to ip address of the current host,

change otherhost to ip address of another node. thange the two “7800″ to “7820″.

Sample TCP configure as follows:

         <Config>

            <TCP bind_addr=”10.5.6.44” start_port=”7820” loopback=”true”

                 tcp_nodelay=”true”

                 recv_buf_size=”20000000″   

                 send_buf_size=”640000″

                 discard_incompatible_packets=”true”          

                 enable_bundling=”false”

                 max_bundle_size=”64000″

                 max_bundle_timeout=”30″       

                 use_incoming_packet_handler=”true”

                 use_outgoing_packet_handler=”false”

                 down_thread=”false” up_thread=”false”   

                 use_send_queues=”false”                            

                 sock_conn_timeout=”300″

                 skip_suspected_members=”true”/>

            <TCPPING initial_hosts=”10.5.6.44[7820],10.5.6.246[7820]” port_range=”3″

                     timeout=”3000″

                     down_thread=”false” up_thread=”false”

                     num_initial_members=”3″/>    

            <MERGE2 max_interval=”100000″                       

                    down_thread=”false” up_thread=”false” min_interval=”20000″/>

            <FD_SOCK down_thread=”false” up_thread=”false”/>

            <FD timeout=”10000″ max_tries=”5″ down_thread=”false” up_thread=”false” shun=”true”/>

            <VERIFY_SUSPECT timeout=”1500″ down_thread=”false” up_thread=”false”/>

            <pbcast.NAKACK max_xmit_size=”60000″

                           use_mcast_xmit=”false” gc_lag=”0″

                           retransmit_timeout=”300,600,1200,2400,4800″

                           down_thread=”false” up_thread=”false”

                           discard_delivered_msgs=”true”/>

            <pbcast.STABLE stability_delay=”1000″ desired_avg_gossip=”50000″

                           down_thread=”false” up_thread=”false”

                           max_bytes=”400000″/>

            <pbcast.GMS print_local_addr=”true” join_timeout=”3000″

                        down_thread=”false” up_thread=”false”

                        join_retry_timeout=”2000″ shun=”true”

                        view_bundling=”true”/>

            <pbcast.STATE_TRANSFER down_thread=”false” up_thread=”false” use_flush=”false”/>

         </Config>

 

3.4 JNDI DataSource

cd ~/jboss/server/default/deploy

cp ~/jboss/docs/examples/jca/postgres-ds.xml .

vi postgres-ds.xml

Please modify servername, port, database name, user name and password on postgres-ds.xml.

 

3.5 Configure other node as above.

 

4 Apache2.0.x and connctors installation

It seem that debian sarge don’t support apache2.2.x version. If you install apache via apt-get method, apache might not support mod_jk. So should compile and install apache2.0.x by manual.

4.1 Upload apache package

Please upload httpd-2.0.63.tar.gz to /usr/src directory.

 

4.2 Compile & Install apache2.0.x

Login with root

cd /usr/src/

tar zxvf httpd-2.0.63.tar.gz

cd httpd-2.0.63

./configure -prefix=/usr/local/apache2 –enable-so –enable-modules=all  –enable-mods-shared=all

If debian has multiple CPU, please add -with-mpm=prefork to above command line.

make clean

make

make install

 

4.3 Complie connectors

Upload tomcat-connectors-1.2.26-src.tar.gz to /usr/src directory.

cd /usr/src/tomcat-connectors-1.2.26-src/native

./configure –with-apxs=/usr/local/apache2/bin/apxs

If debian has multiple CPU, please add –enable-prefork to above command line.

make

make install

You can see mod_jk.so on /usr/local/apache2/modules directory.

 

4.4 Apache & connectors configure

cd /usr/local/apache2/conf

Please add the line to httpd.conf.

Include conf/mod_jk.conf 

Please add mod_mk.conf on /usr/local/apache2/conf directory, its content as follows:

# Load mod_jk module

# Specify the filename of the mod_jk lib

LoadModule jk_module modules/mod_jk.so

#LoadModule jk_module modules/mod_jk-1.2.26-httpd-2.2.4.so

 

# Where to find workers.properties

JkWorkersFile conf/workers.properties

 

# Where to put jk logs

JkLogFile logs/mod_jk.log

 

# Set the jk log level [debug/error/info]

JkLogLevel info

 

# Select the log format

JkLogStampFormat  “[%a %b %d %H:%M:%S %Y]”

 

# JkOptions indicates to send SSK KEY SIZE

JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories

 

# JkRequestLogFormat

JkRequestLogFormat “%w %V %T”

 

# Mount your applications

JkMount /application/* loadbalancer

 

# You can use external file for mount points.

# It will be checked for updates each 60 seconds.

# The format of the file is: /url=worker

# /examples/*=loadbalancer

#JkMountFile conf/uriworkermap.properties

 

# Add shared memory.

# This directive is present with 1.2.10 and

# later versions of mod_jk, and is needed for

# for load balancing to work properly

JkShmFile logs/jk.shm

 

# Add jkstatus for managing runtime data

<Location /jkstatus/>

    JkMount status

    Order deny,allow

    Deny from all

    Allow from 127.0.0.1

</Location>

 

Please add workers.properties on /usr/local/apache2/conf directory, its content as follows:

worker.list=loadbalancer,debian01,debian02

 

# Define the first node…

worker.debian01.port=8009

worker.debian01.host=10.5.6.246

worker.debian01.type=ajp13

 

worker.debian01.lbfactor=1

#worker.debian01.local_worker=1

worker.debian01.cachesize=100

 

# Define the 2nd node…

worker.debian02.port=8009

worker.debian02.host=10.5.6.44

worker.debian02.type=ajp13

worker.debian02.lbfactor=1

#worker.debian02.local_worker=1

worker.debian02.cachesize=100

 

# Now we define the load-balancing behaviour

worker.loadbalancer.type=lb

worker.loadbalancer.balanced_workers=debian01,debian02

 

worker.loadbalancer.sticky_session=1

 

the debian01, debian02 are jvmRoute alias that defined on ~/jboss/server/default/deploy/jboss-web.deployer/server.xml.

10.5.6.246, 10.5.6.44 are ip address of two nodes.

 

4.5 Start/stop jboss and apache

Login with deploy account, then

cd ~/jboss/bin

./run.sh -b 0.0.0.0

If stop jboss, please input ctrl+c on terminal.

 

Login with root,

Cd /usr/local/apache2/bin

Start apache as follows:

./apachectl –k start

Stop apache as follows:

./apachectl stop

 

5 Deploy application on Jboss

安泰信息:Apache 2.0性能优化—MPM的选择与配置

归档在: apache — JACKEYJ @ 11:25 AM


  Apache 2.0在性能上的改善最吸引人。在支持POSIX线程的Unix系统上,Apache可以通过不同的MPM运行在一种多进程与多线程相 混合的模式下,增强部分配置的可扩充性能。相比于Apache 1.3,2.0版本做了大量的优化来提升处理能力和可伸缩性,并且大多数改进在默认状态下 即可生效。但是在编译和运行时刻,2.0也有许多可以显著提高性能的选择。本文不想叙述那些以功能换取速度的指令,如HostnameLookups等, 而只是说明在2.0中影响性能的最核心特性:MPM(Multi -Processing Modules,多道处理模块)的基本工作原理和配置指令。

  毫不夸张地说,MPM的引入是Apache 2.0最重要的变化。大家知道,Apache是基于模块化的设计,而Apache 2.0更扩展 了模块化设计到Web服务器的最基本功能。服务器装载了一种多道处理模块,负责绑定本机网络端口、接受请求,并调度子进程来处理请求。扩展模块化设计有两 个重要好处:

  ◆ Apache可以更简洁、有效地支持多种操作系统;

  ◆ 服务器可以按站点的特殊需要进行自定制。

  在用户级,MPM看起来和其它Apache模块非常类似。主要区别是在任意时刻只能有一种MPM被装载到服务器中。

  指定MPM的方法

  下面以Red Hat Linux 9为平台,说明在Apache 2.0中如何指定MPM (Apache采用2.0.45)。先解压缩源 代码包httpd-2.0.45.tar.gz,生成httpd-2.0.45目录(Apache 1.3源代码包的命名规则是 apache_1.3.NN.tar.gz,而2.0版则是httpd-2.0.NN.tar.gz,其中NN是次版本号)。

  进入httpd-2.0.45目录,运行以下代码:

$ ./configure –help|grep mpm

  显示如下:

–with-mpm=MPM
Choose the process model for Apache to use.
MPM={beos|worker|prefork|mpmt_os2| perchild|leader|threadpool}

  上述操作用来选择要使用的进程模型,即哪种MPM模块。Beos、mpmt_os2分别是BeOS和OS/2上缺省的 MPM, perchild主要设计目的是以不同的用户和组的身份来运行不同的子进程。这在运行多个需要CGI的虚拟主机时特别有用,会比1.3版中的 SuExec 机制做得更好。leader和threadpool都是基于worker的变体,还处于实验性阶段,某些情况下并不会按照预期设想的那样工 作,所以 Apache官方也并不推荐使用。因此,我们主要阐述prefork和worker这两种和性能关系最大的产品级MPM ( 有关其它的MPM 详细说明,请参见Apache官方文档:http://httpd.apache.org/docs-2.0/mod/)。

  prefork的工作原理及配置

  如果不用“–with-mpm”显式指定某种MPM,prefork就是Unix平台上缺省的MPM。它所采用的预派生子进程方式也 是 Apache 1.3中采用的模式。prefork本身并没有使用到线程,2.0版使用它是为了与1.3版保持兼容性;另一方面,prefork用单 独的子进程来处理不同的请求,进程之间是彼此独立的,这也使其成为最稳定的MPM之一。

  若使用prefork,在make编译和make install安装后,使用“httpd -l”来确定当前使用的MPM,应该会看到 prefork.c(如果看到worker.c说明使用的是worker MPM,依此类推)。再查看缺省生成的httpd.conf配置文件,里面包含 如下配置段:

<IfModule prefork.c>;
StartServers 5
MinSpareServers 5
MaxSpareServers 10
MaxClients 150
MaxRequestsPerChild 0
</IfModule>;

  prefork的工作原理是,控制进程在最初建立“StartServers”个子进程后,为了满足MinSpareServers设置的需 要创建一个进程,等待一秒钟,继续创建两个,再等待一秒钟,继续创建四个……如此按指数级增加创建的进程数,最多达到每秒32个,直到满 足 MinSpareServers设置的值为止。这就是预派生(prefork)的由来。这种模式可以不必在请求到来时再产生新的进程,从而减小了系统 开销以增加性能。

  MaxSpareServers设置了最大的空闲进程数,如果空闲进程数大于这个值,Apache会自动kill掉一些多余进程。这个值不要 设得过大,但如果设的值比MinSpareServers小,Apache会自动把其调整为MinSpareServers+1。如果站点负载较大,可考 虑同时加大MinSpareServers和MaxSpareServers。

  MaxRequestsPerChild设置的是每个子进程可处理的请求数。每个子进程在处理了 “MaxRequestsPerChild” 个请求后将自动销毁。0意味着无限,即子进程永不销毁。虽然缺省设为0可以使每个子进程处理更多的请求,但 如果设成非零值也有两点重要的好处:

  ◆ 可防止意外的内存泄漏;

  ◆ 在服务器负载下降的时侯会自动减少子进程数。

  因此,可根据服务器的负载来调整这个值。笔者认为10000左右比较合适。

  MaxClients是这些指令中最为重要的一个,设定的是Apache可以同时处理的请求,是对Apache性能影响最大的参数。其缺省 值 150是远远不够的,如果请求总数已达到这个值(可通过ps -ef|grep http|wc -l来确认),那么后面的请求就要排队,直到某个已 处理请求完毕。这就是系统资源还剩下很多而HTTP访问却很慢的主要原因。系统管理员可以根据硬件配置和负载情况来动态调整这个值。虽然理论上这个值越 大,可以处理的请求就越多,但Apache默认的限制不能大于256。如果把这个值设为大于256,那么 Apache将无法起动。事实上,256对于负 载稍重的站点也是不够的。在Apache 1.3中,这是个硬限制。如果要加大这个值,必须在“configure”前手工修改的源代码树下的 src/include/httpd.h中查找 256,就会发现“#define HARD_SERVER_LIMIT 256”这行。把256改为要 增大的值(如4000),然后重新编译Apache即可。在Apache 2.0中新加入了ServerLimit指令,使得无须重编译Apache就可 以加大MaxClients。下面是笔者的prefork配置段:

<IfModule prefork.c>;
StartServers 10
MinSpareServers 10
MaxSpareServers 15
ServerLimit 2000
MaxClients 1000
MaxRequestsPerChild 10000
</IfModule>;

  上述配置中,ServerLimit的最大值是20000,对于大多数站点已经足够。如果一定要再加大这个数值,对位于源代码树下server/mpm/prefork/prefork.c中以下两行做相应修改即可:

#define DEFAULT_SERVER_LIMIT 256
#define MAX_SERVER_LIMIT 20000

  worker的工作原理及配置

  相对于prefork,worker是2.0 版中全新的支持多线程和多进程混合模型的MPM。由于使用线程来处理,所以可以处理相对海量的 请求,而系统资源的开销要小于基于进程的服务器。但是, worker也使用了多进程,每个进程又生成多个线程,以获得基于进程服务器的稳定性。这种 MPM的工作方式将是Apache 2.0的发展趋势。

  在configure -with-mpm=worker后,进行make编译、make install安装。在缺省生成的httpd.conf中有以下配置段:

<IfModule worker.c>;
StartServers 2
MaxClients 150
MinSpareThreads 25
MaxSpareThreads 75
ThreadsPerChild 25
MaxRequestsPerChild 0
</IfModule>;

  worker的工作原理是,由主控制进程生成“StartServers”个子进程,每个子进程中包含固定的 ThreadsPerChild 线程数,各个线程独立地处理请求。同样,为了不在请求到来时再生成线程,MinSpareThreads和 MaxSpareThreads设置了最少和最多的空闲线程数;而MaxClients设置了所有子进程中的线程总数。如果现有子进程中的线程总数不能满 足负载,控制进程将派生新的子进程。

  MinSpareThreads和MaxSpareThreads的最大缺省值分别是75和250。这两个参数对Apache的性能影响并不大,可以按照实际情况相应调节。

  ThreadsPerChild是worker MPM中与性能相关最密切的指令。ThreadsPerChild的最大缺省值是64,如果 负载较大,64也是不够的。这时要显式使用 ThreadLimit指令,它的最大缺省值是20000。上述两个值位于源码树server/mpm /worker/worker.c中的以下两行:

#define DEFAULT_THREAD_LIMIT 64
#define MAX_THREAD_LIMIT 20000

  这两行对应着ThreadsPerChild和ThreadLimit的限制数。最好在configure之前就把64改成所希望的值。注意,不要把这两个值设得太高,超过系统的处理能力,从而因Apache不起动使系统很不稳定。

  Worker模式下所能同时处理的请求总数是由子进程总数乘以ThreadsPerChild值决定的,应该大于等于MaxClients。 如果负载很大,现有的子进程数不能满足时,控制进程会派生新的子进程。默认最大的子进程总数是16,加大时也需要显式声明ServerLimit(最大值 是20000)。这两个值位于源码树server/mpm/worker/worker.c中的以下两行:

#define DEFAULT_SERVER_LIMIT 16
#define MAX_SERVER_LIMIT 20000

  需要注意的是,如果显式声明了ServerLimit,那么它乘以ThreadsPerChild的值必须大于等于MaxClients,而 且MaxClients必须是ThreadsPerChild的整数倍,否则Apache将会自动调节到一个相应值(可能是个非期望值)。下面是笔者 的 worker配置段:

<IfModule worker.c>;
StartServers 3
MaxClients 2000
ServerLimit 25
MinSpareThreads 50
MaxSpareThreads 200
ThreadLimit 200
ThreadsPerChild 100
MaxRequestsPerChild 0
</IfModule>;

  通过上面的叙述,可以了解到Apache 2.0中prefork和worker这两个重要MPM的工作原理,并可根据实际情况来配置Apache相关的核心参数,以获得最大的性能和稳定性。 

其它更详细的写以到http://httpd.apache.org/docs-2.0/

2007年03月21日

通过Apache proxy 连接Tomcat

归档在: 未分类, Linux, apache, jboss, java — JACKEYJ @ 2:25 AM

以前都是通过mod_jk来使用apache+tomcat.

今天在晚上看到最新的Apache2.2.4正式发布了proxy功能,于是就进行了一下测试.结果还不错,比mod_jk配置要方便多了.

测试情况:

系统:winxp sp2 apache2.2.4 tomcat 5.0

配置:

1.修改apache的httpd.conf文件,启用proxy,如下:

#########################################################
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_ajp_module modules/mod_proxy_ajp.so
LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
LoadModule proxy_connect_module modules/mod_proxy_connect.so
LoadModule proxy_http_module modules/mod_proxy_http.so
LoadModule proxy_ftp_module modules/mod_proxy_ftp.so
#########################################################

2.在httpd.conf中启用vhost配置

# Virtual hosts
Include conf/extra/httpd-vhosts.conf

3.在httpd-vhost.conf配置一个虚拟主机,如下:


ServerAdmin admin@viiving.com
    ProxyPass / ajp://localhost:8009/
ProxyPassReverse / ajp://localhost:8009/
ServerName localhost
ServerAlias test.viiving.com
ErrorLog logs/dummy-host3.www.viiving.com-error_log
CustomLog logs/dummy-host3.www.viiving.com-access_log common
>4.启动tomcat和apache在浏览器中输入http://localhost/index.jsp连接地址就可以访问到tomcat中的内容.

5.其他

linux(CentOS4.4 and Ubuntu6.06)环境下的配置
./configure -prefix=/data/apache2 -enable-so -enable-rewrite=share -enable-proxy=share -enable-proxy-ajp=share -enable-dav =share

______________________________________________________________________
reference other doc

(更多…)

2007年03月02日

通过ssh隧道访问内部网络

归档在: 未分类, Linux, DB, apache, jboss — JACKEYJ @ 11:27 AM

一。简介:

大多数人知道SSH是用来替代R命令集,是用于加密的远程登录,文件传输,甚至加密的
FTP(SSH2内置), 因此SSH成为使用极广的服务之一,不仅如此,SSH还有另一项非常有
用的功能,就是它的端口转发隧道功能,利用此功能,让一些不安全的服务象POP3,
SMTP,FTP,LDAP等等通过SSH的加密隧道传输,然后,既然这些服务本身是不安全的,
密码和内容是明文传送的,现在其它中间媒介也没无监听了。

二。图示:

SSH的加密隧道保护的只是中间传输的安全性,使得任何通常的嗅探工具软件无法获取发
送内容。如下图:
假设客户机和服务器都运行Linux,且以POP3为例。

C (pop3 server: S)              S
_______                         ________
|     |                         |      |
|     |________POP3___________ >|      |
|_____|                         |______|
(图一:正常的POP3)

(图一:正常的POP3)

C (pop3 server:C)               S (pop3 client: S)
_______                         ________
|     |                         |      |
|     |——–SSH连接———>|      |
|_____|                         |______|

(图二:SSH隧道后的POP3)

如图一: 正常的POP3连接是客户C向服务器S进行连接,C的设置是POP3服务器为S。
如图二: 用SSH隧道的话,客户C设置pop3服务器为自己(localhost),然后设置SSH加密
隧道
,如果设置在同样的端口110听取C的请求,则对C来说,pop3服务器是自己本身,端口也
是110 对S来说,看到的pop3请求地址不是来自C,而也是自己本身,因为有了SSH隧道。

三。SSH隧道设置

1. 首先必须在C和S上安装SSH,确保SSH首先能工作。
2. 我们用简单的一个命令如下:

# ssh -C -P -f sshaccount@S -L 110:S:110 sleep 7200

解释如下:
-C 使用压缩功能,是可选的,加快速度。

-P 用一个非特权端口进行出去的连接。

-f 一旦SSH完成认证并建立port forwarding,则转入后台运行。

sshaccount 客户C在服务器S上的SSH连接帐号

-L 110:S:110 转发C对本地端口110的连接到远程服务器S的110端口。
也可以用高端端口(普通用户使用,因为普通用户不能在低于1024的端口上建立SSH隧道)
如果用高端端口,如:-L 1110:S:110,这样任何用户都可建立这种加密隧道。

sleep 7200 一般用于script,必须给一个命令,我们给一个sleep等待空 命令,这里为
2小时,你可以
设为更长用于保持整个连接过程, 如 sleep 100000000 。

四。检验

设置后你就可以在客户C上用 # telnet localhost 110 命令而连到 S 上收取email,
而整个过程也被加密。

五。其它常见问题:

1. 每次启动该命令时需要输入密码以验证SSH连接,你也可以用RSA键对的方法自动化
SSH连接。
看文章荟萃中的另一篇文章《如何在两台linux服务器之间用RSA键对的方法SSH/SCP不需
密码》

2. 如果你希望上面的命令永远保持运行状态,你可以用如下的scripts.
#!/bin/sh
while [ 1 ] ; do
ssh -C -P -f sshaccount@S -L 110:S:110 sleep 7200
sleep 1
done

3. 你可以在一个命令中用多个L 参数 ,如 -L 1110:S:110 -L 225:S:25 -L
389:S:389

4. 一些windows客户端软件,象netscape mail,不能改变pop3端口号,被强迫到110,
则你只能指定110

5. Linux下的fetchmail常用来自动接收邮件,可在.fetchmailrc中利用
preconnect参数预连接 ,指定上面的命令行。

6. 如果客户端是windows, 则可用tera Term pro,参考
http://www.phys.washington.edu/Computing/winftpssh.html

SSH隧道进行安全 TCP/IP 联接

我们可以使用 SSHPostgreSQL 服务器和客户端之间的网络联接进行加密。经过适当处理后,这样做可以获得一个足够安全的网络联接。 即使是没有 SSL 的客户端上也如此。

首先确认 SSH 正在和 PostgreSQL 服务器的同一台机器上正确地运行, 而且你可以通过某个用户用SSH登录。然后你可以用下面这样的命令从客户端的机器上建立一个安全通道:

ssh -L 3333:foo.com:5432 joe@foo.com

-L 参数的第一个数字,3333,是通道你这端的端口号;可以自由选择。 第二个数字,5432,是通道的远端:你的服务器使用的端口号。 在两个端口号之间的名称或者 IP 地址是你准备联接的数据库服务器。 为了使用这个通道与你的数据库服务器联接,你在本机于端口 3333 联接:

psql -h localhost -p 3333 postgres

对于数据库服务器而言,她会把你当做真正的用户 joe@foo.com 并且使用为这个用户和主机设置的认证手段进行认证。 请注意,服务器不会认为连接是 SSL 加密的,因为实际上在 SSH 服务器和 PostgreSQL 服务器之间是没有加密的。 只要它们在同一台机器上,这么做并不会导致任何安全漏洞。

为了保证能够成功地建立通道,你必须被允许作为 joe@foo.com 通过ssh建立联接, 就象你使用ssh建立终端会话一样。

提示: 还有几种不同的产品可以提供安全的通道,所使用的过程类似我们刚刚描述的过程。

上面是postgre的ssh连接方式。同样也使用mysql,oracle等数据库。

测试例子

自公司的内部网络里面通过ssh方式访问另外一个网络里面的http和ssl服务,为了测试我作了3层的ssh连接。

ssh -L 80:localhost:8000 root@192.16.2.22

ssh -L 8000:localhost:8000 test1@59.11.89.15
ssh -L 8000:localhost:80 test@192.168.1.24

ssh -L 443:localhost:4443 test@192.168.1.229

ssh -L 4443:localhost:4443 test1@59.11.89.15
ssh -L 4443:localhost:443 test@192.168.1.24

这样之后就可以在本地通过http://localhost and https://localhost

不过测试过程中遇到了一些问题。当我们把localhost替换成server的ip地址的时候就发生了channel 3: open failed: connect failed: Connection refused的错误。

2007年02月20日

linux web site 收藏

归档在: 未分类, Linux, apache, java, php, perl, python — JACKEYJ @ 9:10 PM

国外
http://lwn.net/
http://www.tldp.org/
https://rhn.redhat.com
http://www.justlinux.com/
http://www.linuxtoday.com/
http://www.linuxquestions.org/
http://www.fokus.gmd.de/linux/
http://www.linux-tutorial.info/
http://public.www.planetmirror.com/
http://www.freebsdforums.org/forums/
http://www.netfilter.org/documentation/
http://www-106.ibm.com/developerworks/linux/
http://www.redhat.com/docs/ redhat官方文档,redhat用户必看
http://www.redhat.com/apps/support/errata/ redhat安全性警告、软件更新和增强,建议每周至少查阅一次

国内
http://www.fanqiang.com/
http://www.linuxsir.com/
http://www.chinaunix.net/
http://www.linuxfans.org/
http://www.linuxeden.com/
http://www.linuxforum.net/
http://www.linuxaid.com.cn/
http://freesoft.online.sh.cn/
http://www-900.ibm.com/developerWorks/cn/linux/index.shtml

redhat linux下载地址
ftp://ftp.redhat.com/pub/redhat/linux
http://redhat.com/download/mirror.html

redhat 发行版全球官方镜像站
http://freesoft.online.sh.cn 国内高速下载
http://www.linuxeden.com/
http://www.freshrpms.net/
http://www.linux.cz/apt-rpm/ - Red Hat linux 7.x repositories (with all freshrpms included too!)
http://apt-rpm.tuxfamily.org/ - Red Hat linux 6.2, 7.x and 8.0 repositories
http://apt.nlc.no/apt/ - Red Hat linux 6.2, 7.x and Rawhide repositories
http://ftp.uninett.no/pub/linux/apt/ - Red Hat linux 6.2, 7.x, 8.0 and Rawhide repositories
http://redhat.usu.edu/ - Red Hat linux 7.2, 7.3 and 8.0 repositories
ftp://mirror.pa.msu.edu/apt/ - Red Hat linux 7.2, 7.3 and 8.0 repositories
http://apt-rpm.codefactory.se/ - Red Hat linux 7.2, 7.3 and 8.0 and gnomehide repositories http://apt.42h.de/ - Red Hat Linux 7.2, 7.3 and 8.0 repositories
http://apt.toggletext.com.au/ - A few Red Hat linux 7.2 custom packages http://people.ecsc.co.uk/~matt/repository.html - Lots of custom Red Hat Linux 8.0 GNOME 2 and related packages
http://ftp-stud.fht-esslingen.de/apt/ - Red Hat linux 7.x, 8.x and rawhide
http://apt.unl.edu/ - Red Hat linux 7.3 and 8.0
http://kde-redhat.sourceforge.net/ - KDE for Red Hat project

linux 常用软件搜索
http://bingle.pku.edu.cn 北大天网ftp搜索,教育网和国内用户查找软件应首先访问,软件特别丰富
http://www.linuxforum.net/ 有很多国外软件的官方镜像,应首先访问
http://rpmfind.net
http://freshmeat.net
http://sourceforge.net
注:很多软件主站是在sourceforge上的,形如http://phpmyadmin.sourceforge.net的地址,可以通过http://sourceforge.net/projects/phpmyadmin这样的地址访问,如不能上也可用http镜象直接下载软件如 http://telia.dl.sourceforge.net/phpmyadmin/
http://www.gnu.org GNU的大本营,gcc,bash之类的软件源码可以到这里得到 linux 文档和FAQs
http://www.tldp.org 强烈推荐! The linux document project: linux HOWTO and guides
http://www.linux.org.tw/CLDP/ 强烈推荐! TLDP中文版,繁体
http://www.linuxgazette.com/ linux在线杂志
http://linux.tcpip.com.**/*ownload/ linux中文版文档和软件下载
http://cosoft.org.cn/html/documents/ 很多中文版手册
http://www.google.com 强烈推荐!本站的讨论内容也可以通过google搜索得到,遇到疑难问题到google搜索会得到比绝大多数论坛更快的回答

linux 桌面主站一览表
GNOME: http://www.gnome.org
KDE: http://www.kde.org
XFce: http://www.xfce.org/
Window Maker: http://www.windowmaker.org/
BlackBox: http://sourceforge.net/projects/blackboxwm
XPde: http://www.xpde.com
ROX: http://sourceforge.net/projects/rox
5dwm: http://5dwm.org/
foXdesktop: http://sourceforge.net/projects/foxdesktop
EDE: http://sourceforge.net/projects/ede
UDE: http://sourceforge.net/projects/udeproject

其它OS:
Mandrake linux http://www.mandrakelinux.com
debian linux http://www.debian.org
slackware linux http://www.slackware.com
suse linux http://www.suse.com
FreeBSD http://www.freebsd.org
netbsd http://www.netbsd.org
openbsd http://www.openbsd.org
Solaris http://www.sun.com/solaris 硬件和安装专业站点
linux Kernel内核: http://www.kernel.org
USB安装问题: http://www.linux-usb.org
笔记本计算机安装: http://www.linux-laptop.net
打印机驱动 http://www.linuxprinting.org

主要服务器软件主站
1. WWW server:
Apache http://www.apache.org Apache
SSL模块Mod_ssl http://www.modssl.org
aol server http://www.aolserver.com

2. Servlets/JSP引擎:
Tomcat: http://jakarta.apache.org
Resin: http://www.caucho.com
开源应用服务器AS–Jboss: http://www.jboss.org

3. 服务器语言:
PHP http://www.php.net
Zend http://www.zend.com
Java http://www.javasoft.com
Python http://www.python.org
Zope http://www.zope.org
Perl http://www.perl.org
Ruby http://www.ruby-lang.org/en

4. 数据库:
MySQL http://www.mysql.com
PostgreSQL http://www.postgresql.org
Oracle http://www.oracle.com
sap db http://www.sapdb.org
PHPMyAdmin — http://www.phpmyadmin.org
PHPPgAdmin: http://sourceforge.net/projects/phppgadmin

5. FTP服务器
wu-ftpd http://www.wu-ftpd.org
Proftpd http://www.proftpd.org
Pure-ftpd http://www.pureftpd.org
ncftpd http://www.ncftpd.com
vsftpd http://vsftpd.beasts.org

6. SMTP服务器
Sendmail http://www.sendmail.org
qmail http://www.qmail.org
Postfix http://www.postfix.org
Exim http://www.exim.org
POP3–qpopper http://www.qpopper.org

7. IMAP服务器
Cyrus-IMAPD http://asg.web.cmu.edu/cyrus/imapd
UW IMAP http://www.washington.edu/imap
courierimap http://www.inter7.com/courierimap

8. 基本安全工具
firewall Iptables: http://www.iptables.org
shorewall: http://www.shorewall.net
inetd Xinetd: http://www.xinetd.org
Openssl: http://www.openssl.org
OpenSSH: http://www.openssh.org
nmap: http://http://www.insecure.org/nmap
Tripwire: http://www.tripwire.org
snort: http://www.snort.org
nessus: http://www.nessus.org
GnuPG: http://www.gnupg.org
chkrootkit: http://www.chkrootkit.org
安全顾问: http://www.cert.org

9. 其它服务器软件
CVS–CVS http://www.cvshome.org
DNS域名解析BIND http://www.isc.org/products/BIND/
DHCP–DHCP http://www.isc.org/products/DHCP/
INN新闻组INN: http://www.isc.org/products/INN/
Proxy代理缓存: Squid http://www.squid-cache.org
Socks代理:http://www.socks.nec.com/
Samba Server: http://www.samba.org
Print CUPS: http://www.cups.org
LDAP OpenLDAP: http://www.openldap.org
流量分析 MRTG http://www.mrtg.org
日志分析 webalizer http://www.mrunix.net/webalizer/
Ipsec/VPN http://www.freeswan.org
路由 Zebra http://www.zebra.org
集群 LVS http://www.linuxvirtualserver.org
MPICH http://www-unix.mcs.anl.gov/mpi/mpich/
PVM:http://www.netlib.org/pvm3/
Rsync: http://rsync.samba.org
linuxconf: http://www.solucorp.qc.ca/linuxconf/
Webmin: http://www.webmin.com

10. 其他
RedHat Advance Server2.1升级内核
http://redhat.pacific.net.au/redhat/linux/updates/enterprise/2.1AS/en/os/SRPMS/

下一页 »

Powered by ZJANT