微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ngx_php7 嵌入 php7 脚本的 nginx 模块

程序名称:ngx_php7

授权协议: BSD

操作系统: 跨平台

开发语言: C/C++

ngx_php7 介绍

ngx_PHP7 是一个嵌入 PHP7 脚本的 Nginx 模块。

环境

PHP-7.0. ~ PHP-7.2.

Nginx-1.4.7 ~ Nginx-1.10.3

安装

$ wget 'http://PHP.net/distributions/PHP-7.2.14.tar.gz'
$ tar xf PHP-7.2.14.tar.gz
$ cd PHP-7.2.14

$ ./configure --prefix=/path/to/PHP --enable-embed
$ make && make install

$ git clone https://github.com/rryqszq4/ngx_PHP7.git

$ wget 'http://Nginx.org/download/Nginx-1.12.2.tar.gz'
$ tar -zxvf Nginx-1.12.2.tar.gz
$ cd Nginx-1.12.2

$ export PHP_CONfig=/path/to/PHP/bin/PHP-config
$ export PHP_BIN=/path/to/PHP/bin
$ export PHP_INC=/path/to/PHP/include/PHP
$ export PHP_LIB=/path/to/PHP/lib

$ ./configure --user=www --group=www \
$             --prefix=/path/to/Nginx \
$             --with-ld-opt="-Wl,-rpath,$PHP_LIB" \
$             --add-module=/path/to/ngx_PHP7/third_party/ngx_devel_kit \
$             --add-module=/path/to/ngx_PHP7
$ make && make install

摘要

user www www;
worker_processes  4;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    keepalive_timeout  65;

    client_max_body_size 64k;   
    client_body_buffer_size 64k;

    PHP_ini_path /usr/local/PHP/etc/PHP.ini;

    server {
        listen       80;
        server_name  localhost;
        default_type 'application/json; charset=UTF-8';

        location /PHP {
            content_by_PHP '
                echo "hello ngx_PHP7";
            ';
        }

        location = /ngx_request {
            content_by_PHP '
                echo ngx_request::document_uri();
            ';
        }

        # curl /ngx_get?a=1&b=2
        location = /ngx_get {
            content_by_PHP '
                echo "ngx::query_args()\n";
                var_dump(ngx::query_args());
            ';
        }

        # curl -d 'a=1&b=2' /ngx_post
        location = /ngx_post {
            content_by_PHP '
                echo "ngx::post_args()\n";
                var_dump(ngx::post_args());
            ';
        }

        location = /ngx_sleep {
            content_by_PHP '
                echo "ngx_sleep start\n";
                yield ngx::sleep(1);
                echo "ngx_sleep end\n";
            ';
        }

        location = /ngx_socket2 {
            default_type 'application/json;charset=UTF-8';
            content_by_PHP '
                $fd = ngx_socket_create();
                var_dump($fd);
                yield ngx_socket_connect($fd, "hq.sinajs.cn", 80);
                $send_buf = "GET /list=s_sh000001 HTTP/1.0\r\n
                                            Host: hq.sinajs.cn\r\nConnection: close\r\n\r\n";
                yield ngx_socket_send($fd, $send_buf, strlen($send_buf));
                $recv_buf = "";
                yield ngx_socket_recv($fd, $recv_buf);
                var_dump($recv_buf);
                yield ngx_socket_close($fd);
            ';
        }

        location = /ngx_var {
            set $a 1234567890;
            content_by_PHP '
                $a = ngx_var::get("a");
                var_dump($a);
            ';
        }

        # set content-type of response headers
        location = /ngx_header {
            content_by_PHP '
                ngx_header_set("Content-Type", "text/html; charset=UTF-8");
            ';
        }

        # run a PHP file
        location = /PHP {
            content_by_PHP '
                include "name_of_PHP_file.PHP";
            ';
        }

        # run any PHP file in root
        location = / {
            content_by_PHP '
                include ngx_var::get("uri");
            ';
        }

    }
}

ngx_php7 官网

https://github.com/rryqszq4/ngx_php7

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐