http://www.chinaunix.net 作者:llzqq 发表于:2004-03-02 07:58:04
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  仅用于限制的文件类型
Read: 648
