泰晓科技 -- 聚焦嵌入式 Linux - 追本溯源,见微知著!
网站地址:http://tinylab.org
微信公众号关注我们新浪微博


扫一扫

关注 @泰晓科技
原创服务 | 实验云台 | 共享书籍 | 直播回放
请稍侯

Add CGI support for Nginx

Wu Zhangjin 创作于 2013/12/26

by falcon of TinyLab.org 2013/12/26

Introduction

Nginx has no builtin CGI support for it cannot call external executables directly, but it allows to perform external executables via the FastCGI interface.

In Linux, FastCGI works in a socket, file socket: unix:/var/run/xxx.socket or Ip socket: 127.0.0.1:9000.

To add CGI support, some extra wrappers must be installed, the wrappers are servers who can fire a thread which execute the external programs, the architecture looks like:

  • Post

    • Internet –> Nginx –> socket –> FastCGI/Wrappers –> CGI Applications
  • Get

    • Internet <– Nginx <– socket <– FastCGI/Wrappers <– CGI Applications

Install FastCGI

First off, Nginx does not provide FastCGI for you, so you’ve got to have a way to spawn your own FastCGI processes, here install spawn-fcgi.

$ sudo apt-get install spawn-fcgi

PHP: php5-fpm

To support PHP, the php5-fpm should be installed:

$ sudp apt-get install php5-fpm

Then, configure your site’s configuration: /etc/nginx/site-available/default

server {
    ...
    index index.php index.html index.htm index.xml index.xhtml;
    ...
    location / {
        try_files $uri $uri/ /index.php?q=$request_uri;
    }
    ...
    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    location ~ \.php$ {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        include fastcgi_params;
    }
}

Then, start the server:

$ sudo service fcgiwrap restart

Shell, Perl: fcgiwrap

To support the other CGI scripts, we can install fcgiwrap.

$ sudo apt-get install fcgiwrap

Configure it, first off, copy the example config to /etc/nginx:

$ dpkg -L fcgiwrap | grep nginx.conf
/usr/share/doc/fcgiwrap/examples/nginx.conf
$ cp /usr/share/doc/fcgiwrap/examples/nginx.conf /etc/nginx/fcgiwrap.conf

Then, include the file to the server note of your site’s configuration: /etc/nginx/site-available/default

server {
    ...
    include /etc/nginx/fcgiwrap.conf;
}

The fcgiwrap.conf looks like:

# Include this file on your nginx.conf to support debian cgi-bin scripts using
# fcgiwrap
location /cgi-bin/ {
  # Disable gzip (it makes scripts feel slower since they have to complete
  # before getting gzipped)
  gzip off;

  # Set the root to /usr/lib (inside this location this means that we are
  # giving access to the files under /usr/lib/cgi-bin)
  root  /usr/lib;

  # Fastcgi socket
  fastcgi_pass  unix:/var/run/fcgiwrap.socket;

  # Fastcgi parameters, include the standard ones
  include /etc/nginx/fastcgi_params;

  # Adjust non standard parameters (SCRIPT_FILENAME)
  fastcgi_param SCRIPT_FILENAME  /usr/lib$fastcgi_script_name;
}

Read Latest: