标题:转freebsd+nginx+php+mysql+zend+phpmyadmin+系统优化+防止ddos +傻瓜式ports安装法 出处:BIWEB开源PHP WMS系统创始人ArthurXF肖飞的blog 时间:Wed, 09 Jun 2010 14:24:49 +0000 作者:ArthurXF 地址:http://www.bizeway.net/read.php/556.htm 内容: 看了张宴的《linux版本的Nginx 0.5.33 + PHP 5.2.5》后,马上找了一台服务器测试了一下,然后加了一个线上的discuz的论坛跑了一下,性能非常的卓越,感叹搜遍整个互联网,就是nginx+php唯一的一篇文档。对于他的文档,我认为:写的非常专业,对于php extensions库也分析的非常透彻。但是一篇专业的文档,也存在一些专业带来的麻烦,就是起点有点高,假如要更新里面的软件包和定义自己的目录,势必需要一点波折。另外不知道为何,最近网站老是打不开。情急之下,小弟重新写了一篇freebsd下面的傻瓜式安装文档。 2. 又结合了平时的一些经验,顺便把如何防卸ddos,并通过对系统的优化,如何来进一步提高nginx的并发数做 一些详解。 3. 由于个人能力有限,错误的地方,还请读者谅解 一、安装软件前的准备 系统的安装:插入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 thread-safe-client=enable dtrace=disable assembler=enable with_big_tables=yes with_embedded_server=yes with_local_infile=yes with_plugins=partition,innobase,myisammrg WITH_CHARSET=utf8 WITH_XCHARSET=all WITH_COLLATION=utf8_general_ci WITH_PROC_SCOPE_PTH=yes BUILD_OPTIMIZED=yes BUILD_STATIC=yes SKIP_DNS_CHECK=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 配置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的时候,会提示你输入用户名密码,然后登陆数据库,相对比较安全的 三、系统优化+防止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。。。。。。。。更新中 Generated by Bo-blog 2.0.3 sp1