solaris 编译安装nginx+php5.3.8(php-fpm)
历史记录:
2011-12-19 修改,添加 fastcgi_intercept_errors on;当请求一个不存在的php页面时原来返回空,添加后返回404错误。
php从5.3.3版本开始内置FPM(FastCGI 进程管理器),用于替换PHP FastCGI的大部分附加功能,对于高负载网站是非常有用的。
它的功能包括:
- 支持平滑停止/启动的高级进程管理功能;
- 可以工作于不同的uid/gid/chroot环境下,并监听不同的端口和使用不同的php.ini配置文件(可取代safe_mode的设置);
- stdout 和 stderr 日志记录;
- 在发生意外情况的时候能够重新启动并缓存被破坏的opcode;
- 文件上传优化支持;
- “慢日志” – 记录脚本 (不仅记录文件名,还记录PHP backtrace信息,可以使用ptrace或者类似工具读取和分析远程进程的运行数据) 运行所导致的异常缓慢;
- fastcgi_finish_request() – 特殊功能:用于在请求完成和刷新数据后,继续在后台执行耗时的工作 (录入视频转换、统计处理等);
- 动态/静态子进程产生;
FPM带来的好处还远不止这些,在此种模式下php本身对apache和nginx等web服务器的依赖程度也更低,服务器软件主要起到请求转发的功能,即将用户的请求转发到FPM监听的端口,php处理后返回结果供web显示。因此选用更轻量级以及并发处理更高的nginx做web服务器成为首选。
一下为本人在solaris10 x86机器上的安装记录:
1.下载并安装pkg-get工具:
wget http://sunfreeware.com/BOLTpget.pkg
pkgadd -d BOLTpget.pkg
2.在线更新php依赖库:
pkg-get install gd-2.0.35
pkg-get install jpeg-8a
pkg-get install libpng-1.2.10
pkg-get install libiconv-1.9.2
pkg-get install openssl-1.0.0e
pkg-get install curl-7.12.2 (此处curl版本不要装太高的)
pkg-get install openldap-2.4.9
pkg-get install sasl-2.1.25
pkg-get install libnet
pkg-get install zlib-1.2.5
pkg-get install mhash-0.9.9.9
pkg-get install gdbm-1.9.1
pkg-get install libxml2-2.7.8
3.下载安装freetype
wget http://download.savannah.gnu.org/releases/freetype/freetype-2.3.5.tar.gz
gunzip freetype-2.3.5.tar.gz
tar -xvf freetype-2.3.5.tar
cd freetype-2.3.5
./configure –prefix=/usr/local/freetype
make
make install
cd ../
4.下载安装libmcrypt
wget http://down1.chinaunix.net/distfiles/libmcrypt-2.5.7.tar.gz
gunzip libmcrypt-2.5.7.tar.gz
tar -xvf libmcrypt-2.5.7.tar
cd libmcrypt-2.5.7.
./configure -–enable-dynamic-loading –-with-included-algos=”rijndael-128 arcfour stream cbc cfb”
make
make install
cd ..
5.下载安装php5.3.8
wget http://cn2.php.net/distributions/php-5.3.8.tar.gz
gunzip php-5.3.8.tar.gz
tar -xvf php-5.3.8.tar
6.下载nginx-1.0.10 稳定版
http://nginx.org/download/nginx-1.0.10.tar.gz
gunzip nginx-1.0.10.tar.gz
tar -xvf nginx-1.0.10.tar
建立服务器软件安装目录
mkdir /usr/local/webserver
创建网站目录
/export/home/web/root
创建web群组和web用户
groupadd web
useradd -d /export/home/web/root -g web web
以fpm方式编译安装php5.3.8
cd php-5.3.8
‘./configure’ ‘–prefix=/usr/local/webserver/php’ ‘–enable-fpm’ ‘–with-mysql=mysqlnd’ ‘–with-mysqli=mysqlnd’ ‘–with-pdo-mysql=mysqlnd’ ‘–with-gd’ ‘–with-png-dir’ ‘–with-jpeg-dir’ ‘–with-iconv=/usr/local/lib’ ‘–enable-mbstring’ ‘–with-zlib’ ‘–with-libxml-dir’ ‘–enable-gd-native-ttf’ ‘–with-openssl’ ‘–enable-pcntl’ ‘–enable-sockets’ ‘–with-ldap’ ‘–with-ldap-sasl’ ‘–with-xmlrpc’ ‘–enable-zip’ ‘–enable-soap’ ‘–with-mcrypt=/usr/local/lib’ ‘–with-freetype-dir=/usr/local/freetype/’ ‘–with-curl’ ‘–with-curlwrappers’
make
make install
查看php是否安装成功:
/usr/local/webserver/php/bin/php -v
提示:
PHP 5.3.8 (cli) (built: Nov 25 2011 00:34:48)
Copyright (c) 1997-2011 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2011 Zend Technologies
表示成功
复制php.ini配置文件
cp php.ini-production /usr/local/webserver/php/lib/php.ini
设置php-fpm自启动
cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod +x /etc/init.d/php-fpm
复制php-fpm配置文件
cp -r sapi/fpm/php-fpm.conf /usr/local/webserver/php/etc/
编辑php-fpm配置文件:
vi /usr/local/webserver/php/etc/php-fpm.conf
查找并打开以下配置(去掉对应行首注释)
pid = run/php-fpm.pid
error_log = log/php-fpm.log
log_level = notice 改为log_level = error
emergency_restart_interval = 0 改为 emergency_restart_interval = 3d
daemonize = yes
listen = 127.0.0.1:9000 改为 listen = /tmp/php-cgi.sock
listen.allowed_clients = 127.0.0.1
user = nobody 改为 user = web
group = nobody 改为 group = web
pm.max_children = 150
pm.start_servers = 45
pm.min_spare_servers = 10
pm.max_spare_servers = 100
pm.max_requests = 1000
保存并退出(各参数意思请查看文档注释)
启动php-fpm(也就是启动php):
/etc/init.d/php-fpm start
ps查看进程,查看php是否启动成功。
7.安装nginx
首先更新pcre:
pkg-get install pcre-8.13
解压并安装nginx:
cd nginx-1.0.10
./configure –group=web –user=web –prefix=/usr/local/webserver/nginx –with-http_stub_status_module –with-http_ssl_module
make && make install
修改配置文件
vi /usr/local/webserver/nginx/conf/nginx.conf,这是主配置文件,设置虚拟主机,php代理设置等,配置如下(示例):
#user nobody;
worker_processes 5;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
pid logs/nginx.pid;
worker_rlimit_nofile 65535;
events {
use epoll;
worker_connections 65535;
}
http {
include mime.types;
default_type application/octet-stream;
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 logs/access.log main;
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 60;
tcp_nodelay on;
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;
fastcgi_intercept_errors on;
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;
#limit_zone crawler $binary_remote_addr 10m;
server {
listen 80;
server_name www.example.com example.com;
root /opt/www/examplecom;
index index.html index.htm index.php;
location ~ .*\.(php|php5)?$
{
fastcgi_pass unix:/tmp/php-cgi.sock;
#fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fcgi.conf;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 1h;
}
}
}
vi /usr/local/webserver/nginx/conf/fcgi.conf ,这里设置fastcgi相关信息,配置如下(示例):
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
# PHP only, required if PHP was built with –enable-force-cgi-redirect
fastcgi_param REDIRECT_STATUS 200;
各项配置的具体意义请阅读相关文档:
启动nginx
/usr/local/webserver/nginx/sbin/nginx
另外:
nginx重启命令:/usr/local/webserver/nginx/sbin/nginx -s reload
php的重启 /etc/init.d/php-fpm reload
cd /opt/www/examplecom
创建phpinfo页面,查看是否安装成功。到此全部安装结束。
在solaris实际安装nginx的过程中ssl可能会出错,此时指定–with-openssl=DIR 选项,特别要注意的是此处的DIR是指openssl的安装文件源码所在的目录,而不是openssl安装以后的目录。如果还是有错误直接pkg-get install nginx也是一种选择,这种安装方式版本可能会比较低,但这种方式我认为不会比自己编译安装差,毕竟此时nginx主要起代理作用,另外我也不认为自己编译安装会比solaris官方提供的nginx更优化。
另外nginx+php5.3+ 在linux上的编译安装过程也大同小异,以上过程也可作为参考。
本次安装过程中参考了一下文章:http://blog.s135.com/nginx_php_v6/
近期评论