版本:ver 1.18 最后修改:2010-7-25 20:40 原创文章,转载请注明出处:http://blog.jiqila.com/
本系列文章已有第二版:FreeBSD上搭建nginx 0.8.x + PHP 5.3.x(FastCGI) + MySQL 5.1.x)(第三版)
前言:在网上Linux环境下搭建nginx+php的文章已经比较多也比较完善了,而在FreeBSD环境下搭建的文章并不多,且都使用的是 ports方式安装。本文的目的就是形成一个比较完整的、可操作强的FreeBSD环境下以编译源码方式搭建nginx+php+mysql的手册。本文将尽量详细地描述每一个操作步骤,使初学者也能迅速搭建一个FEMP环境。
Nginx简介:Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器 。 Nginx 是由 Igor Sysoev 为俄罗斯访问量第二的Rambler.ru 站点开发的,它已经在该站点运行超过六年了。Igor 将源代码以类BSD许可证的形式发布。自Nginx 发布六年来,Nginx 已经因为它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名了。目前国内各大门户网站已经部署了Nginx,如新浪、网易、腾讯等;国内几个重要的视频分享网站也部署了Nginx,如六房间、酷6等。新近发现Nginx 技术在国内日趋火热,越来越多的网站开始部署Nginx。
FreeBSD系统分区的时候,根据自己的需要来做。以下的安装,我都是以我的分区为例。
FreeBSD不同于Linux,我们最小化安装FreeBSD后,系统中并没有wget(可能完全安装也没有,呵呵),因为我已经习惯了使用wget进行下载,所以为了可以在FreeBSD中使用wget,我们需要安装wget,方法如下:
刚刚安装完后,wget并未能使用,需要执行下面的命令后方可生效。
〇、前期准备(适用于CD1安装或DVD最小化安装)和内核优化
对于使用CD1安装FreeBSD的网友,我们需要安装以下支持。
为了避免nginx在大负载量下可能出现500 Internal Server Error,我们需要对内核参数进行一下设置,如下:
在文件末尾增加如下内容:
完成以上的前期准备工作,就可以进行下面的操作了。
一、首先,我们来安装nginx。
1、下载与解压nginx
2、修改nginx原文件。如果不进行修改,nginx编译会以debug方式编译,编译后的文件有3兆之巨。经以下修改后,文件减小到500KB左右。
将文件最后的
修改为
3、编译安装nginx。因为在安装FreeBSD的时候,已经内建了www用户和www用户组,因此不需要再自己建立了。这是与在Linux上安装的一个小小区别。
如果要开机后自动启动nginx,那么需要在/etc/rc.local中加入一行命令:
二、接下来,我们来安装MySQL,因为安装PHP需要用到MySQL支持,所以要先于PHP安装。
如果你希望在这台服务器上运行MySQL数据库服务端,那么执行以下步骤。
1、创建mysql 的用户及用户组(这点与Linux稍有不同)
2、创建MySQL数据存放目录
3、以mysql用户的身份建立数据表
4、创建配置文件
5、打开/etc/rc.local,如果不存在的话,就新建一个,输入以下内容:
6、创建一个可以远程登录的root账户,密码是123456,这样我们就可以使用mysql连接工具进行远程管理了。
三、编译安装PHP(FastCGI)
1、编译安装PHP 5.2.14所需的支持库
FreeBSD与Linux的不同点,造成需要对 mcrypt 的源码进行一下小小的修改,才能在FreeBSD下编译通过。将其中的
修改为
然后继续编译
2、编译安装PHP 5.2.14
3、安装PHP扩展
4、修改php.ini
5、配置eAccelerator,加速PHP
在文件末尾增加下面的内容。
6、开机启动php-fpm
四、配置nginx使其支持PHP
1、创建网站根目录
2、创建PHP测试页
3、修改 /usr/local/nginx/conf/nginx.conf 如下:
4、修改/usr/local/nginx/conf/fastcgi.conf文件,在其最前面加入如下代码:
5、修改/usr/local/php/etc/php-fpm.conf文件如下:
至此,环境已经搭建完毕。重启机器后打开浏览器,在地址栏输入:http://192.168.1.3/,应该可以正确显示PHP信息页。
本文主要参考了:《LEMP构建高性能WEB服务器(第三版)》和《Nginx 0.7.x + PHP 5.2.10(FastCGI)搭建胜过Apache十倍的Web服务器(第5版)》,还有很多FreeBSD相关文章。在此对所有参考文章的作者表示感谢。
本系列文章已有第二版:FreeBSD上搭建nginx 0.8.x + PHP 5.3.x(FastCGI) + MySQL 5.1.x)(第三版)
前言:在网上Linux环境下搭建nginx+php的文章已经比较多也比较完善了,而在FreeBSD环境下搭建的文章并不多,且都使用的是 ports方式安装。本文的目的就是形成一个比较完整的、可操作强的FreeBSD环境下以编译源码方式搭建nginx+php+mysql的手册。本文将尽量详细地描述每一个操作步骤,使初学者也能迅速搭建一个FEMP环境。
Nginx简介:Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器 。 Nginx 是由 Igor Sysoev 为俄罗斯访问量第二的Rambler.ru 站点开发的,它已经在该站点运行超过六年了。Igor 将源代码以类BSD许可证的形式发布。自Nginx 发布六年来,Nginx 已经因为它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名了。目前国内各大门户网站已经部署了Nginx,如新浪、网易、腾讯等;国内几个重要的视频分享网站也部署了Nginx,如六房间、酷6等。新近发现Nginx 技术在国内日趋火热,越来越多的网站开始部署Nginx。
FreeBSD系统分区的时候,根据自己的需要来做。以下的安装,我都是以我的分区为例。
引用
硬盘总大小:250GB
/ 20G
swap 8GB
/var 10GB
/usr 40GB
/data 112GB
/home 40GB
/ 20G
swap 8GB
/var 10GB
/usr 40GB
/data 112GB
/home 40GB
FreeBSD不同于Linux,我们最小化安装FreeBSD后,系统中并没有wget(可能完全安装也没有,呵呵),因为我已经习惯了使用wget进行下载,所以为了可以在FreeBSD中使用wget,我们需要安装wget,方法如下:
引用
cd /usr/ports/ftp/wget
make install clean
#在出现的对话框中去掉IPV6前面的选择
make install clean
#在出现的对话框中去掉IPV6前面的选择
刚刚安装完后,wget并未能使用,需要执行下面的命令后方可生效。
引用
rehash
〇、前期准备(适用于CD1安装或DVD最小化安装)和内核优化
对于使用CD1安装FreeBSD的网友,我们需要安装以下支持。
引用
################ 安装pcre ##################
cd /usr/ports/devel/pcre
make install clean
rehash
################ 安装xml ##################
cd /usr/ports/textproc/libxml
make install clean
cd /usr/ports/textproc/libxml2
make install clean
################ 安装curl ##################
cd /usr/ports/ftp/curl
make install clean
################ 安装jpeg ##################
cd /usr/ports/graphics/jpeg
make install clean
################ 安装png ##################
cd /usr/ports/graphics/png
make install clean
################ 安装freetype ##################
cd /usr/ports/print/freetype
make install clean
cd /usr/ports/print/freetype2
make install clean
################ 安装autoconf ##################
cd /usr/ports/devel/autoconf262
make install clean
cd /usr/ports/devel/pcre
make install clean
rehash
################ 安装xml ##################
cd /usr/ports/textproc/libxml
make install clean
cd /usr/ports/textproc/libxml2
make install clean
################ 安装curl ##################
cd /usr/ports/ftp/curl
make install clean
################ 安装jpeg ##################
cd /usr/ports/graphics/jpeg
make install clean
################ 安装png ##################
cd /usr/ports/graphics/png
make install clean
################ 安装freetype ##################
cd /usr/ports/print/freetype
make install clean
cd /usr/ports/print/freetype2
make install clean
################ 安装autoconf ##################
cd /usr/ports/devel/autoconf262
make install clean
为了避免nginx在大负载量下可能出现500 Internal Server Error,我们需要对内核参数进行一下设置,如下:
引用
vi /etc/sysctl.conf
在文件末尾增加如下内容:
引用
kern.maxfiles=65535 #同时打开文件的最大数量
完成以上的前期准备工作,就可以进行下面的操作了。
一、首先,我们来安装nginx。
1、下载与解压nginx
引用
wget http://sysoev.ru/nginx/nginx-0.7.67.tar.gz
tar zxvf nginx-0.7.67.tar.gz
cd nginx-0.7.67
tar zxvf nginx-0.7.67.tar.gz
cd nginx-0.7.67
2、修改nginx原文件。如果不进行修改,nginx编译会以debug方式编译,编译后的文件有3兆之巨。经以下修改后,文件减小到500KB左右。
引用
cd auto/cc
vi gcc
vi gcc
将文件最后的
引用
# debug
CFLAGS="$CFLAGS -g"
CFLAGS="$CFLAGS -g"
修改为
引用
# debug
#CFLAGS="$CFLAGS -g"
#CFLAGS="$CFLAGS -g"
3、编译安装nginx。因为在安装FreeBSD的时候,已经内建了www用户和www用户组,因此不需要再自己建立了。这是与在Linux上安装的一个小小区别。
引用
cd ../../
./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_sub_module
make
make install
./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_sub_module
make
make install
如果要开机后自动启动nginx,那么需要在/etc/rc.local中加入一行命令:
引用
/usr/local/nginx/sbin/nginx
二、接下来,我们来安装MySQL,因为安装PHP需要用到MySQL支持,所以要先于PHP安装。
引用
wget http://dev.mysql.com/get/Downloads/MySQL-5.1/mysql-5.1.49.tar.gz/from/http://mysql.he.net/
tar zxvf mysql-5.1.49.tar.gz
cd mysql-5.1.49
./configure --prefix=/usr/local/mysql/ --enable-assembler --with-extra-charsets=complex --enable-thread-safe-client --with-big-tables --with-readline --with-ssl --with-embedded-server --enable-local-infile --with-plugins=innobase --with-mysqld-user=mysql --without-ndb-debug --without-debug --with-charset=utf8 --localstatedir=/data/mysql/data --with-collation=utf8_general_ci
make
make install
tar zxvf mysql-5.1.49.tar.gz
cd mysql-5.1.49
./configure --prefix=/usr/local/mysql/ --enable-assembler --with-extra-charsets=complex --enable-thread-safe-client --with-big-tables --with-readline --with-ssl --with-embedded-server --enable-local-infile --with-plugins=innobase --with-mysqld-user=mysql --without-ndb-debug --without-debug --with-charset=utf8 --localstatedir=/data/mysql/data --with-collation=utf8_general_ci
make
make install
如果你希望在这台服务器上运行MySQL数据库服务端,那么执行以下步骤。
1、创建mysql 的用户及用户组(这点与Linux稍有不同)
引用
pw groupadd mysql
pw useradd mysql -g mysql -d /data/mysql -s /usr/sbin/nologin
pw useradd mysql -g mysql -d /data/mysql -s /usr/sbin/nologin
2、创建MySQL数据存放目录
引用
mkdir -p /data/mysql/data/
chown -R mysql:mysql /data/mysql/
chown -R mysql:mysql /data/mysql/
3、以mysql用户的身份建立数据表
引用
/usr/local/mysql/bin/mysql_install_db --basedir=/usr/local/mysql --datadir=/data/mysql/data --user=mysql
4、创建配置文件
引用
cd /usr/local/mysql/share/mysql
cp my-large.cnf /etc/my.cnf
cp mysql.server /usr/local/mysql/mysqld
chmod 755 /usr/local/mysql/mysqld
cp my-large.cnf /etc/my.cnf
cp mysql.server /usr/local/mysql/mysqld
chmod 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.sock
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456';
quit
/usr/local/mysql/bin/mysql -u root -p -S /tmp/mysql.sock
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456';
quit
三、编译安装PHP(FastCGI)
1、编译安装PHP 5.2.14所需的支持库
引用
wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.13.tar.gz
tar zxvf libiconv-1.13.tar.gz
cd libiconv-1.13/
./configure --prefix=/usr
make
make install
cd ..
tar zxvf libiconv-1.13.tar.gz
cd libiconv-1.13/
./configure --prefix=/usr
make
make install
cd ..
引用
wget "http://downloads.sourceforge.net/mcrypt/libmcrypt-2.5.8.tar.gz?modtime=1171868460&big_mirror=0"
tar zxvf libmcrypt-2.5.8.tar.gz
cd libmcrypt-2.5.8
./configure --prefix=/usr
make
make install
cd ..
tar zxvf libmcrypt-2.5.8.tar.gz
cd libmcrypt-2.5.8
./configure --prefix=/usr
make
make install
cd ..
引用
wget "http://downloads.sourceforge.net/mhash/mhash-0.9.9.9.tar.gz?modtime=1175740843&big_mirror=0"
tar zxvf mhash-0.9.9.9.tar.gz
cd mhash-0.9.9.9
./configure --prefix=/usr
make
make install
cd ..
tar zxvf mhash-0.9.9.9.tar.gz
cd mhash-0.9.9.9
./configure --prefix=/usr
make
make install
cd ..
引用
wget "http://downloads.sourceforge.net/mcrypt/mcrypt-2.6.8.tar.gz?modtime=1194463373&big_mirror=0"
tar zxvf mcrypt-2.6.8.tar.gz
cd mcrypt-2.6.8
cd src
vi rfc2440.c
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
cd ..
make
make install
cd ..
2、编译安装PHP 5.2.14
引用
wget http://www.php.net/get/php-5.2.14.tar.gz/from/this/mirror
wget http://php-fpm.org/downloads/php-5.2.14-fpm-0.5.14.diff.gz
tar zxvf php-5.2.14.tar.gz
gzip -cd php-5.2.14-fpm-0.5.14.diff.gz | patch -d php-5.2.14 -p1
cd php-5.2.14
./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-pear
make ZEND_EXTRA_LIBS='-liconv'
make install
cp php.ini-dist /usr/local/php/etc/php.ini
wget http://php-fpm.org/downloads/php-5.2.14-fpm-0.5.14.diff.gz
tar zxvf php-5.2.14.tar.gz
gzip -cd php-5.2.14-fpm-0.5.14.diff.gz | patch -d php-5.2.14 -p1
cd php-5.2.14
./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-pear
make ZEND_EXTRA_LIBS='-liconv'
make install
cp php.ini-dist /usr/local/php/etc/php.ini
3、安装PHP扩展
引用
wget http://pecl.php.net/get/memcache-2.2.5.tgz
tar zxvf memcache-2.2.5.tgz
cd memcache-2.2.5
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make
make install
cd ..
tar zxvf memcache-2.2.5.tgz
cd memcache-2.2.5
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make
make install
cd ..
引用
wget http://bart.eaccelerator.net/source/0.9.5.3/eaccelerator-0.9.5.3.tar.bz2
tar jxvf eaccelerator-0.9.5.3.tar.bz2
cd eaccelerator-0.9.5.3
/usr/local/php/bin/phpize
./configure --enable-eaccelerator=shared --with-php-config=/usr/local/php/bin/php-config
make
make install
cd ..
tar jxvf eaccelerator-0.9.5.3.tar.bz2
cd eaccelerator-0.9.5.3
/usr/local/php/bin/phpize
./configure --enable-eaccelerator=shared --with-php-config=/usr/local/php/bin/php-config
make
make install
cd ..
引用
wget http://pecl.php.net/get/PDO_MYSQL-1.0.2.tgz
tar zxvf PDO_MYSQL-1.0.2.tgz
cd 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/mysql
make
make install
cd ..
tar zxvf PDO_MYSQL-1.0.2.tgz
cd 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/mysql
make
make install
cd ..
引用
wget ftp://mirror.aarnet.edu.au/pub/imagemagick/ImageMagick-6.5.5-6.tar.gz
tar zxvf ImageMagick-6.5.5-6.tar.gz
cd ImageMagick-6.5.5-6
./configure
make
make install
cd ..
tar zxvf ImageMagick-6.5.5-6.tar.gz
cd ImageMagick-6.5.5-6
./configure
make
make install
cd ..
引用
有网友提示,按照以上方法安装ImageMagick后,有可能会遇到PHP加载imagick.so后运行错误,解决方法是在编译ImageMagick时关掉openmp: –disable-openmp。在此感谢网友Sonic热心提示,谢谢。如果还不行的话,请更换ImageMagick至低版本,比如:6.5.4-2。
引用
wget http://pecl.php.net/get/imagick-2.2.2.tgz
tar zxvf imagick-2.2.2.tgz
cd imagick-2.2.2
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make
make install
cd ..
tar zxvf imagick-2.2.2.tgz
cd imagick-2.2.2
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make
make install
cd ..
4、修改php.ini
引用
vi /usr/local/php/etc/php.ini
查找output_buffering = Off
修改为output_buffering = On
再查找extension_dir = "./"
修改为extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/"
并在此行后增加以下几行,然后保存:
extension = "memcache.so"
extension = "pdo_mysql.so"
extension = "imagick.so"
extension = "eaccelerator.so"
查找output_buffering = Off
修改为output_buffering = On
再查找extension_dir = "./"
修改为extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/"
并在此行后增加以下几行,然后保存:
extension = "memcache.so"
extension = "pdo_mysql.so"
extension = "imagick.so"
extension = "eaccelerator.so"
5、配置eAccelerator,加速PHP
引用
mkdir -p /data/php/eaccelerator_cache
vi /usr/local/php/etc/php.ini
vi /usr/local/php/etc/php.ini
在文件末尾增加下面的内容。
引用
[eaccelerator]
eaccelerator.shm_size="64"
eaccelerator.cache_dir="/data/php/eaccelerator_cache"
eaccelerator.enable="1"
eaccelerator.optimizer="1"
eaccelerator.check_mtime="1"
eaccelerator.debug="0"
eaccelerator.filter=""
eaccelerator.shm_max="0"
eaccelerator.shm_ttl="3600"
eaccelerator.shm_prune_period="3600"
eaccelerator.shm_only="0"
eaccelerator.compress="1"
eaccelerator.compress_level="9"
eaccelerator.shm_size="64"
eaccelerator.cache_dir="/data/php/eaccelerator_cache"
eaccelerator.enable="1"
eaccelerator.optimizer="1"
eaccelerator.check_mtime="1"
eaccelerator.debug="0"
eaccelerator.filter=""
eaccelerator.shm_max="0"
eaccelerator.shm_ttl="3600"
eaccelerator.shm_prune_period="3600"
eaccelerator.shm_only="0"
eaccelerator.compress="1"
eaccelerator.compress_level="9"
6、开机启动php-fpm
引用
vi /etc/rc.local
增加下面语句
/usr/local/php/sbin/php-fpm start
增加下面语句
/usr/local/php/sbin/php-fpm start
四、配置nginx使其支持PHP
1、创建网站根目录
引用
mkdir -p /data/www
chmod +w /data/www
chown -R www:www /data/www
chmod +w /data/www
chown -R www:www /data/www
2、创建PHP测试页
引用
vi /data/www/index.php
在文件中输入以下内容
<?php
phpinfo();
?>
在文件中输入以下内容
<?php
phpinfo();
?>
3、修改 /usr/local/nginx/conf/nginx.conf 如下:
引用
user www www;
worker_processes 8;
events {
use kqueue;
worker_connections 51200;
}
http {
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/css application/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 ~ .*\.(php|php5)?$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
worker_processes 8;
events {
use kqueue;
worker_connections 51200;
}
http {
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/css application/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 ~ .*\.(php|php5)?$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
4、修改/usr/local/nginx/conf/fastcgi.conf文件,在其最前面加入如下代码:
引用
if ($request_filename ~* (.*)\.php) {
set $php_url $1;
}
if (!-e $php_url.php) {
return 404;
}
set $php_url $1;
}
if (!-e $php_url.php) {
return 404;
}
5、修改/usr/local/php/etc/php-fpm.conf文件如下:
引用
<?xml version="1.0" ?>
<configuration>
All relative paths in this config are relative to php's install prefix
<section name="global_options">
Pid file
<value name="pid_file">/usr/local/php/logs/php-fpm.pid</value>
Error log file
<value name="error_log">/usr/local/php/logs/php-fpm.log</value>
Log level
<value name="log_level">notice</value>
When this amount of php processes exited with SIGSEGV or SIGBUS ...
<value name="emergency_restart_threshold">10</value>
... in a less than this interval of time, a graceful restart will be initiated.
Useful to work around accidental curruptions in accelerator's shared memory.
<value name="emergency_restart_interval">1m</value>
Time limit on waiting child's reaction on signals from master
<value name="process_control_timeout">5s</value>
Set to 'no' to debug fpm
<value name="daemonize">yes</value>
</section>
<workers>
<section name="pool">
Name of pool. Used in logs and stats.
<value name="name">default</value>
Address to accept fastcgi requests on.
Valid syntax is 'ip.ad.re.ss:port' or just 'port' or '/path/to/unix/socket'
<value name="listen_address">127.0.0.1:9000</value>
<value name="listen_options">
Set listen(2) backlog
<value name="backlog">-1</value>
Set permissions for unix socket, if one used.
In Linux read/write permissions must be set in order to allow connections from web server.
Many BSD-derrived systems allow connections regardless of permissions.
<value name="owner"></value>
<value name="group"></value>
<value name="mode">0666</value>
</value>
Additional php.ini defines, specific to this pool of workers.
<value name="php_defines">
<!-- <value name="sendmail_path">/usr/sbin/sendmail -t -i</value> -->
<value name="display_errors">0</value>
</value>
Unix user of processes
<value name="user">www</value>
Unix group of processes
<value name="group">www</value>
Process manager settings
<value name="pm">
Sets style of controling worker process count.
Valid values are 'static' and 'apache-like'
<value name="style">static</value>
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.
<value name="max_children">8</value>
Settings group for 'apache-like' pm style
<value name="apache_like">
Sets the number of server processes created on startup.
Used only when 'apache-like' pm_style is selected
<value name="StartServers">20</value>
Sets the desired minimum number of idle server processes.
Used only when 'apache-like' pm_style is selected
<value name="MinSpareServers">5</value>
Sets the desired maximum number of idle server processes.
Used only when 'apache-like' pm_style is selected
<value name="MaxSpareServers">35</value>
</value>
</value>
The timeout (in seconds) for serving a single request after which the worker process will be terminated
Should be used when 'max_execution_time' ini option does not stop script execution for some reason
'0s' means 'off'
<value name="request_terminate_timeout">0s</value>
The timeout (in seconds) for serving of single request after which a php backtrace will be dumped to slow.log file
'0s' means 'off'
<value name="request_slowlog_timeout">0s</value>
The log file for slow requests
<value name="slowlog">logs/slow.log</value>
Set open file desc rlimit
<value name="rlimit_files">51200</value>
Set max core size rlimit
<value name="rlimit_core">0</value>
Chroot to this directory at the start, absolute path
<value name="chroot"></value>
Chdir to this directory at the start, absolute path
<value name="chdir"></value>
Redirect workers' stdout and stderr into main error log.
If not set, they will be redirected to /dev/null, according to FastCGI specs
<value name="catch_workers_output">yes</value>
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
<value name="max_requests">102400</value>
Comma separated list of ipv4 addresses of FastCGI clients that allowed to connect.
Equivalent to FCGI_WEB_SERVER_ADDRS environment in original php.fcgi (5.2.2+)
Makes sense only with AF_INET listening socket.
<value name="allowed_clients">127.0.0.1</value>
Pass environment variables like LD_LIBRARY_PATH
All $VARIABLEs are taken from current environment
<value name="environment">
<value name="HOSTNAME">$HOSTNAME</value>
<value name="PATH">/usr/local/bin:/usr/bin:/bin</value>
<value name="TMP">/tmp</value>
<value name="TMPDIR">/tmp</value>
<value name="TEMP">/tmp</value>
<value name="OSTYPE">$OSTYPE</value>
<value name="MACHTYPE">$MACHTYPE</value>
<value name="MALLOC_CHECK_">2</value>
</value>
</section>
</workers>
</configuration>
<configuration>
All relative paths in this config are relative to php's install prefix
<section name="global_options">
Pid file
<value name="pid_file">/usr/local/php/logs/php-fpm.pid</value>
Error log file
<value name="error_log">/usr/local/php/logs/php-fpm.log</value>
Log level
<value name="log_level">notice</value>
When this amount of php processes exited with SIGSEGV or SIGBUS ...
<value name="emergency_restart_threshold">10</value>
... in a less than this interval of time, a graceful restart will be initiated.
Useful to work around accidental curruptions in accelerator's shared memory.
<value name="emergency_restart_interval">1m</value>
Time limit on waiting child's reaction on signals from master
<value name="process_control_timeout">5s</value>
Set to 'no' to debug fpm
<value name="daemonize">yes</value>
</section>
<workers>
<section name="pool">
Name of pool. Used in logs and stats.
<value name="name">default</value>
Address to accept fastcgi requests on.
Valid syntax is 'ip.ad.re.ss:port' or just 'port' or '/path/to/unix/socket'
<value name="listen_address">127.0.0.1:9000</value>
<value name="listen_options">
Set listen(2) backlog
<value name="backlog">-1</value>
Set permissions for unix socket, if one used.
In Linux read/write permissions must be set in order to allow connections from web server.
Many BSD-derrived systems allow connections regardless of permissions.
<value name="owner"></value>
<value name="group"></value>
<value name="mode">0666</value>
</value>
Additional php.ini defines, specific to this pool of workers.
<value name="php_defines">
<!-- <value name="sendmail_path">/usr/sbin/sendmail -t -i</value> -->
<value name="display_errors">0</value>
</value>
Unix user of processes
<value name="user">www</value>
Unix group of processes
<value name="group">www</value>
Process manager settings
<value name="pm">
Sets style of controling worker process count.
Valid values are 'static' and 'apache-like'
<value name="style">static</value>
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.
<value name="max_children">8</value>
Settings group for 'apache-like' pm style
<value name="apache_like">
Sets the number of server processes created on startup.
Used only when 'apache-like' pm_style is selected
<value name="StartServers">20</value>
Sets the desired minimum number of idle server processes.
Used only when 'apache-like' pm_style is selected
<value name="MinSpareServers">5</value>
Sets the desired maximum number of idle server processes.
Used only when 'apache-like' pm_style is selected
<value name="MaxSpareServers">35</value>
</value>
</value>
The timeout (in seconds) for serving a single request after which the worker process will be terminated
Should be used when 'max_execution_time' ini option does not stop script execution for some reason
'0s' means 'off'
<value name="request_terminate_timeout">0s</value>
The timeout (in seconds) for serving of single request after which a php backtrace will be dumped to slow.log file
'0s' means 'off'
<value name="request_slowlog_timeout">0s</value>
The log file for slow requests
<value name="slowlog">logs/slow.log</value>
Set open file desc rlimit
<value name="rlimit_files">51200</value>
Set max core size rlimit
<value name="rlimit_core">0</value>
Chroot to this directory at the start, absolute path
<value name="chroot"></value>
Chdir to this directory at the start, absolute path
<value name="chdir"></value>
Redirect workers' stdout and stderr into main error log.
If not set, they will be redirected to /dev/null, according to FastCGI specs
<value name="catch_workers_output">yes</value>
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
<value name="max_requests">102400</value>
Comma separated list of ipv4 addresses of FastCGI clients that allowed to connect.
Equivalent to FCGI_WEB_SERVER_ADDRS environment in original php.fcgi (5.2.2+)
Makes sense only with AF_INET listening socket.
<value name="allowed_clients">127.0.0.1</value>
Pass environment variables like LD_LIBRARY_PATH
All $VARIABLEs are taken from current environment
<value name="environment">
<value name="HOSTNAME">$HOSTNAME</value>
<value name="PATH">/usr/local/bin:/usr/bin:/bin</value>
<value name="TMP">/tmp</value>
<value name="TMPDIR">/tmp</value>
<value name="TEMP">/tmp</value>
<value name="OSTYPE">$OSTYPE</value>
<value name="MACHTYPE">$MACHTYPE</value>
<value name="MALLOC_CHECK_">2</value>
</value>
</section>
</workers>
</configuration>
至此,环境已经搭建完毕。重启机器后打开浏览器,在地址栏输入:http://192.168.1.3/,应该可以正确显示PHP信息页。
引用
修改历史:
ver 1.0:2009-6-23 0:34,本文创建。
ver 1.1:2009-7-1 23:02,PHP支持库安装中为configure增加参数--prefix=/usr。
ver 1.2:2009-7-2 13:21,1、增加nginx源代码修改部分;2、修改MySQL安装,设置utf8为缺省编码;3、增加分区参考;4、修改用户创建的命令,之前的命令在FreeBSD下无效。
ver 1.3:2009-7-2 23:36,1、增加wget的安装;2、增加了对mcrypt源码的修改,以使其可以在FreeBSD下编译通过。
ver 1.4:2009-7-3 21:26,1、增加PHP扩展的安装;2、增加eAccelerator配置;3、增加nginx的PHP支持配置。
ver 1.5:2009-7-10 15:57,1、升级php-5.2.10-fpm版本到0.5.13;2、升级MySQL到5.1.36
ver 1.6:2009-7-16 18:51,1、增加php-fpm.conf 的配置。
ver 1.7:2009-11-6 10:23,1、增加有关ImageMagick的安装补充。
ver 1.8:2009-11-27 17:51,1、升级nginx版本到0.7.64;2、升级mysql到5.1.41;3、升级php到5.2.11。
ver 1.9:2009-12-11 16:00,1、修改了eaccelerator.so的加载方式。
ver 1.10:2010-1-1 0:05,1、升级MySQL到5.1.42。
ver 1.11:2010-1-29 10:31,1、升级PHP到PHP 5.2.12;2、升级php-fpm到php-5.2.12-fpm-0.5.13。
ver 1.12:2010-5-25 18:03,1、升级nginx版本到0.7.65;2、升级mysql到5.1.47;3、升级php到5.2.13;4、升级php-fpm到php-5.2.13-fpm-0.5.13。
ver 1.13:2010-6-3 10:52,1、修改nginx.conf,将include fastcgi_params改为include fastcgi.conf;2、修改fastcgi.conf,增加防止非法php解析的相关配置。
ver 1.14:2010-6-10 12:18,1、升级nginx版本到0.7.66,0.7.66修补的bug较多,建议升级;2、nginx编译时增加--with-http_sub_module,Substitution模块,点击查看详情。
ver 1.15:2010-6-21 14:45,1、升级nginx版本到0.7.67。
ver 1.16:2010-7-12 13:47,1、升级mysql版本到5.1.48。
ver 1.17:2010-7-24 19:56,1、升级mysql版本到5.1.49;2、升级php版本到5.2.14;3、升级php-fpm版本到0.5.14。
var 1.18:2010-7-25 20:40,1、增加CD1安装后,前期准备工作;2、增加修改FreeBSD内核参数设置;3、修改硬盘分区示例。
ver 1.0:2009-6-23 0:34,本文创建。
ver 1.1:2009-7-1 23:02,PHP支持库安装中为configure增加参数--prefix=/usr。
ver 1.2:2009-7-2 13:21,1、增加nginx源代码修改部分;2、修改MySQL安装,设置utf8为缺省编码;3、增加分区参考;4、修改用户创建的命令,之前的命令在FreeBSD下无效。
ver 1.3:2009-7-2 23:36,1、增加wget的安装;2、增加了对mcrypt源码的修改,以使其可以在FreeBSD下编译通过。
ver 1.4:2009-7-3 21:26,1、增加PHP扩展的安装;2、增加eAccelerator配置;3、增加nginx的PHP支持配置。
ver 1.5:2009-7-10 15:57,1、升级php-5.2.10-fpm版本到0.5.13;2、升级MySQL到5.1.36
ver 1.6:2009-7-16 18:51,1、增加php-fpm.conf 的配置。
ver 1.7:2009-11-6 10:23,1、增加有关ImageMagick的安装补充。
ver 1.8:2009-11-27 17:51,1、升级nginx版本到0.7.64;2、升级mysql到5.1.41;3、升级php到5.2.11。
ver 1.9:2009-12-11 16:00,1、修改了eaccelerator.so的加载方式。
ver 1.10:2010-1-1 0:05,1、升级MySQL到5.1.42。
ver 1.11:2010-1-29 10:31,1、升级PHP到PHP 5.2.12;2、升级php-fpm到php-5.2.12-fpm-0.5.13。
ver 1.12:2010-5-25 18:03,1、升级nginx版本到0.7.65;2、升级mysql到5.1.47;3、升级php到5.2.13;4、升级php-fpm到php-5.2.13-fpm-0.5.13。
ver 1.13:2010-6-3 10:52,1、修改nginx.conf,将include fastcgi_params改为include fastcgi.conf;2、修改fastcgi.conf,增加防止非法php解析的相关配置。
ver 1.14:2010-6-10 12:18,1、升级nginx版本到0.7.66,0.7.66修补的bug较多,建议升级;2、nginx编译时增加--with-http_sub_module,Substitution模块,点击查看详情。
ver 1.15:2010-6-21 14:45,1、升级nginx版本到0.7.67。
ver 1.16:2010-7-12 13:47,1、升级mysql版本到5.1.48。
ver 1.17:2010-7-24 19:56,1、升级mysql版本到5.1.49;2、升级php版本到5.2.14;3、升级php-fpm版本到0.5.14。
var 1.18:2010-7-25 20:40,1、增加CD1安装后,前期准备工作;2、增加修改FreeBSD内核参数设置;3、修改硬盘分区示例。
本文主要参考了:《LEMP构建高性能WEB服务器(第三版)》和《Nginx 0.7.x + PHP 5.2.10(FastCGI)搭建胜过Apache十倍的Web服务器(第5版)》,还有很多FreeBSD相关文章。在此对所有参考文章的作者表示感谢。
Nginx 0.7.x 已经进入正式版本了
Nginx负载均衡配置示例


2009/06/23 00:34 | by

参数就会提示问题?是为什么啊 提示“ starting mysql.error! manager of pid-file quit withou”
[root@test /usr/local/php/sbin]# uname -a
FreeBSD test 7.2-RELEASE FreeBSD 7.2-RELEASE #0: Fri May 1 08:49:13 UTC 2009 root@walker.cse.buffalo.edu:/usr/obj/usr/src/sys/GENERIC i386
[root@test /usr/local/php/sbin]# /usr/local/php/bin/php -v
PHP 5.2.10 (cli) (built: Nov 5 2009 09:44:08)
Copyright (c) 1997-2009 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2009 Zend Technologies
那个问题解决了,但是php-fpm启动还是有问题,我再重新编译一次php试试吧
有时编译ImageMagick并加载imagick.so后运行php会有错误,解决方法:
在编译ImageMagick时关掉openmp: –disable-openmp
configure: error: xml2-config not found. Please check your libxml2 installation.
网上查了下很多遇到这个问题的,原因是这两个包,
libxml2-2.6.23-1.2.i386.rpm
libxml2-devel-2.6.23-1.2.i386.rpm
没有安装。
这个执行一直提示:
make: no target to make.
我知道为什么?
ftp://mirror.aarnet.edu.au/pub/imagemagick/ImageMagick-6.5.5-6.tar.gz