nginx + php-fpm

本文介绍如何在Linux环境下配置PHP-FPM及Nginx服务器,包括PHP编译安装、PHP-FPM配置启动、eAccelerator加速器安装配置及Nginx服务器配置等步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[size=medium]
虽然对php不太感冒,特别是调用对象的方法obj->say();这种语法简直就是...
但是一般很多开源项目都使用PHP开发,比如mysql管理工具phpmyadmin,
blog系统wordpress,and so on...
所以还是打算在linux上搭起php的环境,以备不时之需.

php-5.3.5包含了fpm(fastcgi process manager),
这样php就可以以fastcgi的形式启动了, 再跟nginx配合起来, 那简直就是...

[b]1.编译php-5.3.5[/b]
(默认php.ini配置文件在安装目录下的lib中查找)
./configure主要的选项是
--enable-fpm
--with-mysql=mysqlnd
--with-mysqli=mysqlnd
--with-pdo-mysql=mysqlnd
注意这里使用的是mysqlnd, 这个php官方的mysql客户端driver,
所以这里可以不用使用mysql本身的include了.

[b]2.配置php-fpm[/b]
php-fpm默认的配置文件在安装目录下的etc文件夹中, php-fpm二进制在安装目录下的sbin文件夹中.
#vi etc/php-fpm.conf
主要设置需要启动多少fastcgi子进程
pm=dynamic
pm.max_children = 10
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 10
#具体含义php-fpm.conf有详细说明

[b]3.启动php-fpm[/b]
首先检查一下php-fpm.conf配置文件是否配置正确
#sudo sbin/php-fpm -t
ok的话启动之
#sudo sbin/php-fpm
#ps aux | grep php-fpm 可以看到已启动的主进程和多个子进程

[b]4.安装php加速器eaccelerator[/b] [url]http://www.eaccelerator.net[/url]
官网上有详细安装说明,这里简要记录一下
1).bootstrap eAccelerator
#/opt/php/bin/phpize

2).build eAccelerator
#./configure --enable-eaccelerator=shared \
--with-php-config=/opt/php/bin/php-config
#make & make install

3)config eAccelerator in php.ini file
zend_extension="/path/to/eaccelerator.so"
eaccelerator.shm_size="16"
eaccelerator.cache_dir="/tmp/eaccelerator"
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"

4)create eAccelerator cache_dir(see above)
#mkdir /tmp/eaccelerator
#chmod 0777 /tmp/eaccelerator

5)done. check it works
#sudo sbin/php-fpm -v

6)restart php-fpm


[b]5.配置nginx服务器[/b]
php-fpm默认启动地址:127.0.0.1:9000
server {
listen 80;
server_name tgf.me www.tgf.me;
if ($host = 'www.tgf.me') {
rewrite ^/(.*)$ http:tgf.me/$1 permanent;
}

location ~ .*\.(php|php5)$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
}
access_log /path/to/log/access.log;
}

###very important###
include 进来的fastcgi_params的文件中必须包含的参数
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param QUERY_STRING $query_string;
#如果要处理POST,那么还需要这三个参数
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;

#其中SCRIPT_FILENAME是PHP用来确定执行脚本的文件名称.
我试过不小心漏掉这个参数导致返回空白页,又追踪不了错误信息的惨痛教训.
只返回空白页,firebug显示返回状态200, 开启了php的错误提示设置而没打印错误,

===update 2013-8-15===
尽管配置了SCRIPT_FILENAME参数, 页面返回File not found 404错误...
nginx-1.4.2,php-5.5.1
fastcgi_param SCRIPT_FILENAME $document_root[color=darkred]/[/color]$fastcgi_script_name;
之前中间少了一个斜杠,不科学啊,浪费我大把时间,php又没有办法调试.

=====end of line======

[b]6.php框架CodeIgniter(CI)[/b]
CI小巧,且设计理念不错,restful的url设计,试用过一下下
配置nginx以适应CI
location / {
try_files $uri @codeigniter;
}
location ~* \.php$ {
try_files $uri @codeigniter;
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
}
location @codeigniter {
internal;
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root/index.php;
fastcgi_param SCRIPT_NAME $document_root/index.php$request_uri;
}

[/size]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值