【php精品源码栏目提醒】:文章导读:在新的一年中,各位网友都进入紧张的学习或是工作阶段。
网学会员整理了php精品源码-【精品】FreeBSD环境下以编译源码方式搭建nginx php mysql的手册 - 文化的相关内容供大家参考,祝大家在新的一年里工作和学习顺利!
更多分享资料请浏览 http://hi.baidu.com/gdfz(孤独疯子的涂鸦之地) 前言:在网上 Linux 环境下搭建 nginxphp 的文章已经比较多也比较完善了,而在FreeBSD 环境下搭建的文章并不多,且都使用的是 ports 方式安装。
本文的目的就是形成一个比较完整的、可操作强的 FreeBSD 环境下以编译
源码方式搭建 nginxphpmysql 的手册。
本文将尽量详细地描述每一个操作步骤,使初学者也能迅速搭建一个 FEMP 环境。
Nginx 简介:Nginx quotengine xquot 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器 。
Nginx 是由 Igor Sysoev 为俄罗斯访问量第二的Rambler.ru 站点开发的,它已经在该站点运行超过四年多了。
Igor 将
源代码以类 BSD 许可证的形式发布。
自 Nginx 发布四年来,Nginx 已经因为它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名了。
目前国内各大门户网站已经部署了 Nginx,如新浪、网易、腾讯等;国内几个重要的视频分享网站也部署了 Nginx,如六房间、酷 6 等。
新近发现 Nginx 技术在国内日趋火热,越来越多的网站开始部署 Nginx。
FreeBSD
系统分区的时候,根据自己的需要来做。
以下的安装,我都是以我的分区为例。
引用硬盘总大小:160GB/ 512MBswap 4GB/var 3GB/tmp 512MB/usr 50GB/data 40GB/home 55GB FreeBSD 不同于 Linux,我们最小化安装 FreeBSD 后,系统中并没有 wget(可能完全安装也没有,呵呵) ,因为我已经习惯了使用 wget 进行下载,所以为了可以在 FreeBSD 中使用wget,我们需要安装 wget,方法如下:引用cd /usr/ports/ftp/wgetmake install clean在出现的对话框中去掉 IPV6 前面的选择 刚刚安装完后,wget 并未能使用,需要执行下面的命令后方可生效。
引用rehash 一、首先,我们来安装 nginx。
安装 Nginx 前,需要先安装 pcre,因为 nginx 需要 pcre 支持。
FreeBSD 7.2-Release已经包含了 pcre 7.9,所以选择安装 Package 的时候,选择上 pcre 即可。
或者使用 ports方式安装 pcre。
ports 方式安装如下:引用cd /usr/ports/devel/pcremake install clean 1、下载与解压 nginx引用wget http://sysoev.ru/nginx/nginx-0.7.64.tar.gztar zxvf nginx-0.7.64.tar.gzcd nginx-0.7.64 2、修改 nginx 原文件。
如果不进行修改,nginx 编译会以 debug 方式编译,编译后的文件有 3 兆之巨。
经以下修改后,文件减小到 500KB 左右。
引用cd auto/ccvi gcc 将文件最后的引用 debugCFLAGSquotCFLAGS -gquot 修改为引用 debugCFLAGSquotCFLAGS -gquot 3、编译安装 nginx。
因为在安装 FreeBSD 的时候,已经内建了 www 用户和 www 用户组,因此不需要在自己建立了。
这是与在 Linux 上安装的一个小小区别。
引用cd ../.././configure --userwww --groupwww --prefix/usr/local/nginx--with-http_stub_status_module --with-http_ssl_modulemakemake install 如果要开机后自动启动 nginx,那么需要在/etc/rc.local 中加入一行命令:引用/usr/local/nginx/sbin/nginx 二、接下来,我们来安装 MySQL,因为安装
PHP 需要用到 MySQL 支持,所以要先于
PHP安装。
引用wgethttp://dev.mysql.com/get/Downloads/MySQL-5.1/mysql-5.1.42.tar.gz/from/http://mysql.he.net/tar zxvf mysql-5.1.42.tar.gzcd mysql-5.1.42./configure --prefix/usr/local/mysql/ --enable-assembler--with-extra-charsetscomplex --enable-thread-safe-client --with-big-tables--with-readline --with-ssl --with-embedded-server --enable-local-infile--with-pluginsinnobase --with-mysqld-usermysql --without-ndb-debug--without-debug --with-charsetutf8 --localstatedir/data/mysql/datamakemake install以 MySQL 5.1.42 为例tar xvf mysql-5.1.42.tar.gzcd mysql-5.1.42/./configure –prefix/usr/local/mysql51 指定安装目录– with-embedded-server – enable-assembler – with-mysqld-ldflags-all-static –with-client-ldflags-all-static 静态编译–without-debug –without-docs –without-man –without-readline 舍去不必要的东东–with-charsetutf8 –with-collationutf8_general_ci 指定编码建议使用 utf8默认 latin1–with-extra-charsetscomplex 添加支持的编码 根据自己的需求指定–enable-thread-safe-client 线程安全–enable-local-infile –with-ssl –with-libwrap 附加
网络安全支持–with-pluginsinnobaseheap 支持 Innodb 及内存表 可 以 不 只 定 系 统 平 台 编 译 程 序 会 自 检 如 果 指 定 Like: –with-system-typei686-redhat-linux-gnu –with-machine-typei686-redhat-
linux-gnu 如果你希望在这台服务器上运行 MySQL 数据库服务端,那么执行以下步骤。
1、创建 mysql 的用户及用户组(这点与 Linux 稍有不同)引用pw groupadd mysqlpw useradd mysql -g mysql -d /data/mysql -s /usr/sbin/nologin 2、创建 MySQL 数据存放目录引用mkdir -p /data/mysql/data/chown -R mysql:mysql /data/mysql/ 3、以 mysql 用户的身份建立数据表引用/usr/local/mysql/bin/
mysql_install_db --basedir/usr/local/my
sql--datadir/data/mysql/data --usermysql 4、创建配置文件引用cd /usr/local/mysql/share/mysqlcp my-large.cnf /etc/my.cnfcp mysql.server /usr/local/mysql/mysqldchmod 755 /usr/local/mysql/mysqld 5、打开/etc/rc.local,如果不存在的话,就新建一个,输入以下内容:引用/usr/local/mysql/mysqld start 6、创建一个可以远程登录的 root 账户,密码是 123456,这样我们就可以使用 mysql 连接工具进行远程管理了。
引用/usr/local/mysql/mysqld start/usr/local/mysql/bin/mysql -u root -p -S /tmp/mysql.sockGRANT ALL PRIVILEGES ON . TO root IDENTIFIED BY 123456quit 三、编译安装 PHPFastCGI 1、编译安装
PHP 5.2.12 所需的支持库引用wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.13.tar.gztar zxvf libiconv-1.13.tar.gzcd libiconv-1.13/./configure --prefix/usrmakemake installcd ..引用wget quothttp://downloads.sourceforge.net/mcrypt/libmcrypt-2.5.8.tar.gzmodtime1171868460ampbig_mirror0quottar zxvf libmcrypt-2.5.8.tar.gzcd libmcrypt-2.5.8./configure --prefix/usrmakemake installcd ..引用wget quothttp://downloads.sourceforge.net/mhash/mhash-0.9.9.9.tar.gzmodtime1175740843ampbig_mirror0quottar zxvf mhash-0.9.9.9.tar.gzcd mhash-0.9.9.9./configure --prefix/usrmakemake installcd ..引用wget quothttp://downloads.sourceforge.net/mcrypt/mcrypt-2.6.8.tar.gzmodtime1194463373ampbig_mirror0quottar zxvf mcrypt-2.6.8.tar.gzcd mcrypt-2.6.8cd srcvi rfc2440.c FreeBSD 与 Linux 的不同点,造成需要对 mcrypt 的
源码进行一下小小的修改,才能在FreeBSD 下编译通过。
将其中的引用include ltmalloc.hgt 修改为引用include ltstdlib.hgt 然后继续编译引用./configure --prefix/usrmakemake installcd .. 2、编译安装
PHP 5.2.12引用wget http://www.
php.net/get/
php-5.2.12.tar.gz/from/this/mirrorwget http://
php-fpm.org/downloads/
php-5.2.12-fpm-0.5.13.diff.gztar zxvf
php-5.2.12.tar.gzgzip -cd
php-5.2.12-fpm-0.5.13.diff.gz patch -d
php-5.2.12 -p1cd
php-5.2.12./buildconf --force./configure --prefix/usr/local/
php --with-config-file-path/usr/local/
php/etc--with-mysql/usr/local/mysql --with-mysqli/usr/local/mysql/bin/mysql_config--with-iconv-dir/usr --with-freetype-dir --with-jpeg-dir --with-png-dir--with-zlib --with-libxml-dir/usr/local --enable-xml --disable-rpath--enable-discard-path --enable-safe-mode --enable-bcmath --enable-shmop--enable-sysvsem --enable-inline-optimization --with-curl --with-curlwrappers--enable-mbregex --enable-fastcgi --enable-fpm --enable-force-cgi-redirect--enable-mbstring --with-mcrypt --with-gd --enable-gd-native-ttf --with-openssl--with-mhash --enable-pcntl --enable-sockets --with-
xmlrpc --enable-zip--disable-ipv6 --without-pearmake ZEND_EXTRA_LIBS-liconvmake installcp
php.ini-dist /usr/local/
php/etc/
php.ini 3、安装
PHP 扩展 安装扩展前,有些扩展需要一些包支持,所以先要安装以下包。
引用cd /usr/ports/devel/autoconf262make install clean 接下来就可以顺利安装
PHP 扩展了。
引用wget http://pecl.
php.net/get/memcache-2.2.5.tgztar zxvf memcache-2.2.5.tgzcd memcache-2.2.5/usr/local/
php/bin/phpize./configure --with-
php-config/usr/local/
php/bin/
php-configmakemake installcd ..引用wget http://bart.eaccelerator.net/source/0.9.5.3/eaccelerator-0.9.5.3.tar.bz2tar jxvf eaccelerator-0.9.5.3.tar.bz2cd eaccelerator-0.9.5.3/usr/local/
php/bin/phpize./configure --enable-eacceleratorshared--with-
php-config/usr/local/
php/bin/
php-configmakemake installcd ..引用wget http://pecl.
php.net/get/PDO_MYSQL-1.0.2.tgztar zxvf PDO_MYSQL-1.0.2.tgzcd PDO_MYSQL-1.0.2/usr/local/
php/bin/phpize./configure --with-
php-config/usr/local/
php/bin/
php-config--with-pdo-mysql/usr/local/mysqlmakemake installcd ..引用wget ftp://mirror.aarnet.edu.au/pub/imagemagick/ImageMagick-6.5.5-6.tar.gztar zxvf ImageMagick-6.5.5-6.tar.gzcd ImageMagick-6.5.5-6./configuremakemake installcd ..引用有网友提示,按照以上方法安装 ImageMagick 后,有可能会遇到
PHP 加载 imagick.so 后运行错误,解决方法是在编译 ImageMagick 时关掉 openmp: –disable-openmp。
在此感谢网友Sonic 热心提示,谢谢。
如果还不行的话,请更换 ImageMagick 至低版本,比如:6.5.4-2。
引用wget http://pecl.
php.net/get/imagick-2.2.2.tgztar zxvf imagick-2.2.2.tgzcd imagick-2.2.2/usr/local/
php/bin/phpize./configure --with-
php-config/usr/local/
php/bin/
php-configmakemake installcd .. 4、修改
php.ini引用vi /usr/local/
php/etc/
php.ini查找 output_buffering Off修改为 output_buffering On再查找 extension_dir quot./quot修 改 为 extension_dir quot/usr/local/
php/lib/
php/extensions/no-debug-non-zts-20060613/quot并在此行后增加以下几行,然后保存:extension quotmemcache.soquotextension quotpdo_mysql.soquotextension quotimagick.soquotextension quoteaccelerator.soquot 5、配置 eAccelerator,加速
PHP引用mkdir -p /data/
php/eaccelerator_cachevi /usr/local/
php/etc/
php.ini 在文件末尾增加下面的内容。
引用eacceleratoreaccelerator.shm_sizequot64quoteaccelerator.cache_dirquot/data/
php/eaccelerator_cachequoteaccelerator.enablequot1quoteaccelerator.optimizerquot1quoteaccelerator.check_mtimequot1quoteaccelerator.debugquot0quoteaccelerator.filterquotquoteaccelerator.shm_maxquot0quoteaccelerator.shm_ttlquot3600quoteaccelerator.shm_prune_periodquot3600quoteaccelerator.shm_onlyquot0quoteaccelerator.compressquot1quoteaccelerator.compress_levelquot9quot 6、开机启动
php-fpm引用vi /etc/rc.local增加下面语句/usr/local/
php/sbin/
php-fpm start 四、配置 nginx 使其支持
PHP 1、创建网站根目录引用mkdir -p /data/wwwchmod w /data/wwwchown -R www:www /data/www 2、创建
PHP 测试页引用vi /data/www/index.
php在文件中输入以下内容lt phpphpinfo gt 3、修改 /usr/local/nginx/conf/nginx.conf 如下:引用user www wwwworker_processes 8events use kqueue worker_connections 51200http include mime.types default_type application/octet-stream server_names_hash_bucket_size 128 client_header_buffer_size 32k large_client_header_buffers 4 32k client_max_body_size 8m sendfile on tcp_nopush on keepalive_timeout 65 fastcgi_connect_timeout 300 fastcgi_send_timeout 300 fastcgi_read_timeout 300 fastcgi_buffer_size 64k fastcgi_buffers 4 64k fastcgi_busy_buffers_size 128k fastcgi_temp_file_write_size 128k 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/cssapplication/xml gzip_vary on tcp_nodelay on server listen 80 server_name 192.168.1.3 root /data/www index index.html index.
php location ..phpphp5 fastcgi_pass 127.0.0.1:9000 fastcgi_index index.
php fastcgi_param SCRIPT_FILENAME document_rootfastcgi_script_name include fastcgi_params error_page 500 502 503 504 /50x.html location /50x.html root html 4、修改/usr/local/
php/etc/
php-fpm.conf 文件如下:引用lt xml versionquot1.0quot gtltconfigurationgt All relative paths in this config are relative to phps install prefixltsection namequotglobal_optionsquotgt Pid file ltvalue namequotpid_filequotgt/usr/local/
php/logs/
php-fpm.pidlt/valuegt Error log file ltvalue namequoterror_logquotgt/usr/local/
php/logs/
php-fpm.loglt/valuegt Log level ltvalue namequotlog_levelquotgtnoticelt/valuegt When this amount of
php processes exited with SIGSEGV or SIGBUS ... ltvalue namequotemergency_restart_thresholdquotgt10lt/valuegt ... in a less than this interval of time a graceful restart will be initiated. Useful to work around accidental curruptions in accelerators shared memory. ltvalue namequotemergency_restart_intervalquotgt1mlt/valuegt Time limit on waiting childs reaction on signals from master ltvalue namequotprocess_control_timeoutquotgt5slt/valuegt Set to no to debug fpm ltvalue namequotdaemonizequotgtyeslt/valuegtlt/sectiongtltworkersgt ltsection namequotpoolquotgt Name of pool. Used in logs and stats. ltvalue namequotnamequotgtdefaultlt/valuegt Address to accept fastcgi requests on. Valid syntax is ip.ad.re.ss:port or just port or /path/to/unix/socket ltvalue namequotlisten_addressquotgt127.0.0.1:9000lt/valuegt ltvalue namequotlisten_optionsquotgt Set listen2 backlog ltvalue namequotbacklogquotgt-1lt/valuegt Set permissions for unix socket if one used. In Linux read/write per
missions must be set in order to allow connectionsfrom
web server. Many BSD-derrived systems allow connections regardless of permissions. ltvalue namequotownerquotgtlt/valuegt ltvalue namequotgroupquotgtlt/valuegt ltvalue namequotmodequotgt0666lt/valuegt lt/valuegt Additional
php.ini defines specific to this pool of workers. ltvalue namequotphp_definesquotgt lt-- ltvalue namequotsendmail_pathquotgt/usr/sbin/sendmail -t -ilt/valuegt --gt ltvalue namequotdisplay_errorsquotgt0lt/valuegt lt/valuegt Unix user of processes ltvalue namequotuserquotgtwwwlt/valuegt Unix group of processes ltvalue namequotgroupquotgtwwwlt/valuegt Process manager settings ltvalue namequotpmquotgt Sets style of controling worker process count. Valid values are static and apache-like ltvalue namequotstylequotgtstaticlt/valuegt Sets the limit on the number of simultaneous requests that will be served. Equivalent to Apache MaxClients directive. Equivalent to
PHP_FCGI_CHILDREN environment in original
php.fcgi Used with any pm_style. ltvalue namequotmax_childrenquotgt8lt/valuegt Settings group for apache-like pm style ltvalue namequotapache_likequotgt Sets the number of server processes created on startup. Used only when apache-like pm_style is selected ltvalue namequotStartServersquotgt20lt/valuegt Sets the desired minimum number of idle server processes. Used only when apache-like pm_style is selected ltvalue namequotMinSpareServersquotgt5lt/valuegt Sets the desired maximum number of idle server processes. Used only when apache-like pm_style is selected ltvalue namequotMaxSpareServersquotgt35lt/valuegt lt/valuegt lt/valuegt The timeout in seconds for serving a single request after which the workerprocess will be terminated Should be used when max_execution_time ini option does not stop scriptexecution for some reason 0s means off ltvalue namequotrequest_terminate_timeoutquotgt0slt/valuegt The timeout in seconds for serving of single request after which a phpbacktrace will be dumped to slow.log file 0s means off ltvalue namequotrequest_slowlog_timeoutquotgt0slt/valuegt The log file for slow requests ltvalue namequotslowlogquotgtlogs/slow.loglt/valuegt Set open file desc rlimit ltvalue namequotrlimit_filesquotgt51200lt/valuegt Set max core size rlimit ltvalue namequotrlimit_corequotgt0lt/valuegt Chroot to this directory at the start absolute path ltvalue namequotchrootquotgtlt/valuegt Chdir to this directory at the start absolute path ltvalue namequotchdirquotgtlt/valuegt Redirect workers stdout and stderr into main error log. If not set they will be redirected to /dev/null according to FastCGI specs ltvalue namequotcatch_workers_outputquotgtyeslt/valuegt How much requests each process should execute before respawn. Useful to work around memory leaks in 3rd party libraries. For endless request processing please specify 0 Equivalent to
PHP_FCGI_MAX_REQUESTS ltvalue namequotmax_requestsquotgt102400lt/valuegt Comma separated list of ipv4 addresses of FastCGI clients that allowed toconnect. Equivalent to FCGI_WEB_SERVER_ADDRS environment in original
php.fcgi 5..