使用MacPorts安装必要的软件

查找可用的php-fpm版本

$ port search php*fpm
php53-fpm @5.3.18 (lang, php, www)
    php53 FPM SAPI

php54-fpm @5.4.8 (lang, php, www)
    php54 FPM SAPI

Found 2 ports.

安装你需要的php-fpm版本

$ sudo port install php-fpm53

其他必要的软件安装

$ sudo port install nginx +ssl
$ sudo port install php

警告

Mac OS X 10.8系统自带了一套PHP运行环境,其中就包括php-fpm。MacPorts默认安装的所有程序的${prefix}在/opt/local,默认情况下,当前用户的$PATH设置中并没有包括/opt/local这个目录及其子目录,这有可能导致很多莫名其妙的配置和程序运行问题。

我的解决方案是:

  • 配置环境变量$PATH,优先使用/opt/local目录及其重要子目录下的可执行程序。具体做法是:编辑当前用户的.bashrc,做如下修改
    export PATH=/opt/local/bin:/opt/local/sbin:/opt/local/apache2/bin:$PATH
    
  • 编辑~/.bash_profile,注意:Mac OS X默认的bash在启动时读取的配置文件并不是~/.bashrc,而是~/.bash_profile
    [ -r ~/.bashrc ] && source ~/.bashrc
    

php-fpm配置

php-fpm目前主要有两个分支,分别对应于php-5.2.x的版本和php-5.3.x的版本。在5.2.x的版本中,php-fpm.conf使用的是xml格式,而在新的5.3.x版本中,则是和php.ini一样的配置风格。

基本配置可以直接在php-fpm发行版给出的default配置文件基础上,参考注释进行配置。

配置状态监控

  • 编辑/opt/local/etc/php53/php-fpm.conf,在需要监控的[pool]下找到pm.status_path
    pm.status_path=/status
    
  • 编辑/opt/local/etc/nginx/nginx.conf,添加以下配置:
    # php-fpm status monitor
    location /status {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
        allow 127.0.0.1;
        deny all;
    }
    

启动php-fpm

注意事项

由于Mac系统自带php和php-fpm,为了确保你即将启动的是通过MacPorts安装php-fpm,请检查php-fpm的默认搜索路径。

$ which php-fpm
/opt/local/sbin/php-fpm
$ which php-fpm53
/opt/local/sbin/php-fpm53
$ ll /opt/local/sbin/php-fpm
lrwxr-xr-x  1 root  admin  25 Oct 25 16:35 /opt/local/sbin/php-fpm -> /opt/local/sbin/php-fpm53

启动

php-fpm支持2种运行方式:后台方式和前台运行方式。后台运行方式适用于生产环境,前台运行方式适用于开发和测试环境。在php-fpm.conf中定义php-fpm的运行方式

; Send FPM to background. Set to 'no' to keep FPM in foreground for debugging
; or for use with launchd.
; Default Value: yes
daemonize = no

启动php-fpm非常简单:

$sudo php-fpm

配置nginx+php-fpm

直接贴我机器上的开发用配置文件

user  nobody;
worker_processes  1;

error_log  /opt/local/var/log/nginx/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}

http {
    upstream backend_php {
        server 127.0.0.1:9000;
    }

    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;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  60;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;
        charset utf8;

        access_log  /opt/local/var/log/nginx/localhost.access.log  main;

        location / {
            root   share/nginx/html;
            index  index.html index.htm;
        }

        error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   share/nginx/html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php$ {
            root /opt/local/share/nginx/php/; # php scripts ROOT directory
            fastcgi_index   index.php; 
            include         fastcgi.conf;  # fastcgi.conf must be accessible by nginx.conf
            include         fastcgi_params; # fastcgi_params must be accessible by nginx.conf
            fastcgi_pass    backend_php;     # The upstream determined above
        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        location ~ /\.ht {
            deny  all;
        }


        # php-fpm status monitor
        location /status {
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            include fastcgi.conf;
            include fastcgi_params;
            allow 127.0.0.1;
            deny all;
        }
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   share/nginx/html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443;
    #    server_name  localhost;

    #    ssl                  on;
    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_timeout  5m;

    #    ssl_protocols  SSLv2 SSLv3 TLSv1;
    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers   on;

    #    location / {
    #        root   share/nginx/html;
    #        index  index.html index.htm;
    #    }
    #}

}

体验php-fpm独有的fastcgi_finish_request特性

以下是测试代码,验证fastcgi_finish_request可以立刻flush PHP输出缓存,使得客户端请求可以立即得到响应,结束当前HTTP请求-响应会话。同时,fastcgi_finish_request()之后的代码可以继续在当前php-fpm进程中运行下去。实现服务器端的后台异步处理长时业务逻辑代码,客户端“零”等待。

<?php
echo "fastcgi_finish_request demo";
if (!function_exists("fastcgi_finish_request")) {
    function fastcgi_finish_request()  {
        echo "fastcgi_finish_request not supported at this ENV";
    }
}
fastcgi_finish_request();
sleep(5);
file_put_contents('/tmp/log.txt', date('Y-m-d H:i:s') . " 转换格式\n", FILE_APPEND);

sleep(5);
file_put_contents('/tmp/log.txt', date('Y-m-d H:i:s') . " 提取图片\n", FILE_APPEND);

以上代码,如果用apache的mod_php方式运行则客户端会在请求得到处理之时,等待大约10秒后才会得到服务器端的响应输出。

延伸阅读

返回顶部