freebsd+nginx+php+mysql+zend系統優化防止ddos +傻瓜式ports安裝

前言php

1. 看了張宴的《linux版本的Nginx 0.5.33 + PHP 5.2.5》後,立刻找了一臺服務器測試了一下,而後加了一個線上的discuz的論壇跑了一下,性能很是的卓越,感嘆搜遍整個互聯網,就是 nginx+php惟一的一篇文檔。對於他的文檔,我認爲:寫的很是專業,對於php extensions庫也分析的很是透徹。可是一篇專業的文檔,也存在一些專業帶來的麻煩,就是起點有點高,假如要更新裏面的軟件包和定義本身的目錄,勢 必須要一點波折。另外不知道爲什麼,最近網站總是打不開。情急之下,小弟從新寫了一篇freebsd下面的傻瓜式安裝文檔。html

2. 又結合了平時的一些經驗,順便把如何防卸ddos,並經過對系統的優化,如何來進一步提升nginx的併發數作 一些詳解。mysql

3. 因爲我的能力有限,錯誤的地方,還請讀者諒解linux

1、安裝軟件前的準備nginx

系統的安裝:插入freebsd6.2以上的光盤,最小化安裝系統,同時安裝好portsweb

2、手動安裝nginx+phpsql

1) 進入系統後,準備cvs更新:數據庫

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

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

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,裏面配置文件有問題,須要手動修改,請修改爲以下內容:

 

/* $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

*/

/*

* 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的時候,會提示你輸入用戶名密碼,而後登錄數據庫,相對比較安全的

3、系統優化+防止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) 經過上述的簡單優化,會給你帶來意外的驚喜,若是有興趣的兄弟,能夠嘗試一下看看,絕無反作用。

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

相關文章
相關標籤/搜索