Nginx+php-FPM安装配置手记
2012-08-07 10:25
501 查看
php-fpm不用再依赖其它的fastcgi启动器,比如lighttpd的spawn-fcgi。
php-fpm的使用非常方便,配置都是在php-fpm.ini的文件内
而启动,重启都可以从php/sbin/php-fpm中进行
更方便的是修改javascript:;" onClick="javascript:tagshow(event, 'php');" target="_self">php.ini后可以直接使用php-fpm reload进行加载
无需杀掉进程就可以完成php.ini的修改加载
结果显示使用php-fpm可以使php有不小的性能提升
php-fpm控制的进程.cpu回收的速度比较慢.内存分配的很均匀
而spawn-cgi控制的进程CPU下降的很快.而内存分配的比较不均匀.
有很多进程似乎未分配到,而另外一些却占用很高.
可能是由于进程任务分配的不均匀导致的.而这也导致了总体响应速度的下降
而php-fpm合理的分配.导致总体响应的提到以及任务的平均
使用php-fpm需要在php源码上打补丁,然后重新编译php
一.下载php-fpm
wget http://cn.php.net/get/php-5.2.8.tar.gz/from/www.php.net/mirror wget http://php-fpm.anight.org/downloads/head/php-5.2.8-fpm-0.5.10.diff.gz
与php-5.2.9在同一级目录
gzip -cd php-5.2.8-fpm-0.5.10.diff.gz | patch -d php-5.2.9 -p1
补丁打好以后,编译php的时候增加了下面几个参数:
–enable-fpm 激活fastcgi模式的fpm支持
–with-fpm-conf php-fpm的配置文件(默认是PREFIX/etc/php-fpm.conf)
–with-fpm-log php-fpm的日志文件(默认是PREFIX/logs/php-fpm.log)
–with-fpm-pid php-fpm的pid文件(默认是PREFIX/logs/php-fpm.pid)
./configure --prefix=/EBS/php \
--with-config-file-path=/EBS/php/etc \
--enable-fastcgi \
--enable-fpm \
--OTHERS
注:--enable-fastcgi \ 需要在--enable-fpm \的前面,否则,fpm不能编译上。
二.编译好php后,修改配置文件
vi /EBS/php/etc/php-fpm.conf
需要注意下面几处配置
<value name="listen_address">127.0.0.1:9000</value>
这个表示php的fastcgi进程监听的ip地址以及端口
<value name="user">nobody</value>
<value name="group">nobody</value>
表示php的fastcgi进程以什么用户以及用户组来运行,默认该行是注释掉的,需要打开
<value name="display_errors">0</value>
是否显示php错误信息
<value name="max_children">5</value>
最大的子进程数目
运行php-fpm:
php-fpm用一个程序来控制fastcgi进程,这个文件在$PREFIX/sbin/php-fpm
/usr/local/php/sbin/php-fpm
该程序有如下参数:
start 启动php的fastcgi进程
stop 强制终止php的fastcgi进程
quit 平滑终止php的fastcgi进程
restart 重启php的fastcgi进程
reload 重新加载php的php.ini
logrotate 重新启用log文件
也就是说,在修改了php.ini之后,我们可以使用
/usr/local/php/sbin/php-fpm reload
这样,就保持了在php的fastcgi进程持续运行的状态下,又重新加载了php.ini。
三、配置nginx
新建配置文件
/usr/local/nginx/conf/fcgi.conf
注:nginx自带了一个配置文件,/usr/local/nginx/conf/fastcgi_params,该配置文件缺少粗体字体的部分,会造成访问php文件时报404错误。
四 配置XCache
1、安装xcache模块
wgethttp://xcache.lighttpd.net/pub/Releases/1.2.2/xcache-1.2.2.tar.gz
tar -xvzf xcache-1.2.2.tar.gz
cd xcache-1.2.2
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config --enable-xcache --enable-xcache-optimizer
make
make install
2、计算密码的md5值
echo -n "password"|md5sum
5f4dcc3b5aa765d61d8327deb882cf99
3、配置XCache
;注:zend_extension,用来加载zend的扩展,是绝对路径,
extension是相对路径,相对于extension_dir的相对路径,非zend扩展
如果你更改路径以后,一定要apachectl stop后再start,而不要restart。
vi /usr/local/php/etc/php.ini
添加:
5、重启PHP模块
正常load之后,
在phpinfo显出的信息内
Zend这快应该会加上XCache的内容
[align=center]6、另外两种加速模块:[/align]
[align=center]在我们的测试中,效果都要好于xcache,这3中加速不能同时存在两种,有冲突。[/align]
[align=center]6.1 apc[/align]
[align=center]wget http://pecl.php.net/get/APC-3.0.19.tgz[/align] [align=center]cd APC-3.0.19[/align]
[align=center]/usr/local/php/bin/phpize[/align]
[align=center]./configure --enable-apc --enable-apc-mmap --with-apxs=/EBS/apache/bin/apxs --with-php-config=/EBS/php/bin/php-config[/align]
[align=center]make[/align]
[align=center]make install[/align]
[align=center]6.2 eaccelerator[/align]
[align=center]wget http://bart.eaccelerator.net/source/0.9.5.3/eaccelerator-0.9.5.3.zip[/align] [align=center]cd eaccelerator-0.9.5.3[/align]
[align=center]/usr/local/php/bin/phpize[/align]
[align=center]./configure --enable-eaccelerator=shared --with-php-config=/EBS/php/bin/php-config[/align]
[align=center]make[/align]
[align=center]make install[/align]
[align=center]vi php.ini[/align]
[align=center]zend_extension="/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/eaccelerator.so"[/align]
[align=center]eaccelerator.shm_size="16"[/align]
[align=center]eaccelerator.cache_dir="/tmp/eaccelerator"[/align]
[align=center]eaccelerator.enable="1"[/align]
[align=center]eaccelerator.optimizer="1"[/align]
[align=center]eaccelerator.check_mtime="1"[/align]
[align=center]eaccelerator.debug="0"[/align]
[align=center]eaccelerator.filter=""[/align]
[align=center]eaccelerator.shm_max="0"[/align]
[align=center]eaccelerator.shm_ttl="0"[/align]
[align=center]eaccelerator.shm_prune_period="0"[/align]
[align=center]eaccelerator.shm_only="0"[/align]
[align=center]eaccelerator.compress="1"[/align]
[align=center]eaccelerator.compress_level="9"[/align]
[align=center]五、使用nginx对应多台facgi服务器[/align]
[align=center]思路:前端一台nginx,用于做为负载均衡和处理静态页面。利用nginx的upstream模块来将php请求分发到后段的php-fpm服务器上。[/align]
[align=center]后端多台php-fpm的服务器,只起php-fpm服务来处理php。[/align]
[align=center]这样做减少了php-fpm上的nginx服务,相当于少了一层。[/align]
php-fpm的使用非常方便,配置都是在php-fpm.ini的文件内
而启动,重启都可以从php/sbin/php-fpm中进行
更方便的是修改javascript:;" onClick="javascript:tagshow(event, 'php');" target="_self">php.ini后可以直接使用php-fpm reload进行加载
无需杀掉进程就可以完成php.ini的修改加载
结果显示使用php-fpm可以使php有不小的性能提升
php-fpm控制的进程.cpu回收的速度比较慢.内存分配的很均匀
而spawn-cgi控制的进程CPU下降的很快.而内存分配的比较不均匀.
有很多进程似乎未分配到,而另外一些却占用很高.
可能是由于进程任务分配的不均匀导致的.而这也导致了总体响应速度的下降
而php-fpm合理的分配.导致总体响应的提到以及任务的平均
使用php-fpm需要在php源码上打补丁,然后重新编译php
一.下载php-fpm
wget http://cn.php.net/get/php-5.2.8.tar.gz/from/www.php.net/mirror wget http://php-fpm.anight.org/downloads/head/php-5.2.8-fpm-0.5.10.diff.gz
与php-5.2.9在同一级目录
gzip -cd php-5.2.8-fpm-0.5.10.diff.gz | patch -d php-5.2.9 -p1
补丁打好以后,编译php的时候增加了下面几个参数:
–enable-fpm 激活fastcgi模式的fpm支持
–with-fpm-conf php-fpm的配置文件(默认是PREFIX/etc/php-fpm.conf)
–with-fpm-log php-fpm的日志文件(默认是PREFIX/logs/php-fpm.log)
–with-fpm-pid php-fpm的pid文件(默认是PREFIX/logs/php-fpm.pid)
./configure --prefix=/EBS/php \
--with-config-file-path=/EBS/php/etc \
--enable-fastcgi \
--enable-fpm \
--OTHERS
注:--enable-fastcgi \ 需要在--enable-fpm \的前面,否则,fpm不能编译上。
二.编译好php后,修改配置文件
vi /EBS/php/etc/php-fpm.conf
需要注意下面几处配置
<value name="listen_address">127.0.0.1:9000</value>
这个表示php的fastcgi进程监听的ip地址以及端口
<value name="user">nobody</value>
<value name="group">nobody</value>
表示php的fastcgi进程以什么用户以及用户组来运行,默认该行是注释掉的,需要打开
<value name="display_errors">0</value>
是否显示php错误信息
<value name="max_children">5</value>
最大的子进程数目
运行php-fpm:
php-fpm用一个程序来控制fastcgi进程,这个文件在$PREFIX/sbin/php-fpm
/usr/local/php/sbin/php-fpm
该程序有如下参数:
start 启动php的fastcgi进程
stop 强制终止php的fastcgi进程
quit 平滑终止php的fastcgi进程
restart 重启php的fastcgi进程
reload 重新加载php的php.ini
logrotate 重新启用log文件
也就是说,在修改了php.ini之后,我们可以使用
/usr/local/php/sbin/php-fpm reload
这样,就保持了在php的fastcgi进程持续运行的状态下,又重新加载了php.ini。
三、配置nginx
user www www; worker_processes 10; error_log logs/error.log notice; pid logs/nginx.pid; #Specifies the value for maximum file descriptors that can be opened by this process. worker_rlimit_nofile 51200; events { use epoll; worker_connections 51200; } http { include mime.types; default_type application/octet-stream; charset gb2312; server_names_hash_bucket_size 128; #sendfile on; #tcp_nopush on; keepalive_timeout 60; tcp_nodelay on; gzip on; gzip_min_length 1k; gzip_buffers 4 8k; gzip_http_version 1.1; gzip_types text/plain application/x-javascript text/css text/html application/xml; server { listen 80; server_name 192.168.1.2; index index.html index.htm index.php; root /EBS/www; if (-d $request_filename) { rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent; } location ~ .*\.php?$ { include fcgi.conf fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; } log_format access '$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 access; } } |
/usr/local/nginx/conf/fcgi.conf
注:nginx自带了一个配置文件,/usr/local/nginx/conf/fastcgi_params,该配置文件缺少粗体字体的部分,会造成访问php文件时报404错误。
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; |
1、安装xcache模块
wgethttp://xcache.lighttpd.net/pub/Releases/1.2.2/xcache-1.2.2.tar.gz
tar -xvzf xcache-1.2.2.tar.gz
cd xcache-1.2.2
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config --enable-xcache --enable-xcache-optimizer
make
make install
2、计算密码的md5值
echo -n "password"|md5sum
5f4dcc3b5aa765d61d8327deb882cf99
3、配置XCache
;注:zend_extension,用来加载zend的扩展,是绝对路径,
extension是相对路径,相对于extension_dir的相对路径,非zend扩展
如果你更改路径以后,一定要apachectl stop后再start,而不要restart。
vi /usr/local/php/etc/php.ini
添加:
[xcache-common] zend_extension = /usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/xcache.so [xcache.admin] ; Change xcache.admin.user to your preferred login name xcache.admin.user = "admin" ; Change xcache.admin.pass to the MD5 fingerprint of your password ; Use md5 -s "your_secret_password" to find the fingerprint xcache.admin.pass = "5f4dcc3b5aa765d61d8327deb882cf99" [xcache] ; Change xcache.size to tune the size of the opcode cache xcache.size = 24M xcache.shm_scheme = "mmap" xcache.count = 2 xcache.slots = 8K xcache.ttl = 0 xcache.gc_interval = 0 ; Change xcache.var_size to adjust the size of variable cache xcache.var_size = 8M xcache.var_count = 1 xcache.var_slots = 8K xcache.var_ttl = 0 xcache.var_maxttl = 0 xcache.var_gc_interval = 300 xcache.test = Off xcache.readonly_protection = On xcache.mmap_path = "/tmp/xcache" xcache.coredump_directory = "" xcache.cacher = On xcache.stat = On xcache.optimizer = Off [xcache.coverager] xcache.coverager = On xcache.coveragedump_directory = "" |
正常load之后,
在phpinfo显出的信息内
Zend这快应该会加上XCache的内容
[align=center]6、另外两种加速模块:[/align]
[align=center]在我们的测试中,效果都要好于xcache,这3中加速不能同时存在两种,有冲突。[/align]
[align=center]6.1 apc[/align]
[align=center]wget http://pecl.php.net/get/APC-3.0.19.tgz[/align] [align=center]cd APC-3.0.19[/align]
[align=center]/usr/local/php/bin/phpize[/align]
[align=center]./configure --enable-apc --enable-apc-mmap --with-apxs=/EBS/apache/bin/apxs --with-php-config=/EBS/php/bin/php-config[/align]
[align=center]make[/align]
[align=center]make install[/align]
[align=center]6.2 eaccelerator[/align]
[align=center]wget http://bart.eaccelerator.net/source/0.9.5.3/eaccelerator-0.9.5.3.zip[/align] [align=center]cd eaccelerator-0.9.5.3[/align]
[align=center]/usr/local/php/bin/phpize[/align]
[align=center]./configure --enable-eaccelerator=shared --with-php-config=/EBS/php/bin/php-config[/align]
[align=center]make[/align]
[align=center]make install[/align]
[align=center]vi php.ini[/align]
[align=center]zend_extension="/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/eaccelerator.so"[/align]
[align=center]eaccelerator.shm_size="16"[/align]
[align=center]eaccelerator.cache_dir="/tmp/eaccelerator"[/align]
[align=center]eaccelerator.enable="1"[/align]
[align=center]eaccelerator.optimizer="1"[/align]
[align=center]eaccelerator.check_mtime="1"[/align]
[align=center]eaccelerator.debug="0"[/align]
[align=center]eaccelerator.filter=""[/align]
[align=center]eaccelerator.shm_max="0"[/align]
[align=center]eaccelerator.shm_ttl="0"[/align]
[align=center]eaccelerator.shm_prune_period="0"[/align]
[align=center]eaccelerator.shm_only="0"[/align]
[align=center]eaccelerator.compress="1"[/align]
[align=center]eaccelerator.compress_level="9"[/align]
[align=center]五、使用nginx对应多台facgi服务器[/align]
[align=center]思路:前端一台nginx,用于做为负载均衡和处理静态页面。利用nginx的upstream模块来将php请求分发到后段的php-fpm服务器上。[/align]
[align=center]后端多台php-fpm的服务器,只起php-fpm服务来处理php。[/align]
[align=center]这样做减少了php-fpm上的nginx服务,相当于少了一层。[/align]
相关文章推荐
- CentOS7 编译安装Nginx+php并配置php-fpm模块
- 安装配置php-fpm来搭建Nginx+PHP的生产环境
- nginx+php(php-fpm)安装配置
- centos 下编译安装nginx + php 配置nginx+php-fpm
- 安装配置php-fpm来搭建Nginx+PHP的生产环境
- nginx+php-fpm在Linux下的安装和配置
- 安装配置php-fpm来搭建Nginx+PHP的生产环境
- Ubuntu Server 10.10安装配置Nginx+php-fpm+mysql
- CentOS 6.5 + Nginx 1.8.0 + PHP 5.6(with PHP-FPM) 负载均衡源码安装 之 (三)Nginx负载均衡配置
- CentOS 6.5安装配置LNMP服务器(Nginx+PHP+MySQL)
- apache 安装配置 以及php-fpm结合apache配置
- ubuntu安装nginx+php-fpm+mysql
- nginx+php_php-fpm源码安装
- Linux下安装nginx, php, php-fpm并配置
- CentOS 6.5 yum安装配置lnmp服务器(Nginx+PHP+MySQL)
- php-fpm安装、配置与优化
- nginx+php安装配置
- Nginx,MySQL,PHP-FPM的安装和配置
- centos下nginx+php-fpm安装
- Ubuntu 12.04 + nginx + php5 + php-fpm安装,并进行多站点配置