`
linvar
  • 浏览: 254234 次
  • 性别: Icon_minigender_1
  • 来自: 未定
社区版块
存档分类
最新评论

nginx + php-fpm

    博客分类:
  • php
阅读更多

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

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

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

2.配置php-fpm
  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有详细说明

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

4.安装php加速器eaccelerator http://www.eaccelerator.net
  官网上有详细安装说明,这里简要记录一下
  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


5.配置nginx服务器
  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/$fastcgi_script_name;
之前中间少了一个斜杠,不科学啊,浪费我大把时间,php又没有办法调试.

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

6.php框架CodeIgniter(CI)
  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;
  }

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics