免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3258 | 回复: 1
打印 上一主题 下一主题

基于FreeBSD 8.0 Ports配置nginx+php+mysql高性能web平台【解决方案】 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-04-27 17:47 |只看该作者 |倒序浏览
基于FreeBSD 8.0 Ports配置nginx+php+mysql高性能web平台【解决方案】



FreeBSD简介:

      FreeBSD,是AT&T的后裔间接通过Berkeley Software Distribution(BSD)伯克利软件发行版UNIX,其漫长而动荡的历史可以追溯到1993年。不同于Linux发行版,这被定义为集成 Linux内核和数以千计的应用软件组成的解决方案,FreeBSD是一个紧密集成的操作系统基于BSD kernel和所谓的“userland(用户建造)”(因此,甚至没有额外的扩展程序)。发行版巨大失去了在普通电脑系统安装的机会 ? 像许多Linux发行版本,一个易于安装的(大部分)开放源码的应用扩展提供了FreeBSD的核心,但这些通常是由第三方捐助者提供的和不严格的 FreeBSD的一部分。
FreeBSD开发作为一个快速,高性能和极其稳定的操作系统,尤其是适合网络服务器和类似任务。许多大型搜索引擎网站,或组织的关键任务计算基础设施上部署和使用,并在他们的计算机系统上运行FreeBSD多年。相较于Linux操作系统,FreeBSD是分布在一个更宽松的许可证,这几乎可以无限制地重复使用和源代码修改为任何目的。即使是知名的苹果的Mac OS X也是从BSD派生。除了核心的操作系统,该项目还提供超过15,000种二进制和源代码形式
FreeBSD软件方便的应用于BSD核心安装。虽然可以肯定的是FreeBSD能够作为桌面操作系统使用,但它在这方面不具有Linux发行版流行。文本模式的安装程序提供的系统硬件检测或系统配置方面并没有,留下复杂的工作,这让在安装后的用户来设置。在现代的硬件支持方面,FreeBSD普遍滞后Linux操作系统,特别是在支持笔记本电脑和上网本等小工具,如无线网卡或流行数码相机。这些用户寻求现有的桌面FreeBSD的项目是考虑利用FreeBSD在台式机或工作站的速度和稳定,而不是FreeBSD的自身。


一.安装准备:
1.1、操作系统安装:参见http://bbs.linuxtone.org/thread-6422-1-1.html附件
默认FreeBSD不允许Root直接ssh登陆,在安装系统时候可以添加一个wheel组的用户。以确保FreeBSD系统安全!切记:FreeBSD只允许wheel组的用户可以ssh登陆。

例如:pw useradd coralzd -g coralzd


1.2 系统环境

FreeBSD 8.0 I386
Nginx 0.7.65
PHP 5.2.13
Mysql 5.5.4 M3
复制代码

二、系统初始化
2.1 更新ports
用portsnap 更新ports树,已获得最新的软件。
修改portsnap配置文件的server 为国内的镜像
ee /etc/porsnap.conf


将SERVERNAME=portsnap.freebsd.org
修改为SERVERNAME=portsnap.hshh.org

#初次运行portsnap fetch extract
以后运行portsnap fetch update
复制代码
2.2 优化rc.conf配置文档
添加syslogd_flags="-ss"
sendmail_enable="NONE"
2.3 内核增加IPFW防火墙
cd /usr/src/sys/i386/conf
cp GENERIC IPFW
ee IPFW
将ident GENERIC
修改为ident IPFW
在下面增加
  1. options IPFIREWALL
  2. options IPFIREWALL_VERBOSE
  3. options IPFIREWALL_VERBOSE_LIMIT=10
  4. options IPFIREWALL_DEFAULT_TO_ACCEPT
复制代码
复制代码

编译内核
  1. # cd ../compile/IPFW
  2. # make cleandepend && make depend
  3. # make && make install
复制代码
复制代码
编译完成后在/etc/rc.conf 最后添加以下语句
  1. firewall_enable="YES"
  2. firewall_script="YES"
  3. firewall_script="/etc/ipfw.conf"
  4. firewall_quiet="NO"
  5. firewall_logging_enable="YES"
复制代码
复制代码
:编辑/etc/syslog.conf文件
  在文件最后加入如下内容:
  !ipfw *.* /var/log/ipfw.log
这行的作用是将IPFW的日志写到/var/log/ipfw.log文件里
  1. ee /etc/ipfw.conf

  2. #!/bin/sh
  3. IPFW='/sbin/ipfw -q'
  4. $IPFW -f flush
  5. $IPFW add 2000 allow ip from any to any via lo*   
  6. $IPFW add 2010 deny log ip from 127.0.0.0/8 to any in   
  7. $IPFW add 2020 deny log ip from any to 127.0.0.0/8 in   
  8. $IPFW add 2030 deny log ip from 224.0.0.0/3 to any in   
  9. $IPFW add 2040 deny log tcp from any to 224.0.0.0/3 in   
  10. $IPFW add 2050 allow log tcp from any to any out   
  11. $IPFW add 2060 allow log tcp from any to any established   
  12. $IPFW add 2070 allow log tcp from any to any 22 in   
  13. $IPFW add 2080 allow log tcp from any to any 80 in      
  14. $IPFW add 12190 deny log tcp from any to any   
复制代码
复制代码
2.4 安装axel提高ports的安装速度
  1. cd /usr/ports/ftp/axel
  2. make install
  3. #修改 /et/make.conf
  4. ee /etc/make.conf
  5. #加入以下内容
  6. FETCH_CMD=axel
  7. FETCH_BEFORE_ARGS= -n 10 -a
  8. FETCH_AFTER_ARGS=
  9. DISABLE_SIZE=yes
  10. MASTER_SITE_OVERRIDE?=\
  11. http://ports.hshh.org/${DIST_SUBDIR}/\
  12. http://ports.cn.freebsd.org/${DIST_SUBDIR}/\
  13. ftp://ftp.freeBSDchina.org/pub/FreeBSD/ports/distfiles/${DIST_SUBDIR}/
  14. MASTER_SITE_OVERRIDE?=${MASTER_SITE_BACKUP
复制代码
复制代码

2.5 优化内核

  1. ee /etc/sysctl.conf
  2. security.bsd.see_other_uids=0
  3. net.inet.tcp.sendspace=65536
  4. net.inet.tcp.recvspace=65536
  5. #net.inet.udp.sendspace=65536
  6. net.inet.udp.maxdgram=65536
  7. net.local.stream.sendspace=65536
  8. net.inet.tcp.rfc1323=1
  9. #net.inet.tcp.rfc1644=1
  10. net.inet.tcp.rfc3042=1
  11. net.inet.tcp.rfc3390=1
  12. kern.ipc.maxsockbuf=2097152
  13. kern.maxfiles=65536
  14. kern.maxfilesperproc=32768
  15. net.inet.tcp.delayed_ack=0
  16. net.inet.icmp.drop_redirect=1
  17. net.inet.icmp.log_redirect=1
  18. net.inet.ip.redirect=0
  19. net.inet.icmp.bmcastecho=0
  20. net.inet.icmp.maskrepl=0
  21. net.inet.icmp.icmplim=100
  22. net.inet.icmp.icmplim_output=0
  23. #net.inet.tcp.drop_synfin=1
  24. net.inet.tcp.always_keepalive=1
  25. net.inet.ip.intr_queue_maxlen=1000 (这儿太大了。建议不用或小点值)
  26. net.inet.tcp.msl=7500 (太大了。建议2500-2000左右)
  27. net.inet.tcp.blackhole=2
  28. net.inet.tcp.inflight.enable=1
  29. net.inet.ip.fastforwarding=0
  30. kern.ipc.somaxconn=32768
  31. kern.securelevel=0
  32. #net.inet.tcp.log_in=1
  33. #net.inet.udp.log_in=1
  34. net.inet.udp.checksum=1
  35. net.inet.tcp.syncookies=1
  36. kern.ipc.shm_use_phys=1
  37. kern.ipc.shmmax=67108864
  38. kern.ipc.shmall=32768
  39. kern.coredump=0
  40. net.local.stream.recvspace=65536
  41. net.local.dgram.maxdgram=16384
  42. net.local.dgram.recvspace=65536
  43. #net.inet.tcp.mssdflt=1460
  44. #net.inet.tcp.minmss=1640
  45. net.inet.raw.maxdgram=65535
  46. net.inet.raw.recvspace=65535
  47. net.inet.ip.fw.dyn_max=65535
  48. net.inet.ipf.fr_tcpidletimeout=864000
复制代码
复制代码
  1. ee /boot/loader.conf

  2. kern.dfldsiz="2147483648"               # Set the initial data size limit
  3. kern.maxdsiz="2147483648"               # Set the max data size
  4. kern.ipc.nmbclusters="0"        # Set the number of mbuf clusters
  5. kern.ipc.nsfbufs="66560"                # Set the number of sendfile(2) bufs
复制代码
复制代码

三、配置mysql php nginx
3.1 安装mysql 5.5.4 m3

cd /usr/ports/databases/mysql55-server
ee Makefile
在第30行下面加上下面的参数
  1. --enable-thread-safe-client \
  2.                  --disable-dtrace \
  3.                  --enable-assembler \
  4.                  --enable-assembler \
  5.                  --with-big-tables \
  6.                  --with-ssl \
  7.                  --with-embedded-server \
  8.                  --with-local-infile \
  9.                  --with-plugins=partition,innobase,myisammrg
  10. 然后
  11. #make install clean
复制代码
复制代码
3.2 初始化数据库
  1. /usr/local/bin/mysql_install_db --user=mysql
  2. cp /usr/local/share/mysql/my-medium.cnf /etc/my.cnf
复制代码
复制代码
3.3 /etc/rc.conf 增加mysql_enable="YES"
  1. echo 'mysql_enable="YES"' >> /etc/rc.conf
复制代码
复制代码

3.4 安装php
  1. cd /usr/ports/lang/php52
  2. #make config    ##配置编译参数
  3. [X] CLI        Build CLI version
  4. [X] CGI        Build CGI version
  5. [ ] APACHE     Build Apache module
  6. [ ] DEBUG      Enable debug
  7. [X]] SUHOSIN Enable Suhosin protection system
  8. [X] MULTIBYTE Enable zend multibyte support
  9. [ ] IPV6       Enable ipv6 support
  10. [X] REDIRECT   Enable force-cgi-redirect support (CGI only)
  11. [X] DISCARD    Enable discard-path support (CGI only)
  12. [X] FASTCGI    Enable fastcgi support (CGI only)
  13. [X] PATHINFO   Enable path-info-check support (CGI only)
  14. make install clean
复制代码
复制代码
# cp /usr/local/etc/php.ini-dist /usr/local/etc/php.ini
3.5 配置php.ini
ee php.ini
找到如下语句
;
  1. open_basedir =
  2. disable_functions =
  3. expose_php = On
  4. expose_php = Off
  5. display_errors = On
  6. output_buffering = Off
复制代码
复制代码
修改为
  1. open_basedir = /data/www/wwwroot:/tmp
  2. disable_functions =
  3. phpinfo,passthru,exec,system,chroot,scandir,chgrp,chown,shell_exec,proc_open,proc_get_status,ini_alter,ini_alter,ini_restore,dl,pfsock
  4. open,openlog,syslog,readlink,symlink,popepassthru,stream_socket_server
  5. display_errors = Off

  6. output_buffering = On
复制代码
复制代码
3.6 安装php52-extensions
  1. # cd /usr/ports/lang/php52-extensions/
  2. #make config
  3. Options for php52-extensions 1.3
  4. -------------------------------------------------
  5. [X] CURL        CURL support
  6. [X] FTP        FTP support
  7. [X] GD        
  8. [X] GETTEXT
  9. [X] MBSTRING multibyte string support
  10. [X] MCRYPT      Encryption support  
  11. [X] MYSQL
  12. [X] PCRE        Perl Compatible Regular Expression support
  13. [ ] POSIX //去掉
  14. [ ] SQLITE //去掉.
  15. [X] ZIP         ZIP support
  16. [X] ZLIB
  17. # make install clean
复制代码
复制代码
3.7 安装php扩展模块安装eaccelerator
  1. cd /usr/ports/www/eaccelerator
  2. make install clean
复制代码
复制代码
ee /usr/local/etc/php.ini
在后面加上
  1. zend_extension="/usr/local/lib/php/extensions/no-debug-non-zts-
  2. 20060613/eaccelerator.so"
  3. eaccelerator.shm_size="32"
  4. eaccelerator.cache_dir="/data/cache/ea"
  5. eaccelerator.enable="1"
  6. eaccelerator.optimizer="1"
  7. eaccelerator.check_mtime="1"
  8. eaccelerator.debug="0"
  9. eaccelerator.filter=""
  10. eaccelerator.shm_max="0"
  11. eaccelerator.shm_ttl="0"
  12. eaccelerator.shm_prune_period="0"
  13. eaccelerator.shm_only="0"
  14. eaccelerator.compress="1"
  15. eaccelerator.compress_level="9"
复制代码
复制代码
安装memcache
  1. cd /usr/ports/databases/pecl-memcache
  2. make install clean
复制代码
复制代码
安装pdo_mysql
  1. cd /usr/ports/databases/php52-pdo_mysql
  2. make install clean
复制代码
复制代码
安装ImageMagick
  1. # cd /usr/ports/graphics/ImageMagick/
  2. # make install clean
复制代码
复制代码

看是否安装成功 convert --version
PHP还需安装两个.so
安装 php imagick extend (imagick.so)
  1. # cd /usr/ports/graphics/pecl-imagick
  2. # make install clean
复制代码
复制代码
安装 php magickwand extend (magickwand.so)
  1. # cd /usr/ports/graphics/php-magickwand
  2. # make install clean
复制代码
复制代码
关于magicwand的说明:http://www.imagemagick.org/script/api.php#php
然后看下扩展
  1. <?php
  2. phpinfo();
  3. ?>
复制代码
安装lighttpd 获得spawn-fcgi
  1. cd /usr/ports/www/lighttpd
  2. make install clean
复制代码
复制代码
安装nginx
  1. cd /usr/ports/www/nginx
  2. make install clean
复制代码
复制代码
修改php.ini cgi.fix_pathinfo=1,让SCRIPT_FILENAME有效
安装Zendoptimizer
cd /usr/ports/devel/ZendOptimizer/
make (不要安装,只需下载下来即可)
cd work/ZendOptimizer-3.3.0a-freebsd6.0-i386
./install-tty 一路按回车,到最后选择no,不使用apache。
zend 将自动在php.ini最后添加参数的。
让nginx和spawn-fcgi开机启动
  1. echo 'spawn_fcgi_enable="YES" '>> /etc/rc.conf
复制代码
复制代码
  1. echo 'nginx_enable="YES"' >> /etc/rc.conf
复制代码
复制代码
3.8 整合nginx和php
  1. ee fastcgi_params

  2. #fix_php_path
  3. if ($request_filename ~* (.*)\.php) {
  4.       set $php_url $1;
  5. }
  6. if (!-e $php_url.php) {
  7.       return 403;
  8. }
  9. fastcgi_param  QUERY_STRING       $query_string;
  10. fastcgi_param  REQUEST_METHOD     $request_method;
  11. fastcgi_param  CONTENT_TYPE       $content_type;
  12. fastcgi_param  CONTENT_LENGTH     $content_length;
  13. fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
  14. fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
  15. fastcgi_param  REQUEST_URI        $request_uri;
  16. fastcgi_param  DOCUMENT_URI       $document_uri;
  17. fastcgi_param  DOCUMENT_ROOT      $document_root;
  18. fastcgi_param  SERVER_PROTOCOL    $server_protocol;
  19. fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
  20. fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;
  21. fastcgi_param  REMOTE_ADDR        $remote_addr;
  22. fastcgi_param  REMOTE_PORT        $remote_port;
  23. fastcgi_param  SERVER_ADDR        $server_addr;
  24. fastcgi_param  SERVER_PORT        $server_port;
  25. fastcgi_param  SERVER_NAME        $server_name;
  26. # PHP only, required if PHP was built with --enable-force-cgi-redirect
  27. fastcgi_param  REDIRECT_STATUS    200;
复制代码
复制代码


ee nginx.conf

  1. user  www www;
  2. worker_processes 10;
  3. pid /var/log/nginx.pid;
  4. # [ debug | info | notice | warn | error | crit ]
  5. error_log  /dev/null;
  6. #Specifies the value for maximum file descriptors that can be opened by this process.
  7. worker_rlimit_nofile 51200;
  8. events
  9. {
  10.          use kqueue; #FreeSBD下要启用kqueue,Linux下是epoll   
  11.          #maxclient = worker_processes * worker_connections / cpu_number
  12.          worker_connections 51200;
  13. }
  14. http
  15. {
  16.          include       /usr/local/nginx/conf/mime.types;
  17.          default_type  application/octet-stream;
  18.          charset  gb2312;
  19.          server_names_hash_bucket_size 128;
  20.          client_header_buffer_size 32k;
  21.          large_client_header_buffers 4 32k;
  22.          log_format  main  '$remote_addr - $remote_user [$time_local] $request '  '"$status" $body_bytes_sent
  23.                                        "$http_referer" ''"$http_user_agent" "$http_x_forwarded_for"';
  24.          #access_log  /data/www/logs/access.log  main;
  25.           access_log  /dev/null;
  26.          sendfile on;
  27.          tcp_nopush     on;
  28.          
  29.          keepalive_timeout 60;
  30.          tcp_nodelay on;
  31.          fastcgi_connect_timeout 300;
  32.          fastcgi_send_timeout 300;
  33.          fastcgi_read_timeout 300;
  34.          
  35.          fastcgi_buffer_size 128k;
  36.          fastcgi_buffers 4 128k;
  37.          fastcgi_busy_buffers_size 128k;
  38.          fastcgi_temp_file_write_size 128k;
  39.          fastcgi_intercept_errors on;
  40.          client_max_body_size       50m;
  41.          client_body_buffer_size    256k;
  42.          
  43.          gzip on;
  44.          gzip_min_length  1k;
  45.          gzip_buffers     4 16k;
  46.          gzip_http_version 1.0;
  47.          gzip_comp_level 2;
  48.          gzip_types       text/plain application/x-javascript text/css application/xml;
  49.          gzip_vary on;
  50.          #
  51.          proxy_temp_path            /dev/proxy_temp;
  52.          fastcgi_temp_path          /dev/fastcgi_temp;
  53.          client_body_temp_path      /dev/client_body_temp;
  54.          
  55.           server
  56.        {
  57.           listen          80;
  58.           server_name   www.linuxtone.org;
  59.           root      /data/www/wwroot;
  60.           index      index.html index.htm index.php;

  61.       
  62.      location ~ \.php$ {
  63.            #root           html;
  64.              fastcgi_pass   127.0.0.1:9000;
  65.              fastcgi_index  index.php;
  66.              #fastcgi_param  SCRIPT_FILENAME          /data/www/wwwroot$fastcgi_script_name;
  67.              include        fastcgi_params;
  68.          }
  69.            log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
  70.                        '$status $body_bytes_sent "$http_referer" '
  71.                        '"$http_user_agent" "$http_x_forwarded_for"';
  72.      access_log  logs/access.log  main;
  73.          
  74.    }
  75. }
复制代码
复制代码

3.9 安装phpmyadmin
cd  /usr/ports/databases/phpmyadmin/
make (只需获得软件包就可以了)
cd work && mv phpMyAdmin* /data/www/wwroot/phpmyadmin
复制代码
编辑 config.default.php
cd phpmyadmin/lib*
ee config.default.php
将$cfg['blowfish_secret'] = '';
修改为$cfg['blowfish_secret'] = 'cookie';
复制代码
然后就可以通过phpmyadmin 来管理mysql了!
文档修改历史
V1.0 初稿完成
V1.1 整理文档排版,增加fastcgi_params,修复php_pathinfo漏洞。
V1.2 修改nginx.conf 不正确的地方;整理/etc/make.conf 笔误。
V1.3 增加phpmyadmin 管理,修改spawn_fcgi 启动参数。
V1.4 修改nginx.conf配置文档和编译内核笔误!
参考文章:
http://willzh.javaeye.com/blog/252362
http://hi.baidu.com/cajecx/blog/item/279f00e903578036b80

论坛徽章:
0
2 [报告]
发表于 2011-04-28 10:00 |只看该作者
谢谢分享
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP