歡迎您光臨本站 註冊首頁

基於FreeBSD 8.0搭建nginx+php+php-fpm+mysql高性能php平台

←手機掃碼閱讀     火星人 @ 2014-03-03 , reply:0

基於FreeBSD 8.0搭建nginx+php+php-fpm+mysql高性能php平台

基於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登陸
# vi /etc/rc.d/sshd複製代碼
修改下面選項為如下:PermitRootLogin yes
PasswordAuthentication yes
UseDNS no複製代碼
1.2、平台環境:
FreeBSD- 8.0-RELEASE_i386
nginx-0.8.38
php-5.2.13
php-5.2.13-fpm-0.5.14
mysql-5.1.46複製代碼
1.3、系統分區:/   40G
swap   4GB
/var   5GB
/tmp   5MB
/data  剩餘所有分區
註:具體分區請根據相關業務劃分複製代碼
1.4、系統約定:
軟體源代碼包存放位置             /usr/local/src
源碼包編譯安裝位置(prefix)     /usr/local/software_name
腳本以及維護程序存放位置       /usr/local/sbin
MySQL 資料庫位置                /data/mysql/data(可按情況設置)
網站根目錄                          /data/www/wwwroot(可按情況設置)
虛擬主機日誌根目錄               /data/logs(可按情況設置)
Nginx運行賬戶                     www:www
install_software_name.sh     //存放編譯參數腳本習慣將所有編譯腳本存放在install_software_name.sh便於升級和更新軟體複製代碼
1.5、定時校正伺服器時鐘,定時與中國國家授時中心授時伺服器同步#crontab ?e

15 3 * * * /usr/sbin/ntpdate 210.72.145.44 > /dev/null 2>&1複製代碼
1.6、下載軟體包:
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.02.tar.gz
wget http://nginx.org/download/nginx-0.8.37.tar.gz
wget http://mirrors.sohu.com/mysql/MySQL-5.1/mysql-5.1.46.tar.gz
wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.13.tar.gz
wget http://sourceforge.net/projects/mcrypt/files/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.gz/download
wget http://sourceforge.net/projects/mhash/files/mhash/0.9.9.9/mhash-0.9.9.9.tar.gz/download
wget http://sourceforge.net/projects/mcrypt/files/MCrypt/2.6.8/mcrypt-2.6.8.tar.gz/download
wget http://php-fpm.org/downloads/php-5.2.13-fpm-0.5.14.diff.gz
wget http://cn.php.net/get/php-5.2.13.tar.gz/from/this/mirror
wget http://pecl.php.net/get/memcache-2.2.5.tgz
wget http://bart.eaccelerator.net/source/0.9.6/eaccelerator-0.9.6.tar.bz2
wget http://pecl.php.net/get/PDO_MYSQL-1.0.2.tgz
wget http://blog.s135.com/soft/linux/nginx_php/imagick/ImageMagick.tar.gz
wget http://pecl.php.net/get/imagick-2.2.2.tgz
wget http://docs.linuxtone.org/soft/bsder/ZendOptimizer-3.3.0a-freebsd6.0-i386.tar.gz複製代碼
1.7、利用ports安裝基本包
1.7.1安裝wget
# cd /usr/ports/ftp/wget
#make install clean複製代碼#在出現的對話框中去掉IPV6前面的選擇
#接下來對話框全部默認皆可
剛剛安裝完后,wget並未能使用,需要執行下面的命令後方可生效。#rehash複製代碼
1.7.2安裝其他基本的軟體包(出現對話框默認皆可)
1.7.2.1
#cd /usr/ports/textproc/libxml2
make install clean複製代碼1.7.2.2
#/usr/ports/ftp/curl
make install clean複製代碼1.7.2.3
#/usr/ports/graphics/jpeg
make install clean複製代碼1.7.2.4
#/usr/ports/graphics/png
make install clean複製代碼1.7.2.5
#/usr/ports/print/freetype2
make install clean複製代碼
1.7.2.6
#/usr/ports/net/openldap24-server
make install clean複製代碼
1.7.2.7
#/usr/ports/devel/autoconf262
make install clean複製代碼
二.編譯安裝
2.1、pcre安裝配置:
#tar zvxf pcre-8.02.tar.gz
#./configure && make && make install
2.2、nginx安裝配置:
#tar zvxf nginx-0.8.37.tar.gz
#vi init_nginx.sh複製代碼./configure \"--user=www" \
     "--group=www" \
     "--prefix=/usr/local/nginx/" \
     "--with-http_stub_status_module" \
     "--with-http_ssl_module" \
     "--with-md5=/usr/lib" \
     "--with-sha1=/usr/lib"
make
make install複製代碼
2.3、mysql安裝配置:
#tar zvxf mysql-5.1.46.tar.gz
#vi init_mysql.sh複製代碼./configure \
     "--prefix=/usr/local/mysql" \
     "--localstatedir=/data/mysql/data" \
     "--with-comment=Source" \
     "--with-server-suffix=-Linuxtone.Org" \
     "--enable-assembler" \
     "--with-charset=utf8" \
     "--with-collation=utf8_chinese_ci" \
     "--with-collation=utf8_chinese_ci" \
     "--with-extra-charsets=complex" \
     "--enable-thread-safe-client" \
     "--with-big-tables" \
     "--with-readline" \
     "--with-ssl" \
     "--with-embedded-server" \
     "--enable-local-infile" \
     "--with-plugins=innobase"
make
make install
pw useradd mysql -d /data/mysql -s /sbin/nologin
/usr/local/mysql/bin/mysql_install_db --user=mysql
cd /usr/local/mysql
chown -R root:mysql .
mkdir -p /data/mysql/data
chown -R mysql /data/mysql/data
rm -f /etc/my.cnf
cp share/mysql/my-huge.cnf /etc/my.cnf.bak
cd /etc
wget http://www.caifw.com/api/my.cnf
sed -i '50 s/^/#/' /etc/my.cnf
cd /usr/local/mysql
cp share/mysql/mysql.server /etc/rc.d/mysqld
chmod 755 /etc/rc.d/mysqld
/etc/rc.d/mysqld start

ln -s /usr/local/mysql/bin/* /usr/bin/複製代碼
2.4、安裝php相關的lib及擴展模塊
2.4.1 libiconv-1.13
#tar zxvf libiconv-1.13.tar.gz
#cd libiconv-1.13
#./configure --prefix=/usr && make && make install複製代碼2.4.2 libmcrypt-2.5.8
#tar zxvf libmcrypt-2.5.8.tar.gz
#cd libmcrypt-2.5.8/
#./configure --prefix=/usr && make && make install
#ldconfig
#cd libltdl/
#./configure --enable-ltdl-install
#make && make install複製代碼
2.4.3 mhash-0.9.9.9
#tar zxvf mhash-0.9.9.9.tar.gz
#cd mhash-0.9.9.9
#./configure --prefix=/usr && make && make install
#ldconfig複製代碼2.4.4 mcrypt-2.6.8
#tar zxvf mcrypt-2.6.8.tar.gz
#cd mcrypt-2.6.8
#cd src
#vi rfc2440.c複製代碼FreeBSD與Linux的不同點,造成需要對 mcrypt 的源碼進行一下小小的修改,才能在FreeBSD下編譯通過。將其中的
#include <malloc.h>複製代碼修改為
#include <stdlib.h>複製代碼
編譯:
#./configure --prefix=/usr && make&& make install複製代碼
2.5 安裝php與php-fpm#tar zvxf php-5.2.13.tar.gz
#gzip -cd php-5.2.13-fpm-0.5.14.diff.gz | patch -d php-5.2.13 -p1
#vi init_php.sh複製代碼./configure  \ "--prefix=/usr/local/php-fcgi" \
     "--enable-fastcgi" \
     "--enable-fpm" \
     "--enable-discard-path" \
     "--enable-force-cgi-redirect" \
     "--with-config-file-path=/usr/local/php-fcgi/etc" \
     "--with-mysql=/usr/local/mysql" \
     "--with-mysqli=/usr/local/mysql/bin/mysql_config" \
     "--with-iconv-dir" \
     "--with-freetype-dir" \
     "--with-jpeg-dir" \
     "--with-png-dir" \
     "--with-gd" \
     "--with-zlib" \
     "--with-libxml-dir" \
     "--with-curl" \
     "--with-curlwrappers" \
     "--with-openssl" \
     "--with-mhash" \
     "--with-xmlrpc" \
     "--with-mcrypt" \
     "--with-ldap" \
     "--with-ldap-sasl" \
     "--enable-xml" \
     "--disable-rpath" \
     "--enable-discard-path" \
     "--enable-safe-mode" \
     "--enable-bcmath" \
     "--enable-shmop" \
     "--enable-sysvsem" \
     "--enable-inline-optimization" \
     "--enable-mbregex" \
     "--enable-mbstring" \
     "--enable-gd-native-ttf" \
     "--enable-ftp" \
     "--enable-pcntl" \
     "--enable-sockets" \
     "--enable-zip" \
     "--disable-debug" \
     "--disable-ipv6"
make ZEND_EXTRA_LIBS='-liconv'
make install
cp php.ini-dist /usr/local/php-fcgi/etc/php.ini複製代碼2.6 安裝PHP擴展模塊
2.6.1 memcache-2.2.5
#tar zvxf memcache-2.2.5.tgz
#cd memcache-2.2.5
#/usr/local/php-fcgi/bin/phpize
#./configure --with-php-config=/usr/local/php-fcgi/bin/php-config
#make && make install複製代碼
2.6.2 eaccelerator-0.9.6
#tar jvxf eaccelerator-0.9.6.tar.bz2
#cd eaccelerator-0.9.6
#/usr/local/php-fcgi/bin/phpize
#./configure --enable-eaccelerator=shared --with-php-config=/usr/local/php-fcgi/bin/php-config
#make && make install複製代碼
2.6.3 PDO_MYSQL-1.0.2
#tar zxvf PDO_MYSQL-1.0.2.tgz
#cd PDO_MYSQL-1.0.2
#/usr/local/php-fcgi/bin/phpize
#./configure --with-php-config=/usr/local/php-fcgi/bin/php-config --with-pdo-mysql=/usr/local/mysql
#make && make install複製代碼
2.6.4 ImageMagick
#tar zxvf ImageMagick.tar.gz
#cd ImageMagick-6.5.1-2/   
#./configure --enable-shared --with-modules --without-x --with-gs-font-dir=default --with-perl=yes --with-xml=yes --with-zlib=yes --with-jpeg=yes
#make && make install複製代碼
2.6.5 imagick-2.2.2
#tar zxvf imagick-2.2.2.tgz
#cd imagick-2.2.2/
#/usr/local/php-fcgi/bin/phpize
#./configure --with-php-config=/usr/local/php-fcgi/bin/php-config
#make
#make install複製代碼
2.7 配置php.ini

# sh init_php.ini
複製代碼#!/bin/bash
fcgi_cf="/usr/local/php-fcgi/etc/php.ini"
ea(){
cat << EOF

zend_extension="/usr/local/php-fcgi/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"
EOF
}
#mkdir
mkdir -p /data/cache/ea
ea >> $fcgi_cf複製代碼
再修改如下項:
;open_basedir =

disable_functions =

expose_php = On

expose_php = Off

display_errors = On

extension_dir =

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,pfsockopen,openlog,syslog,readlink,symlink,popepassthru,stream_socket_server

display_errors = Off

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

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的內容如下: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;
        #client_body_temp_path  /usr/local/nginx/client_body_temp 1 2;
        #proxy_temp_path        /usr/local/nginx/proxy_temp 1 2;
        #fastcgi_temp_path      /usr/local/nginx/fastcgi_temp 1 2;
        
        #upstream
        # upstream  bbs.linuxtone.org  {
        #  ip_hash;
        #  server   127.0.0.1:8080;
        #  server   61.164.122.252:8080;
        #  server   220.189.245.68;
        #}

        # The following includes are specified for virtual hosts
         #www.linuxtone.org
         include          /usr/local/nginx/conf/vhosts/www.linuxtone.org.conf;
         #for example just for study! have fun!
         #include          /usr/local/nginx/conf/conf_example/*.conf ;
}複製代碼


四、加入開機啟動:
打開/etc/rc.local,如果不存在的話,就新建一個,輸入以下內容:
ulimit -SHn 51200
/etc/rc.d/mysqld start
/usr/local/php-fcgi/sbin/php-fpm start
/usr/local/nginx/sbnin/nginx複製代碼
《解決方案》

好東西,謝謝分享
《解決方案》

這個不錯,需要的時候非常值得參考!
《解決方案》

支持!
使用PORTS安裝,輕鬆愉快。
《解決方案》

http://blog.haohtml.com/archives/1133

自己也整理了一篇,一直在用.
《解決方案》

硬體支持是個大問題啊
《解決方案》

運維群:168572651
歡迎各位新手加入,一起學習集群一起學習運維
《解決方案》

受用了,謝謝

[火星人 ] 基於FreeBSD 8.0搭建nginx+php+php-fpm+mysql高性能php平台已經有1064次圍觀

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