歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Unix知識 >> BSD >> freebsd6.2 nginx+php+mysql+zend系統優化防止ddos攻擊

freebsd6.2 nginx+php+mysql+zend系統優化防止ddos攻擊

日期:2017/3/1 18:14:03   编辑:BSD

一、安裝軟件前的准備

系統的安裝:插入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

小提示:安裝zend,在freebsd下面,目前只有支持到php5.1.x,對於php5.2.x還不能支持,因為zend還沒有為php5.2.x開發版本,在網上查了好多關於解決此類的問題,但得到的結果是,zend可以順利安裝,phpinfo也顯示zend正常了,但程序無法調用,即zend沒有工作,也就是目前無法解決,我想只有等到zend php5.2.x的版本後,才可以解決此問題,也希望哪位已經解決此類問題的兄弟,share一下你的經驗。假如你非要使用zend,那就請你將php降到5.1.x,或者你不當心已經升級了ports,那建議你可以安裝php4.x,畢竟目前php4.x還通用於大部分的環境

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

1) 這個話題有點大,我相信我做的只是其中很小的一部分,同時很多人也會問我,是不是要編譯內核,這邊的回答是不需要編譯任何內核,只需要copy文件,然後重啟一下服務器就可以了。

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。。。。。。。。更新中

Copyright © Linux教程網 All Rights Reserved