---
title: "针对Web服务器总体优化"
author: "winzenghua"
date: "2006-12-13T17:04:00Z"
tags: []
slug: "web"
draft: false
lastmod: "2006-12-13T17:04:00Z"
id: 954655
url: "https://java4gis.iteye.com/blog/954655"
---

一、文件系统  
在fstab里加入noatime，如  
#cat/etc/fstab  
/dev/sda1/homeext3noatime,defaults12  
reboot或者重新mount生效  
二、Tcp优化  
在/etc/sysctl.conf里加入  
net.ipv4.netfilter.ip\_conntrack\_tcp\_timeout\_syn\_recv=3  
net.ipv4.tcp\_syncookies=1 #启用syncookies  
net.ipv4.tcp\_max\_syn\_backlog=8192 #定义backlog队列容纳的最大半连接数  
net.ipv4.tcp\_fin\_timeout=30  
net.ipv4.tcp\_keepalive\_time=1800  
net.ipv4.tcp\_window\_scaling=0  
net.ipv4.tcp\_sack=0  
net.ipv4.tcp\_timestamps=0  
\[color=blue]老实说，对于sysctl.conf究竟能不能用起来还真不知道，因为我是没用起来，虽然内核里面是支持的==b\[/color]  
三、虚拟内存优化  
/etc/sysctl.conf  
vm.lower\_zone\_protection=100  
四、I/O调度器  
在grub.conf的相应启动选项里加入elevator=deadline，如：  
kernel/vmlinuz-2.6.6roroot=/dev/sda6elevator=deadline  
这里用了Deadline的I/O调度器,它比系统默认的AnticipatoryI/O调度器更为小巧,在数据吞吐量非常大的数据库系统中表现得更有优势。  
五、内核源代码参数修改  
修改/usr/src/linux/include/linux/posix\_types.h  
#define\_\_FD\_SETSIZE1024?nbsp;65536  
设置fd\_set支持的最大数量  
修改/usr/src/linux/include/linux/fs.h  
#defineINR\_OPEN1024?nbsp;65536  
#defineNR\_FILE8192?nbsp;65536  
#defineNR\_RESERVED\_FILES10?nbsp;128  
设置最大打开文件数量（TCP连接数量）  
修改/usr/src/linux/include/net/tcp.h  
#defineTCP\_TIMEWAIT\_LEN(60\*HZ)?nbsp;1\*HZ  
#defineTCP\_SYNACK\_RETRIES5?nbsp;3  
设置在backlog队列里的半连接的重试次数，每次都会花相应的时间，本质上  
也是减少重试时间  
makemenuconfig中，去掉没用的选项，打开以下选项的开关：  
HighMemorySupport(支持4GB以上内存)  
Symmetricmulti-processingsupport(支持多CPU)  
TCPsyncookiesupport(可以防DOS)  
设置文件打开数等的其他方法(好处就是可以不重新编译内核)  
在/etc/init.d/sshd里加入(统一加在./etc/rc.d/init.d/functions行后面)

ulimit-n65535&gt;;/dev/null2&gt;;&1

ulimit-u16384&gt;;/dev/null2&gt;;&1

重起sshd：nohup/etc/init.d/sshdrestart&  
BTW：其实limits.conf不好，因为他需要rootsu-user遗传给普通用户  
如果直接远程登陆是没效果的  
六、apache  
1、安装:采用worker线程工作模式

./configure--prefix=/prefix

--enable-mods-shared=all

--enable-deflate

--with-mpm=worker

\[color=blue]chmod+s/prefix/bin/httpd\[/color]#允许普通用户也可以启动apache  
2、配置：httpd.conf

Timeout60

KeepAliveOn

MaxKeepAliveRequests150

KeepAliveTimeout5

StartServers3

MaxClients3200#支持3200个客户端哦

ServerLimit32

MinSpareThreads50

MaxSpareThreads250

ThreadLimit200

ThreadsPerChild100

MaxRequestsPerChild0

#上面的几个参数对机器要求比较高，如果是比较差的机器，每台机1000个连接最多了，你可能要重新设置参数，我的机器是DELL2850

UseCanonicalNameOff

HostnameLookupsOff

ServerSignatureOff