歡迎您光臨本站 註冊首頁

基於FreeBSD 8.0 Ports配置nginx+php+mysql高性能web平台【解決方案】

基於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
在下面增加options IPFIREWALL
options IPFIREWALL_VERBOSE
options IPFIREWALL_VERBOSE_LIMIT=10
options IPFIREWALL_DEFAULT_TO_ACCEPT複製代碼

編譯內核# cd ../compile/IPFW
# make cleandepend && make depend
# make && make install複製代碼
編譯完成後在/etc/rc.conf 最後添加以下語句firewall_enable="YES"
firewall_script="YES"
firewall_script="/etc/ipfw.conf"
firewall_quiet="NO"
firewall_logging_enable="YES"複製代碼
:編輯/etc/syslog.conf文件
  在文件最後加入如下內容:
  !ipfw *.* /var/log/ipfw.log
這行的作用是將IPFW的日誌寫到/var/log/ipfw.log文件里ee /etc/ipfw.conf

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

2.5 優化內核
ee /etc/sysctl.conf
security.bsd.see_other_uids=0
net.inet.tcp.sendspace=65536
net.inet.tcp.recvspace=65536
#net.inet.udp.sendspace=65536
net.inet.udp.maxdgram=65536
net.local.stream.sendspace=65536
net.inet.tcp.rfc1323=1
#net.inet.tcp.rfc1644=1
net.inet.tcp.rfc3042=1
net.inet.tcp.rfc3390=1
kern.ipc.maxsockbuf=2097152
kern.maxfiles=65536
kern.maxfilesperproc=32768
net.inet.tcp.delayed_ack=0
net.inet.icmp.drop_redirect=1
net.inet.icmp.log_redirect=1
net.inet.ip.redirect=0
net.inet.icmp.bmcastecho=0
net.inet.icmp.maskrepl=0
net.inet.icmp.icmplim=100
net.inet.icmp.icmplim_output=0
#net.inet.tcp.drop_synfin=1
net.inet.tcp.always_keepalive=1
net.inet.ip.intr_queue_maxlen=1000 (這兒太大了。建議不用或小點值)
net.inet.tcp.msl=7500 (太大了。建議2500-2000左右)
net.inet.tcp.blackhole=2
net.inet.tcp.inflight.enable=1
net.inet.ip.fastforwarding=0
kern.ipc.somaxconn=32768
kern.securelevel=0
#net.inet.tcp.log_in=1
#net.inet.udp.log_in=1
net.inet.udp.checksum=1
net.inet.tcp.syncookies=1
kern.ipc.shm_use_phys=1
kern.ipc.shmmax=67108864
kern.ipc.shmall=32768
kern.coredump=0
net.local.stream.recvspace=65536
net.local.dgram.maxdgram=16384
net.local.dgram.recvspace=65536
#net.inet.tcp.mssdflt=1460
#net.inet.tcp.minmss=1640
net.inet.raw.maxdgram=65535
net.inet.raw.recvspace=65535
net.inet.ip.fw.dyn_max=65535
net.inet.ipf.fr_tcpidletimeout=864000 複製代碼ee /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複製代碼

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

cd /usr/ports/databases/mysql55-server
ee Makefile
在第30行下面加上下面的參數--enable-thread-safe-client \
                 --disable-dtrace \
                 --enable-assembler \
                 --enable-assembler \
                 --with-big-tables \
                 --with-ssl \
                 --with-embedded-server \
                 --with-local-infile \
                 --with-plugins=partition,innobase,myisammrg
然後
#make install clean複製代碼
3.2 初始化資料庫/usr/local/bin/mysql_install_db --user=mysql
cp /usr/local/share/mysql/my-medium.cnf /etc/my.cnf複製代碼
3.3 /etc/rc.conf 增加mysql_enable="YES"echo 'mysql_enable="YES"' >> /etc/rc.conf複製代碼

3.4 安裝phpcd /usr/ports/lang/php52
#make config    ##配置編譯參數
CLI        Build CLI version
CGI        Build CGI version
[ ] APACHE     Build Apache module
[ ] DEBUG      Enable debug
] SUHOSIN Enable Suhosin protection system
MULTIBYTE Enable zend multibyte support
[ ] IPV6       Enable ipv6 support
REDIRECT   Enable force-cgi-redirect support (CGI only)
DISCARD    Enable discard-path support (CGI only)
FASTCGI    Enable fastcgi support (CGI only)
PATHINFO   Enable path-info-check support (CGI only)
make install clean複製代碼
# cp /usr/local/etc/php.ini-dist /usr/local/etc/php.ini
3.5 配置php.ini
ee php.ini
找到如下語句
;open_basedir =
disable_functions =
expose_php = On
expose_php = Off
display_errors = On
output_buffering = Off複製代碼
修改為open_basedir = /data/www/wwwroot:/tmp
disable_functions =
phpinfo,passthru,exec,system,chroot,scandir,chgrp,chown,shell_exec,proc_open,proc_get_status,ini_alter,ini_alter,ini_restore,dl,pfsock
open,openlog,syslog,readlink,symlink,popepassthru,stream_socket_server
display_errors = Off

output_buffering = On複製代碼
3.6 安裝php52-extensions# cd /usr/ports/lang/php52-extensions/
#make config
Options for php52-extensions 1.3
-------------------------------------------------
CURL        CURL support
FTP        FTP support
GD        
GETTEXT
MBSTRING multibyte string support
MCRYPT      Encryption support  
MYSQL
PCRE        Perl Compatible Regular Expression support
[ ] POSIX //去掉
[ ] SQLITE //去掉.
ZIP         ZIP support
ZLIB
# make install clean複製代碼
3.7 安裝php擴展模塊安裝eacceleratorcd /usr/ports/www/eaccelerator
make install clean複製代碼
ee /usr/local/etc/php.ini
在後面加上zend_extension="/usr/local/lib/php/extensions/no-debug-non-zts-
20060613/eaccelerator.so"
eaccelerator.shm_size="32"
eaccelerator.cache_dir="/data/cache/ea"
eaccelerator.enable="1"
eaccelerator.optimizer="1"
eaccelerator.check_mtime="1"
eaccelerator.debug="0"
eaccelerator.filter=""
eaccelerator.shm_max="0"
eaccelerator.shm_ttl="0"
eaccelerator.shm_prune_period="0"
eaccelerator.shm_only="0"
eaccelerator.compress="1"
eaccelerator.compress_level="9"複製代碼
安裝memcachecd /usr/ports/databases/pecl-memcache
make install clean複製代碼
安裝pdo_mysqlcd /usr/ports/databases/php52-pdo_mysql
make install clean複製代碼
安裝ImageMagick# cd /usr/ports/graphics/ImageMagick/
# make install clean複製代碼

看是否安裝成功 convert --version
PHP還需安裝兩個.so
安裝 php imagick extend (imagick.so)# cd /usr/ports/graphics/pecl-imagick
# make install clean複製代碼
安裝 php magickwand extend (magickwand.so)# cd /usr/ports/graphics/php-magickwand
# make install clean複製代碼
關於magicwand的說明:http://www.imagemagick.org/script/api.php#php
然後看下擴展<?php
phpinfo();
?>安裝lighttpd 獲得spawn-fcgicd /usr/ports/www/lighttpd
make install clean複製代碼
安裝nginxcd /usr/ports/www/nginx
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開機啟動echo 'spawn_fcgi_enable="YES" '>> /etc/rc.conf複製代碼echo 'nginx_enable="YES"' >> /etc/rc.conf複製代碼
3.8 整合nginx和phpee fastcgi_params

#fix_php_path
if ($request_filename ~* (.*)\.php) {
      set $php_url $1;
}
if (!-e $php_url.php) {
      return 403;
}
fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;
fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;
fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;
fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;
# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param  REDIRECT_STATUS    200;複製代碼


ee nginx.conf
user  www www;
worker_processes 10;
pid /var/log/nginx.pid;
# [ debug | info | notice | warn | error | crit ]
error_log  /dev/null;
#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 51200;
events
{
         use kqueue; #FreeSBD下要啟用kqueue,Linux下是epoll   
         #maxclient = worker_processes * worker_connections / cpu_number
         worker_connections 51200;
}
http
{
         include       /usr/local/nginx/conf/mime.types;
         default_type  application/octet-stream;
         charset  gb2312;
         server_names_hash_bucket_size 128;
         client_header_buffer_size 32k;
         large_client_header_buffers 4 32k;
         log_format  main  '$remote_addr - $remote_user [$time_local] $request '  '"$status" $body_bytes_sent
                                       "$http_referer" ''"$http_user_agent" "$http_x_forwarded_for"';
         #access_log  /data/www/logs/access.log  main;
          access_log  /dev/null;
         sendfile on;
         tcp_nopush     on;
         
         keepalive_timeout 60;
         tcp_nodelay on;
         fastcgi_connect_timeout 300;
         fastcgi_send_timeout 300;
         fastcgi_read_timeout 300;
         
         fastcgi_buffer_size 128k;
         fastcgi_buffers 4 128k;
         fastcgi_busy_buffers_size 128k;
         fastcgi_temp_file_write_size 128k;
         fastcgi_intercept_errors on;
         client_max_body_size       50m;
         client_body_buffer_size    256k;
         
         gzip on;
         gzip_min_length  1k;
         gzip_buffers     4 16k;
         gzip_http_version 1.0;
         gzip_comp_level 2;
         gzip_types       text/plain application/x-javascript text/css application/xml;
         gzip_vary on;
         #
         proxy_temp_path            /dev/proxy_temp;
         fastcgi_temp_path          /dev/fastcgi_temp;
         client_body_temp_path      /dev/client_body_temp;
         
          server
       {
          listen          80;
          server_name   www.linuxtone.org;
          root      /data/www/wwroot;
          index      index.html index.htm index.php;

      
     location ~ \.php$ {
           #root           html;
             fastcgi_pass   127.0.0.1:9000;
             fastcgi_index  index.php;
             #fastcgi_param  SCRIPT_FILENAME          /data/www/wwwroot$fastcgi_script_name;
             include        fastcgi_params;
         }
           log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                       '$status $body_bytes_sent "$http_referer" '
                       '"$http_user_agent" "$http_x_forwarded_for"';
     access_log  logs/access.log  main;
         
   }
}複製代碼

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
《解決方案》

謝謝分享

[火星人 ] 基於FreeBSD 8.0 Ports配置nginx+php+mysql高性能web平台【解決方案】已經有1237次圍觀

http://coctec.com/docs/service/show-post-2377.html