ホームページ  >  記事  >  バックエンド開発  >  nginxサーバー上にCGIプログラムを書き込む

nginxサーバー上にCGIプログラムを書き込む

WBOY
WBOYオリジナル
2016-08-08 09:21:381288ブラウズ

私はUbuntuシステムを使用しています

1. nginxサーバーをインストールします

http://nginx.org/download/nginx-1.9.2.tar.gz
tar -zvxf nginx-1.9.2.tar.gz
cd nginx
./configure
make && make install

デフォルトのインストールパス: /usr/local/nginx

nginxの簡単な設定: /usr/local/nginx/conf/nginx.conf

#user  nobody;
worker_processes  4;

error_log  logs/error.log;
error_log  logs/error.log  notice;
error_log  logs/error.log  info;

pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    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  90;

    #gzip  on;

    server {
        listen       192.168.88.134:8080; //修改成自己的ip地址跟端口
        server_name  192.168.88.134;

        #charset koi8-r;

        access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm index.php;
			#autoindex on;
        }

        #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   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           html;
           fastcgi_pass   127.0.0.1:9999;
           fastcgi_index  index.php;
           fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
           include        fastcgi_params;
        }
        location ~ \.cgi$ {
           root           html;
           fastcgi_pass   127.0.0.1:7000;
           fastcgi_index  index.php;
           fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
           include        fastcgi_params;
        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }
}
nginxを起動します: / usr/ローカル/nginx/sbin/nginx

nginx を終了します: kill `ps aux|grep nginx|sed -n '1p'| awk '{print $2}'`

2. spawn-fcgi

apt-get install apawn-fcgi
3 をインストールします コンパイルされた spawn-fcgi Go をコピーします。 nginx ディレクトリへ
cp./src/spawn-fcgi/usr/local /nginx/ sbin だから .

0 / usr/lib インストールディレクトリは誰にとっても同じではない可能性があります。インストールに従って検索してコピーし、変更してくださいディレクトリ 4. CGI プログラムを作成します

wget http://www.fastcgi.com/dist/fcgi.tar.gz
tar -xvzf <span style="font-family: Arial, Helvetica, sans-serif; font-size: 12px;">fcgi.tar.gz</span>
コンパイル:
此时不能编译安装
cd fcgi-2.4.1/include
打开fcgio.h 添加#include <stdio.h>
./configure
make && make install
は test.cgi を /usr/local/nginx/html/ にコピーします

spqwn-fcgi プログラムを開始します

#include <fcgi_stdio.h>
#include <iostream>
using namespace std;

int main(int argc, char *argv[])
{
    while(FCGI_Accept() >=0)
	{
		FCGI_printf( "Status: 200 OK\r\n" );
		FCGI_printf( "Content-Type: text/html\r\n\r\n" );
		FCGI_printf( "Hello world in C\n" );
	}
    return 0;
}
を nginx.conf に追加します

g++ -o test.cgi test.cpp -L /usr/local/lib/ -lfcgi 
ブラウザを開いて次のように入力します: http://192.168.88.134:8080/test.cgi

Printアウト: Hello world in C記事参照:

http://wiki.ubuntu.org.cn/Nginx

http://blog.chinaunix.net/uid-30030431-id-4669581.html

http://terry831010.blog.163.com/blog/static/6916117120126185428827/


元の記事はブロガーによるものであり、異なる可能性がありますブロガーの許可なく転載。

上記は、nginx サーバーでの CGI プログラムの記述を内容の側面も含めて紹介しました。PHP チュートリアルに興味のある友人に役立つことを願っています。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。