选择显示字体大小

建立一个带宽、线程可控的下载型web网站

apache+mysql+php+proftpd+mod_limitip+mod_bandwidth

一、前言

本人结合网上资料和个人的实践,利用一个下午的时间写出了这个适用于资源下载型网站的典型配置。这种配置的web网站初步实现了ip线程线程流量的管理,同时proftpd用于上传资源,ssh用于主机管理。无疑这样可以自己支配宝贵的网络带宽,文章后面有个简单的防火墙配置仅供参考,我用的linux版本是redhat as 3.0。希望大家在看了这篇文章后多多和我交流, mail:llzqq@163.com

二、软件版本

apache-1.3.29
mysql-4.0.16
php-4.3.4
proftpd-1.2.9
mod_limitipconn-0.04
mod_bandwidth-2.0.4

三、安装配置mysql

# cd mysql-4.0.16

# ./configure --prefix=/usr/local/mysql --localstatedir=/usr/local/mysql/data
--disable-maintainer-mode ?with-mysqld-user=mysql --enable-large-files-without-debug

# make
# make install

添加mysql用户

# /usr/sbin/groupadd mysql

then we create a user called mysql which belongs to the mysql group;

# /usr/sbin/useradd -g mysql mysql

安装数据库文件:

#./scripts/mysql_install_db

设置文件权限:

# chown -r root:mysql /usr/local/mysql
# chown -r mysql:mysql /usr/local/mysql/data

配置ld.so.conf

# vi /etc/ld.so.conf

增加下面一句:

/usr/local/mysql/lib/mysql

建立mysql的启动文件:

# cp support-files/mysql.server /etc/init.d/mysql
# cp support-files/my-medium.cnf /etc/my.cnf

安全模式启动mysql

# /usr/local/mysql/bin/mysqld_safe ?user=mysql &

设置mysql的root密码:

# /usr/local/mysql/bin/mysqladmin -u root password new_password

mysql服务设置为开机启动:

# chmod 755 /etc/init.d/mysql
# chkconfig ?-add mysql
# chkconfig mysql on


四、安装配置php

#cd php-4.3.4

#./configure --prefix=/usr/local/php --with-apxs=/usr/local/apache/bin/apxs --with-mysql=/usr/local/mysql

#make
#make install

建立php的配置文件:

# cp php.ini-dist /usr/local/php/lib/php.ini

修改php的配置文件:

#vi /usr/local/php/lib/php.ini

doc_root= “/home/www/”
file_uploads=off
register-golbals = on

建立测试php页面

# vi /home/www/test.php



# chomd 755 /home/www/test.php


五、安装apache-1.3.29

# tar zvxf apache_1.3.29.tar.gz
# cd apache_1.3.29
# cp ../mod_bandwidth.c mod_bandwidth.c

修改src/include/httpd.h 增大最大线程
# vi src/include/httpd.h

修改其中的
#define hard_server_limit 256

#define hard_server_limit 2560

# ./configure --prefix=/usr/local/apache --enable-module=so
--enable-module=rewrite --enable-shared=max
--htdocsdir=/home/www --add-module=mod_bandwidth.c
--permute-module=begin:bandwidth

# make
# make install

建立启动文件:

#cp apachectl /etc/init.d/httpd

修改apache的配置文件:

#vi /usr/local/apache/conf/httpd.conf

修改、添加和确认存在下列配置项:

adddefaultcharset gb2312 注释掉“adddefaultcharset iso8859*”
extendedstatus on
loadmodule php4_module modules/libphp4.so
directoryindex index.html index.html.var index.php
addtype application/x-httpd-php .php
addtype application/x-httpd-php-source .phps
loadmodule limitipconn_module libexec/mod_limitipconn.so
addmodule mod_limitipconn.c

安装mod_limitipconn-0.04

# tar xzf mod_limitipconn-0.04.tar.gz
# cd mod_limitipconn-0.04
# vi makefile

apxs = /usr/local/apache/bin/apxs

# make
# make install

使用mod_limitip模块需要在httpd.conf中增加这个设置


设置要控制的目录
maxconnperip 2 限制每个ip的最大线程



完成mod_bandwidth模块的安装

创建 mod_bandwidth 运行需要的目录

mkdir /var/apachebw
mkdir /var/apachebw/link
mkdir /var/apachebw/master
chmod -r 777 /var/apachebw

修改httpd.conf增加下列内容


bandwidthdatadir "/var/apachebw/"
bandwidthmodule on


bandwidth 192.168.0 0 200000 制局域网内用户的下载速度为200k
bandwidth all 51200 限制其他用户的下载速度为每秒 51200 字节




修改/etc/init.d/httpd

# vi /etc/init.d/httpd

在三行之后添加如下内容:
#!/bin/sh
#
# startup script for the apache web server
# chkconfig: - 85 15
# description: apache is a world wide web server. it is used to serve \
# html files and cgi.
# processname: httpd
# pidfile: /usr/local/apache/log/httpd.pid
# config: /usr/local/apache/conf/httpd.conf

把apache服务设置为开机启动:

# chkconfig ?-add httpd
# chmod 755 /etc/init.d/httpd
# chkconfig httpd on

六、安装porftpd-1.2.9

# tar ?zxvf proftpd-1.2.9.tar.gz
# cd proftpd-1.2.9
# ./configure --prefix=/usr/local/proftpd
# make
# make install 

建立启动文件、把proftpd设置为开机启动

# cp ./contrib/dist/rpm/proftpd.init.d /etc/rc.d/init.d/proftpd


# chkconfig --add proftpd
# chmod 755 /etc/rc.d/init.d/proftpd

# vi /etc/rc.d/init.d/functions
export path="/sbin:/usr/sbin:/bin:/usr/bin:/usr/x11r6/bin:/usr/local/proftpd/
sbin"

# vi /etc/rc.d/init.d/proftpd
config: /usr/local/proftpd/etc/proftpd.conf
path="$path:/usr/local/proftpd/sbin"

# chkconfig proftpd on

建立帐号和目录:

# mkdir /home/test
# chmod 755 /home/test
# adduser -d /home/test -g ftp -s /sbin/nologin test
# passwd test

# adduser -d /home/upload -g ftp -s /sbin/nologin upload
# passwd upload

配置/usr/local/proftpd/etc/proftpd.conf,禁用匿名登陆

servername "llzqq's ftp service"
servertype standalone
defaultserver on

# port 21 is the standard ftp port.
port 21

# umask 022 is a good standard umask to prevent new dirs and files
# from being group and world writable.
umask 022

maxinstances 10

# set the user and group under which the server will run.
user nobody
group ftp

# to cause every ftp user to be "jailed" (chrooted) into their home
# directory, uncomment this line.
defaultroot ~

# normally, we want files to be overwriteable.

allowoverwrite on


# we want 'welcome.msg' displayed at login, and '.message' displayed
# in each newly chdired directory.
displaylogin welcome.msg
displayfirstchdir .message
requirevalidshell no

#limit user of being enbled login ftp server

allowgroup ftp
denyall

#


denygroup ftp

transferrate retr 51200 group ftp



denygroup ftp

transferrate stor 256000 group ftp


serverident off
maxclientsperhost 2
timeoutidle 600
timeoutlogin 300
timeoutnotransfer 300
timeoutstalled 300


七、reboot计算机

# shutdown ?r now

打开浏览器输入:http://loaclhost/test.php

如果你看到了关于:mysql;apache;php的一大堆信息,恭喜,你的安装基本上成功了,下一步可以测试一下proftp和带宽的设置了。


八、建立一个简单有效的防火墙

export path=/sbin:/usr/sbin:/bin:/usr/bin

modprobe iptable_nat
modprobe ip_nat_ftp
modprobe ip_nat_irc
modprobe ip_conntrack
modprobe ip_conntrack_ftp
modprobe ip_conntrack_irc

echo 1 >/proc/sys.net/ipv4/icmp_echo_ignore_broadcasts
echo 0 >/proc/sys.net/ipv4/conf/all/accept_source_route
echo 0 >/proc/sys.net/ipv4/conf/all/accept_redirects
echo 1 >/proc/sys.net/ipv4/icmp_ignore_bogus_error_responses
echo 1 >/proc/sys.net/ipv4/conf/all/log_martians

iptables -f
iptables -x
iptables -z

iptables -a input -i eth0 -s 10.0.0.0/8 -j drop
iptables -a input -i eth0 -s 192.168.0.0/16 -j drop

##
iptables -a input -m state --state established,related -j accept

## loopback
iptables -a input -i lo -j accept
iptables -a output -o lo -j accept

## syn-flooding
iptables -n syn-flood
iptables -a input -i eth0 -p tcp --syn -j syn-flood
iptables -a syn-flood -m limit --limit 1/s --limit-burst 4 -j return
iptables -a syn-flood -j drop

## make sure that new tcp connections are syn packets
iptables -a input -i eth0 -p tcp ! --syn -m state --state new -j drop

## http
iptables -a input -i eth0 -p tcp -d 0/0 --dport 80 -j accept

## ip packets limit
iptables -a input -f -m limit --limit 100/s --limit-burst 100 -j accept
iptables -a input -p icmp -m limit --limit 1/s --limit-burst 3 -j accept

## ftp service
iptables -a input -i eth0 -p tcp --dport 21 -j accept
iptables -a input -i eth0 -p tcp --dport 20 -j accept

## ssh login
iptables -a input -i eth0 -m mac --mac-source 00:00:00:00:00:00 -p tcp --dport 22 -j accept
iptables -a input -i eth0 -p tcp --dport 22 -j drop

## anything else not allowed
iptables -a input -i eth0 -j drop



九、附录:

mod_bandwidth选项简单说明:

bandwidthpulse
格式: bandwidthpulse <毫秒(千分之一秒>
默认: 1000
上下文: per server config

改变计算带宽的时间间隔,默认为1000毫秒(1秒)。使用更低的间隔可以获得
更精确的带宽控制,但消耗更多的cpu时间,反之亦然。

bandwidth
格式: bandwidth <速率>
默认: 无
上下文: per directory, .htaccess

限制这个目录下文件下载的速率。

domain 指定来自哪个域的连接受到这个设置的影响。
ip 指定来自哪个ip地址(或者ip段)的连接受到影响。
all 所有连接都受到影响。

示例:


# 来自 dualface.com 的连接不限制下载速度
bandwidth dualface.com 0
# 来自 192.168.0.0/16(或者192.168.0) 网段的连接不限制下载速度
bandwidth 192.168.0.0/16 0
# 其他连接限制下载速度为每秒1024字节
bandwidth all 1024
# 越前面的设置优先权越高


largefilelimit
格式: largefilelimit <文件大小> <速率>
默认: 无
上下文: per directory, .htaccess

对于超过指定大小的文件,下载时使用的速率。如果速率设置0即不限制速度,
但下载速度仍然要受到bandwidth设置的影响。如果设置成-1,则完全不受影响。
通过设置不同的文件大小和速率,可以设置不同大小范围内文件的下载速度。

示例:

文件尺寸大于等于200千字节的文件,下载速率为每秒3072字节
largefilelimit 200 3072
largefilelimit 1024 2048
maxconnection
格式: maxconnection <连接数>
默认: 0 (不限制)
上下文: per directory, .htaccess

当超过指定连接数时,拒绝新的连接。

minbandwidth
格式: minbandwidth <速率>
默认: all 256
上下文: per directory, .htaccess

设置最小带宽,默认为每秒256字节。根据bandwidth和largefilelimit设置的速
率。mod_bandwidth会计算允许的连接数。例如bandwidth为4096字节,而
minbandwidth为1024字节,则最大并发连接数为4。

mod_limitipconn选项简单说明:

设置要控制的目录
maxconnperip 2 限制单ip并发连接数
noiplimit image/* 不受限制的文件类型


maxconnperip 1
onlyiplimit audio/mpeg video 仅用于限制的文件类型   


 


关键字 本文所属关键字

相关 与本文相关文章

分类 所有文章关键字导航

源码编程相关

Java   Asp   PHP   .Net   XML   C/C++   CGI   VB   Jsp   J2ee   J2se   J2me   EJB   Servlet   Tomcat   Resin   Struts   Weblogic   Eclipse   ANT   GUI   JMS   Web servise   IDEA   Webphere   Hibernate   Spring   Jboss   Applet   Swing   Socket   Javamail   Perl   Ajax   P2P   安全   模式   框架   测试   开源   游戏

SQL数据库相关

My-SQL   Ms-SQL   Access   DB2   Oracle   Sybase   SQLserver   索引   存储过程   加密   数据库   分页   视图  

手机无线相关

3G   Wap   CDMA   GRPS   GSM   IVR   彩信   短信   无线   增值业务

网页设计制作相关

HTML   CSS   网页配色   网页特效   Javascript   VBscript   Dreamweaver   Frontpage   JS   Web   网站设计

网站建设推广相关

建站经验   网站优化   网站排名   推广   Alexa

操作系统/服务器相关

Windows XP   Windows 2000   Windows 2003   Windows Me   Windows 9.x   Linux   UNIX   注册表   操作系统   服务器   应用服务器

图形图像多媒体相关

Photoshop   Fireworks   Flash   Coreldraw   Illustrator   Freehand   Photoimpact   多媒体   图形图像

标准 网站致力的规范

Valid CSS!

无不良内容,无不良广告,无恶意代码

Valid XHTML 1.0 Transitional

creativecommons