基于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
- [eAccelerator]
- 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
复制代码复制代码
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
好东西,谢谢分享
这个不错,需要的时候非常值得参考!
支持!
使用PORTS安装,轻松愉快。
http://blog.haohtml.com/archives/1133
自己也整理了一篇,一直在用.
硬件支持是个大问题啊
运维群:168572651
欢迎各位新手加入,一起学习集群一起学习运维
受用了,谢谢