免费注册 查看新帖 |

Chinaunix

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

基于FreeBSD 8.0搭建nginx+php+php-fpm+mysql高性能php平台 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-05-24 12:08 |只看该作者 |倒序浏览
基于FreeBSD 8.0搭建nginx+php+php-fpm+mysql高性能php平台



FreeBSD简介:
FreeBSD是一种自由类Unix操作系统,是由经过BSD、386BSD和4.4BSD发展而来的类Unix的一个重要分支。FreeBSD拥有超过200名活跃开发者和上千名贡献者。
FreeBSD被认为是自由操作系统中的不知名的巨人。它不是Unix,但如Unix一样运行,具有兼容Unix的系统API。作为一个操作系统,FreeBSD被认为相当稳建可靠。
FreeBSD是以一个完善的操作系统的定位来做开发。其内核、驱动程序以及所有的用户层(Userland)应用程序(比方说是Shell)均由同一源代码版本控制系统保存(目前使用Subversion并与CVS兼容)。相较于另一知名的的操作系统Linux,其内核为一组开发人员设计,而用户应用程序则交由他人开发(例如GNU计划),最后再由其他团体集成并包装成Linux包。
FreeBSD默认是无桌面环境的命令行界面,想要使用桌面环境必须自行安装,或是使用PC-BSD之类的桌面发行版。


一. 安装准备:
1.1、操作系统安装:
参见http://bbs.linuxtone.org/thread-6422-1-1.html附件
补充:配置sshd让root登陆

  1. # vi /etc/rc.d/sshd
复制代码
复制代码
修改下面选项为如下:
  1. PermitRootLogin yes
  2. PasswordAuthentication yes
  3. UseDNS no
复制代码
复制代码
1.2、平台环境:

  1. FreeBSD- 8.0-RELEASE_i386
  2. nginx-0.8.38
  3. php-5.2.13
  4. php-5.2.13-fpm-0.5.14
  5. mysql-5.1.46
复制代码
复制代码
1.3、系统分区:
  1. /   40G
  2. swap   4GB
  3. /var   5GB
  4. /tmp   5MB
  5. /data  剩余所有分区
  6. 注:具体分区请根据相关业务划分
复制代码
复制代码
1.4、系统约定:

  1. 软件源代码包存放位置             /usr/local/src
  2. 源码包编译安装位置(prefix)     /usr/local/software_name
  3. 脚本以及维护程序存放位置       /usr/local/sbin
  4. MySQL 数据库位置                /data/mysql/data(可按情况设置)
  5. 网站根目录                          /data/www/wwwroot(可按情况设置)
  6. 虚拟主机日志根目录               /data/logs(可按情况设置)
  7. Nginx运行账户                     www:www
  8. install_software_name.sh     //存放编译参数脚本习惯将所有编译脚本存放在install_software_name.sh便于升级和更新软件
复制代码
复制代码
1.5、定时校正服务器时钟,定时与中国国家授时中心授时服务器同步
  1. #crontab ?e

  2. 15 3 * * * /usr/sbin/ntpdate 210.72.145.44 > /dev/null 2>&1
复制代码
复制代码
1.6、下载软件包:

  1. wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.02.tar.gz
  2. wget http://nginx.org/download/nginx-0.8.37.tar.gz
  3. wget http://mirrors.sohu.com/mysql/MySQL-5.1/mysql-5.1.46.tar.gz
  4. wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.13.tar.gz
  5. wget http://sourceforge.net/projects/mcrypt/files/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.gz/download
  6. wget http://sourceforge.net/projects/mhash/files/mhash/0.9.9.9/mhash-0.9.9.9.tar.gz/download
  7. wget http://sourceforge.net/projects/mcrypt/files/MCrypt/2.6.8/mcrypt-2.6.8.tar.gz/download
  8. wget http://php-fpm.org/downloads/php-5.2.13-fpm-0.5.14.diff.gz
  9. wget http://cn.php.net/get/php-5.2.13.tar.gz/from/this/mirror
  10. wget http://pecl.php.net/get/memcache-2.2.5.tgz
  11. wget http://bart.eaccelerator.net/source/0.9.6/eaccelerator-0.9.6.tar.bz2
  12. wget http://pecl.php.net/get/PDO_MYSQL-1.0.2.tgz
  13. wget http://blog.s135.com/soft/linux/nginx_php/imagick/ImageMagick.tar.gz
  14. wget http://pecl.php.net/get/imagick-2.2.2.tgz
  15. wget http://docs.linuxtone.org/soft/bsder/ZendOptimizer-3.3.0a-freebsd6.0-i386.tar.gz
复制代码
复制代码
1.7、利用ports安装基本包
1.7.1安装wget

  1. # cd /usr/ports/ftp/wget
  2. #make install clean
复制代码
复制代码#在出现的对话框中去掉IPV6前面的选择
#接下来对话框全部默认皆可
刚刚安装完后,wget并未能使用,需要执行下面的命令后方可生效。
  1. #rehash
复制代码
复制代码
1.7.2安装其他基本的软件包(出现对话框默认皆可)
1.7.2.1

  1. #cd /usr/ports/textproc/libxml2
  2. make install clean
复制代码
复制代码1.7.2.2

  1. #/usr/ports/ftp/curl
  2. make install clean
复制代码
复制代码1.7.2.3

  1. #/usr/ports/graphics/jpeg
  2. make install clean
复制代码
复制代码1.7.2.4

  1. #/usr/ports/graphics/png
  2. make install clean
复制代码
复制代码1.7.2.5

  1. #/usr/ports/print/freetype2
  2. make install clean
复制代码
复制代码
1.7.2.6

  1. #/usr/ports/net/openldap24-server
  2. make install clean
复制代码
复制代码
1.7.2.7

  1. #/usr/ports/devel/autoconf262
  2. make install clean
复制代码
复制代码
二.编译安装
2.1、pcre安装配置:
#tar zvxf pcre-8.02.tar.gz
#./configure && make && make install
2.2、nginx安装配置:

  1. #tar zvxf nginx-0.8.37.tar.gz
  2. #vi init_nginx.sh
复制代码
复制代码./configure \
  1. "--user=www" \
  2.      "--group=www" \
  3.      "--prefix=/usr/local/nginx/" \
  4.      "--with-http_stub_status_module" \
  5.      "--with-http_ssl_module" \
  6.      "--with-md5=/usr/lib" \
  7.      "--with-sha1=/usr/lib"
  8. make
  9. make install
复制代码
复制代码
2.3、mysql安装配置:

  1. #tar zvxf mysql-5.1.46.tar.gz
  2. #vi init_mysql.sh
复制代码
复制代码
  1. ./configure \
  2.      "--prefix=/usr/local/mysql" \
  3.      "--localstatedir=/data/mysql/data" \
  4.      "--with-comment=Source" \
  5.      "--with-server-suffix=-Linuxtone.Org" \
  6.      "--enable-assembler" \
  7.      "--with-charset=utf8" \
  8.      "--with-collation=utf8_chinese_ci" \
  9.      "--with-collation=utf8_chinese_ci" \
  10.      "--with-extra-charsets=complex" \
  11.      "--enable-thread-safe-client" \
  12.      "--with-big-tables" \
  13.      "--with-readline" \
  14.      "--with-ssl" \
  15.      "--with-embedded-server" \
  16.      "--enable-local-infile" \
  17.      "--with-plugins=innobase"
  18. make
  19. make install
  20. pw useradd mysql -d /data/mysql -s /sbin/nologin
  21. /usr/local/mysql/bin/mysql_install_db --user=mysql
  22. cd /usr/local/mysql
  23. chown -R root:mysql .
  24. mkdir -p /data/mysql/data
  25. chown -R mysql /data/mysql/data
  26. rm -f /etc/my.cnf
  27. cp share/mysql/my-huge.cnf /etc/my.cnf.bak
  28. cd /etc
  29. wget http://www.caifw.com/api/my.cnf
  30. sed -i '50 s/^/#/' /etc/my.cnf
  31. cd /usr/local/mysql
  32. cp share/mysql/mysql.server /etc/rc.d/mysqld
  33. chmod 755 /etc/rc.d/mysqld
  34. /etc/rc.d/mysqld start

  35. ln -s /usr/local/mysql/bin/* /usr/bin/
复制代码
复制代码
2.4、安装php相关的lib及扩展模块
2.4.1 libiconv-1.13

  1. #tar zxvf libiconv-1.13.tar.gz
  2. #cd libiconv-1.13
  3. #./configure --prefix=/usr && make && make install
复制代码
复制代码2.4.2 libmcrypt-2.5.8

  1. #tar zxvf libmcrypt-2.5.8.tar.gz
  2. #cd libmcrypt-2.5.8/
  3. #./configure --prefix=/usr && make && make install
  4. #ldconfig
  5. #cd libltdl/
  6. #./configure --enable-ltdl-install
  7. #make && make install
复制代码
复制代码
2.4.3 mhash-0.9.9.9

  1. #tar zxvf mhash-0.9.9.9.tar.gz
  2. #cd mhash-0.9.9.9
  3. #./configure --prefix=/usr && make && make install
  4. #ldconfig
复制代码
复制代码2.4.4 mcrypt-2.6.8

  1. #tar zxvf mcrypt-2.6.8.tar.gz
  2. #cd mcrypt-2.6.8
  3. #cd src
  4. #vi rfc2440.c
复制代码
复制代码FreeBSD与Linux的不同点,造成需要对 mcrypt 的源码进行一下小小的修改,才能在FreeBSD下编译通过。将其中的

  1. #include <malloc.h>
复制代码
复制代码修改为

  1. #include <stdlib.h>
复制代码
复制代码
编译:

  1. #./configure --prefix=/usr && make&& make install
复制代码
复制代码
2.5 安装php与php-fpm
  1. #tar zvxf php-5.2.13.tar.gz
  2. #gzip -cd php-5.2.13-fpm-0.5.14.diff.gz | patch -d php-5.2.13 -p1
  3. #vi init_php.sh
复制代码
复制代码./configure  \
  1. "--prefix=/usr/local/php-fcgi" \
  2.      "--enable-fastcgi" \
  3.      "--enable-fpm" \
  4.      "--enable-discard-path" \
  5.      "--enable-force-cgi-redirect" \
  6.      "--with-config-file-path=/usr/local/php-fcgi/etc" \
  7.      "--with-mysql=/usr/local/mysql" \
  8.      "--with-mysqli=/usr/local/mysql/bin/mysql_config" \
  9.      "--with-iconv-dir" \
  10.      "--with-freetype-dir" \
  11.      "--with-jpeg-dir" \
  12.      "--with-png-dir" \
  13.      "--with-gd" \
  14.      "--with-zlib" \
  15.      "--with-libxml-dir" \
  16.      "--with-curl" \
  17.      "--with-curlwrappers" \
  18.      "--with-openssl" \
  19.      "--with-mhash" \
  20.      "--with-xmlrpc" \
  21.      "--with-mcrypt" \
  22.      "--with-ldap" \
  23.      "--with-ldap-sasl" \
  24.      "--enable-xml" \
  25.      "--disable-rpath" \
  26.      "--enable-discard-path" \
  27.      "--enable-safe-mode" \
  28.      "--enable-bcmath" \
  29.      "--enable-shmop" \
  30.      "--enable-sysvsem" \
  31.      "--enable-inline-optimization" \
  32.      "--enable-mbregex" \
  33.      "--enable-mbstring" \
  34.      "--enable-gd-native-ttf" \
  35.      "--enable-ftp" \
  36.      "--enable-pcntl" \
  37.      "--enable-sockets" \
  38.      "--enable-zip" \
  39.      "--disable-debug" \
  40.      "--disable-ipv6"
  41. make ZEND_EXTRA_LIBS='-liconv'
  42. make install
  43. cp php.ini-dist /usr/local/php-fcgi/etc/php.ini
复制代码
复制代码2.6 安装PHP扩展模块
2.6.1 memcache-2.2.5

  1. #tar zvxf memcache-2.2.5.tgz
  2. #cd memcache-2.2.5
  3. #/usr/local/php-fcgi/bin/phpize
  4. #./configure --with-php-config=/usr/local/php-fcgi/bin/php-config
  5. #make && make install
复制代码
复制代码
2.6.2 eaccelerator-0.9.6

  1. #tar jvxf eaccelerator-0.9.6.tar.bz2
  2. #cd eaccelerator-0.9.6
  3. #/usr/local/php-fcgi/bin/phpize
  4. #./configure --enable-eaccelerator=shared --with-php-config=/usr/local/php-fcgi/bin/php-config
  5. #make && make install
复制代码
复制代码
2.6.3 PDO_MYSQL-1.0.2

  1. #tar zxvf PDO_MYSQL-1.0.2.tgz
  2. #cd PDO_MYSQL-1.0.2
  3. #/usr/local/php-fcgi/bin/phpize
  4. #./configure --with-php-config=/usr/local/php-fcgi/bin/php-config --with-pdo-mysql=/usr/local/mysql
  5. #make && make install
复制代码
复制代码
2.6.4 ImageMagick

  1. #tar zxvf ImageMagick.tar.gz
  2. #cd ImageMagick-6.5.1-2/   
  3. #./configure --enable-shared --with-modules --without-x --with-gs-font-dir=default --with-perl=yes --with-xml=yes --with-zlib=yes --with-jpeg=yes
  4. #make && make install
复制代码
复制代码
2.6.5 imagick-2.2.2

  1. #tar zxvf imagick-2.2.2.tgz
  2. #cd imagick-2.2.2/
  3. #/usr/local/php-fcgi/bin/phpize
  4. #./configure --with-php-config=/usr/local/php-fcgi/bin/php-config
  5. #make
  6. #make install
复制代码
复制代码
2.7 配置php.ini

# sh init_php.ini
复制代码
  1. #!/bin/bash
  2. fcgi_cf="/usr/local/php-fcgi/etc/php.ini"
  3. ea(){
  4. cat << EOF
  5. [eAccelerator]
  6. zend_extension="/usr/local/php-fcgi/lib/php/extensions/no-debug-non-zts-20060613/eaccelerator.so"
  7. eaccelerator.shm_size="32"
  8. eaccelerator.cache_dir="/data/cache/ea"
  9. eaccelerator.enable="1"
  10. eaccelerator.optimizer="1"
  11. eaccelerator.check_mtime="1"
  12. eaccelerator.debug="0"
  13. eaccelerator.filter=""
  14. eaccelerator.shm_max="0"
  15. eaccelerator.shm_ttl="0"
  16. eaccelerator.shm_prune_period="0"
  17. eaccelerator.shm_only="0"
  18. eaccelerator.compress="1"
  19. eaccelerator.compress_level="9"
  20. EOF
  21. }
  22. #mkdir
  23. mkdir -p /data/cache/ea
  24. ea >> $fcgi_cf
复制代码
复制代码
再修改如下项:

  1. ;open_basedir =

  2. disable_functions =

  3. expose_php = On

  4. expose_php = Off

  5. display_errors = On

  6. extension_dir =

  7. output_buffering = Off
复制代码
复制代码
修改为:

  1. open_basedir = /data/www/wwwroot:/tmp

  2. disable_functions = phpinfo,passthru,exec,system,chroot,scandir,chgrp,chown,shell_exec,proc_open,proc_get_status,ini_alter,ini_alter,ini_restore,dl,pfsockopen,openlog,syslog,readlink,symlink,popepassthru,stream_socket_server

  3. display_errors = Off

  4. extension_dir = "/usr/local/php-fcgi/lib/php/extensions/no-debug-non-zts-20060613/"
  5. extension = "memcache.so"
  6. extension = "pdo_mysql.so"
  7. extension = "imagick.so"
  8. extension = "ffmpeg.so"

  9. output_buffering = On
复制代码
复制代码
2.8 安装Zend
#tar zvxf ZendOptimizer-3.3.0a-freebsd6.0-i386.tar.gz
# cd ZendOptimizer-3.3.0a-freebsd6.0-i386
# ./install
注:php.ini的位置为/usr/local/php-fcgi/etc 服务器不选用apache
三、配置整合nginx与php
参见netseek的LEMP构建高性能WEB服务器(第三版)提供完整的配置脚本下载:http://www.linuxtone.org/lemp/scripts.tar.gz  
基于netseek的附件需要修改nginx.conf的内容如下:
  1. nginx.conf

  2. user  www www;

  3. worker_processes 10;

  4. pid /var/log/nginx.pid;
  5. # [ debug | info | notice | warn | error | crit ]
  6. error_log  /dev/null;
  7. #Specifies the value for maximum file descriptors that can be opened by this process.
  8. worker_rlimit_nofile 51200;

  9. events
  10. {
  11.         use kqueue; #FreeSBD下要启用kqueue,Linux下是epoll   

  12.         #maxclient = worker_processes * worker_connections / cpu_number
  13.         worker_connections 51200;
  14. }

  15. http
  16. {
  17.         include       /usr/local/nginx/conf/mime.types;
  18.         default_type  application/octet-stream;
  19.         charset  gb2312;
  20.         server_names_hash_bucket_size 128;
  21.         client_header_buffer_size 32k;
  22.         large_client_header_buffers 4 32k;

  23.         log_format  main  '$remote_addr - $remote_user [$time_local] $request '
  24.                           '"$status" $body_bytes_sent "$http_referer" '
  25.                           '"$http_user_agent" "$http_x_forwarded_for"';

  26.         #access_log  /data/www/logs/access.log  main;
  27.          access_log  /dev/null;

  28.         sendfile on;
  29.         tcp_nopush     on;
  30.         
  31.         keepalive_timeout 60;
  32.         tcp_nodelay on;

  33.         fastcgi_connect_timeout 300;
  34.         fastcgi_send_timeout 300;
  35.         fastcgi_read_timeout 300;
  36.         
  37.         fastcgi_buffer_size 128k;
  38.         fastcgi_buffers 4 128k;
  39.         fastcgi_busy_buffers_size 128k;
  40.         fastcgi_temp_file_write_size 128k;
  41.         fastcgi_intercept_errors on;
  42.         client_max_body_size       50m;
  43.         client_body_buffer_size    256k;
  44.         
  45.         gzip on;
  46.         gzip_min_length  1k;
  47.         gzip_buffers     4 16k;
  48.         gzip_http_version 1.0;
  49.         gzip_comp_level 2;
  50.         gzip_types       text/plain application/x-javascript text/css application/xml;
  51.         gzip_vary on;
  52.         #
  53.         proxy_temp_path            /dev/proxy_temp;
  54.         fastcgi_temp_path          /dev/fastcgi_temp;
  55.         client_body_temp_path      /dev/client_body_temp;
  56.         #client_body_temp_path  /usr/local/nginx/client_body_temp 1 2;
  57.         #proxy_temp_path        /usr/local/nginx/proxy_temp 1 2;
  58.         #fastcgi_temp_path      /usr/local/nginx/fastcgi_temp 1 2;
  59.         
  60.         #upstream
  61.         # upstream  bbs.linuxtone.org  {
  62.         #  ip_hash;
  63.         #  server   127.0.0.1:8080;
  64.         #  server   61.164.122.252:8080;
  65.         #  server   220.189.245.68;
  66.         #}

  67.         # The following includes are specified for virtual hosts
  68.          #www.linuxtone.org
  69.          include          /usr/local/nginx/conf/vhosts/www.linuxtone.org.conf;
  70.          #for example just for study! have fun!
  71.          #include          /usr/local/nginx/conf/conf_example/*.conf ;
  72. }
复制代码
复制代码


四、加入开机启动:
打开/etc/rc.local,如果不存在的话,就新建一个,输入以下内容:

  1. ulimit -SHn 51200
  2. /etc/rc.d/mysqld start
  3. /usr/local/php-fcgi/sbin/php-fpm start
  4. /usr/local/nginx/sbnin/nginx
复制代码
复制代码

论坛徽章:
0
2 [报告]
发表于 2011-05-30 10:04 |只看该作者
好东西,谢谢分享

论坛徽章:
0
3 [报告]
发表于 2011-05-31 13:13 |只看该作者
这个不错,需要的时候非常值得参考!

论坛徽章:
0
4 [报告]
发表于 2011-07-07 11:55 |只看该作者
支持!
使用PORTS安装,轻松愉快。

论坛徽章:
0
5 [报告]
发表于 2011-07-23 00:36 |只看该作者
http://blog.haohtml.com/archives/1133

自己也整理了一篇,一直在用.

论坛徽章:
0
6 [报告]
发表于 2011-09-01 21:29 |只看该作者
硬件支持是个大问题啊

论坛徽章:
0
7 [报告]
发表于 2011-09-08 21:49 |只看该作者
运维群:168572651
欢迎各位新手加入,一起学习集群一起学习运维

论坛徽章:
0
8 [报告]
发表于 2011-09-21 15:32 |只看该作者
受用了,谢谢
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP