当前位置: 首页 > 图文教程 > 操作系统 > Unix/Linux > freebsd6.2 nginx+php+mysql+zend系统优化防止ddos攻击

Unix/Linux
mysql+httpd+gd+php+zend
automake 小回顾
unix黑客精神的最好诠释
www
sun t3存储的设置
RPM简明中文手册
Linux爱好者入门教程 序章 (持续更新)
第一章 Linux基础知识 (飘心)
linux下进程与线程
浅谈如何学习linux
第二章 Linux安装
硬件安装指南
Windows 2000 的桌面不見了的解決方法
部分的ADSL路由器默认帐号密码
如何设定安全log服务器呢?
iptables 规则速查
网友学习 Linux 的七点忠告
sniffer的含义及原理
Linux编程白皮书 第二章 内存管理
Linux编程白皮书 第二章 内存管理 2.1.1 请求调页 --2.1.5 访问控制

Unix/Linux 中的 freebsd6.2 nginx+php+mysql+zend系统优化防止ddos攻击


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2010-01-15   浏览: 199 ::
收藏到网摘: n/a

一、安装软件前的准备

系统安装:插入freebsd6.2以上的光盘,最小化安装系统,同时安装好ports

二、手动安装nginx+php

1)        进入系统后,准备cvs更新:

1.         cd /usr/ports/net/cvsup-without-gui

2.         cp /usr/share/examples/cvsup/ports-supfile /etc/ports-supfile

3.         # vi /etc/ports-supfile

将其中的#*default host=CHANGE_THIS.FreeBSD.org一行改为

*default host=cvsup4.FreeBSDchina.org

4.         更新ports

    /usr/local/bin/cvsup -g -L 2 /etc/ports-supfile

2)        安装mysql

#cd /usr/ports/databases/mysql51-server

#make WITH_CHARSET=gbk WITH_XCHARSET=all WITH_PROC_SCOPE_PTH=yes BUILD_OPTIMIZED=yes BUILD_STATIC=yes SKIP_DNS_CHECK=yes WITHOUT_INNODB=yes install clean

#cp /usr/local/share/mysql/my-large.cnf /usr/local/etc/my.cnf   ##mysql的优化参数,也可以手动修改

#rehash

# mysql_install_db --user=mysql          ##初始化mysql

#/usr/local/bin/mysqld_safe &            ##启动mysql

#/usr/local/bin/mysqladmin -u root password 'newpass'    ##修改root密码,newpass是你需要改的密码

关闭mysql可以使用mysqladmin -uroot -p shutdown

3)        安装php

#cd /usr/ports/lang/php5

#make config    ##配置编译参数

[X] CLI        Build CLI version
[X] CGI        Build CGI version
[ ] APACHE     Build Apache module
[ ] DEBUG      Enable debug
[X]] SUHOSIN Enable Suhosin protection system
[X] MULTIBYTE Enable zend multibyte support
[ ] IPV6       Enable ipv6 support
[ ] REDIRECT   Enable force-cgi-redirect support (CGI only)
[ ] DISCARD    Enable discard-path support (CGI only)
[X] FASTCGI    Enable fastcgi support (CGI only)
[X] PATHINFO   Enable path-info-check support (CGI only)
#make install clean

# cp /usr/local/etc/php.ini-dist /usr/local/etc/php.ini   ##cp php.ini配置文件

4)        安装php5-extensions

# cd /usr/ports/lang/php5-extensions/

#make config

Options for php5-extensions 1.1
-------------------------------------------------
[X] CURL        CURL support

[X] FTP        FTP support
[X] GD       
[X] GETTEXT
[X] MBSTRING multibyte string support

[X] MCRYPT      Encryption support 
[X] MYSQL

[X] PCRE        Perl Compatible Regular Expression support
[ ] POSIX //去掉.
[ ] SQLITE //去掉.

[X] ZIP         ZIP support
[X] ZLIB
# make install clean

5)        安装Zend Optimizer

#cd /usr/ports/devel/ZendOptimizer/

#make                       #不要安装,只需要下载解包

#cd /usr/ports/devel/ZendOptimizer/work/ZendOptimizer-*

#./install-tty               ##会出现一个文字的安装界面,只是最后一步,不要选择apache就可以了

#vi /usr/local/etc/php.ini          #插入zend的路径,一般来说,上面的安装会自动加入下面的文字,假如没有,请手动添加。

[Zend]

zend_extension_manager.optimizer=/usr/local/Zend/lib/Optimizer-3.3.0

zend_extension_manager.optimizer_ts=/usr/local/Zend/lib/Optimizer_TS-3.3.0

zend_optimizer.version=3.3.0a

zend_extension=/usr/local/Zend/lib/ZendExtensionManager.so

zend_extension_ts=/usr/local/Zend/lib/ZendExtensionManager_TS.so

6)        安装nginx

#cd /usr/ports/www/nginx/

#make install

7)        安装lighttpd,为了得到fastcgi

# cd /usr/ports/www/lighttpd/

#make install

#rehash

8)        配置nginx

#user   nobody
删除前面的注释#,改成 user   www


#log_format  main  '$remote_addr - $remote_user [$time_local] '

#                      '"$request" $status $body_bytes_sent '

#                      '"$http_referer" "$http_user_agent"';

log_format  main  '$remote_addr - $remote_user [$time_local] '

                      '"$request" $status $body_bytes_sent '

                      '"$http_referer" "$http_user_agent"';

##以上步骤,为了能够正常分析log的pv,hits,访问量,才设定的,默认的log格式,是无法准确分析出所需要的结果

location / {
    root    /usr/local/www/nginx;
    index    index.html index.htm;
}
在index.html前面添加一个index.php
location / {
    root    /data/web/www.jk0086.com/htdocs;
    index    index.php index.html index.htm;
}


#location ~ \.php$ {
#    fastcgi_pass    127.0.0.1:9000;
#           fastcgi_index   index.php;
#           fastcgi_param     SCRIPT_FILENAME     /scripts$fastcgi_script.name;
#    include      fastcgi_params;
#}
将前面的#去掉,修改为
location ~ \.php$ {
    fastcgi_pass    127.0.0.1:9000;
           fastcgi_index   index.php;
           fastcgi_param     SCRIPT_FILENAME     /usr/local/etc/nginx$fastcgi_script.name;
    include      fastcgi_params;
}
##去掉注释,其实就是激活了nginx的fastcgi功能,也说明了nginx本身就已经准备用于fastcgi的环境中

9)        配置spawn-fcgi,就是一个启动fastcgi命令,使得nginx可以通过9000端口访问(纯粹个人理解-_-)

参数说明:****127.0.0.1的9000端口,进程数为64(如果服务器内存小于3GB,可以只开启25个进程),用户为www

/usr/local/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -u www -g www -C 64 -f /usr/local/bin/php-cgi

10)     这边请注意启动顺序,先启动fastcgi,再启动nginx

Nginx.conf由于经常需要修改,即经常需要重启nginx,因此这边写了一个启动脚本,请查看:

#!/usr/local/bin/bash

case $1 in

start)

        /usr/local/sbin/nginx

;;

stop)

        killall -9 nginx

;;

test)

        nginx -t -c /usr/local/etc/nginx/nginx.conf

;;

restart)

        ps auxww | grep nginx | grep master | awk '{print $2}' | xargs kill -HUP

;;

show)

        ps -aux|grep nginx

;;

esac
 


保存为nginx.sh使用方法为:

nginx.sh start   #启动nginx

nginx.sh stop   #停止nginx

nginx.sh restart #重启nginx

nginx.sh test   #测试nginx.conf的准确性

11)     安装phpMyAdmin

#cd /usr/ports/databases/phpmyadmin

#make install

#mv /usr/local/www/phpmyadmin /data/web/ www.jk0086.com/htdocs/

#cd /data/web/www.jk0086.com/htdocs/phpmyadmin

#vi config.inc.php   ##这边要说明一下,freebsd默认安装的phpmyadmin,里面配置文件有问题,需要手动修改,请修改成如下内容:

<?php

/* $Id: config.sample.inc.php 9689 2006-11-10 20:05:49Z nijel $ */

// vim: expandtab sw=4 ts=4 sts=4:

/**

 * phpMyAdmin sample configuration, you can use it as base for

 * manual configuration. For easier setup you can use scripts/setup.php

 *

 * All directives are explained in Documentation.html and on phpMyAdmin

 * wiki <http://wiki.cihar.com>.

 */

/*

 * This is needed for cookie based authentication to encrypt password in

 * cookie

 */

$cfg['blowfish_secret'] = 'asdf:LKJ'; /* YOU MUST FILL IN THIS FOR COOKIE AUTH! */

/*

 * Servers configuration

 */

$i = 0;

/*

 * First server

 */

$i++;

/* Authentication type */

$cfg['Servers'][$i]['auth_type'] = 'cookie';

/* Server parameters */

$cfg['Servers'][$i]['host'] = 'localhost';

$cfg['Servers'][$i]['connect_type'] = 'tcp';

$cfg['Servers'][$i]['compress'] = false;

/* Select mysqli if your server has it */

$cfg['Servers'][$i]['extension'] = 'mysql';

/* User for advanced features */

// $cfg['Servers'][$i]['controluser'] = 'pam';

// $cfg['Servers'][$i]['controlpass'] = 'pampasswd';

/* Advanced phpMyAdmin features */

// $cfg['Servers'][$i]['pmadb'] = 'phpmyadmin';

// $cfg['Servers'][$i]['bookmarktable'] = 'pma_bookmark';

// $cfg['Servers'][$i]['relation'] = 'pma_relation';

// $cfg['Servers'][$i]['table_info'] = 'pma_table_info';

// $cfg['Servers'][$i]['table_coords'] = 'pma_table_coords';

// $cfg['Servers'][$i]['pdf_pages'] = 'pma_pdf_pages';

// $cfg['Servers'][$i]['column_info'] = 'pma_column_info';

// $cfg['Servers'][$i]['history'] = 'pma_history';

// $cfg['Servers'][$i]['designer_coords'] = 'pma_designer_coords';

/*

 * End of servers configuration

 */

/*

 * Directories for saving/loading files from server

 */

$cfg['UploadDir'] = '';

$cfg['SaveDir'] = '';

?>

 
 


以上只是一个配置文件,当你打开Phpmyadmin的时候,会提示你输入用户名密码,然后登陆数据库,相对比较安全的

三、系统优化+防止ddos

2)        加载文件修改

# vi /boot/loader.conf    #加入如下文本

kern.dfldsiz="2147483648"               # Set the initial data size limit

kern.maxdsiz="2147483648"               # Set the max data size

kern.ipc.nmbclusters="0"        # Set the number of mbuf clusters

kern.ipc.nsfbufs="66560"                # Set the number of sendfile(2) bufs

##解释:

a.  第一,第二行主要是为了突破1G内存设置

b.  第三行其实是bsd的一个bug,当系统并发达到一个数量级的时候,系统会crash,这个是非常糟糕的事情,所幸更改了这个参数后,在高并发的时候,基本可以没有类似情况,当然非常bt的情况,还得进一步想办法

c.  第四行是读取的文件数,如果你下载的文件比较大,且比较多,加大这个参数,是非常爽的

3)        Sysctl修改

#vi /etc/rc.local

sysctl kern.ipc.maxsockets=100000    ##增加并发的socket,对于ddos很有用

sysctl kern.ipc.somaxconn=65535     ##打开文件数

sysctl net.inet.tcp.msl=2500          ##timeout时间
 


 

4)        通过上述的简单优化,会给你带来意外的惊喜,如果有兴趣的兄弟,可以尝试一下看看,绝无副作用。

四、其他

1)        加速ports安装

#vi /etc/make.conf     ##加入如下

MASTER_SITE_OVERRIDE?=http://ports.hshh.org/${DIST_SUBDIR}/

MASTER_SITE_OVERRIDE?=http://ports.cn.freebsd.org/${DIST_SUBDIR}/

2)        Freebsd颜色显示

secureCRT设置:仿真:终端->linux>勾选ANSI颜色-->确定

#vi /etc/csh.cshrc    ##加入如下

setenv LSCOLORS ExGxFxdxCxegedabagExEx

setenv CLICOLOR yes

#cd /usr/ports/edit/vim;make install

#echo "syntax on">/root/.vimrc

#echo "alias vi        vim" >>/root/.cshrc

##颜色主要是靠vim来显示的,因此需要安装vim,然后把vi alias成vim就可以了

3)        Other。。。。。。。。更新中