在Ubuntu 20.04上安装fcgiwrap

打开终端应用程序,然后执行以下命令以更新已安装的软件包,以确保Ubuntu 20.04的安全:

$ sudo apt update
$ sudo apt upgrade

为Nginx安装fcgiwrap软件包:

$ sudo apt install fcgiwrap

在Ubuntu 20.04上启动fcgiwrap服务

使用systemctl命令,如下所示:

$ sudo systemctl enable fcgiwrap
$ sudo systemctl start fcgiwrap
$ sudo systemctl status fcgiwrap

编写CGI脚本

使用FastCGI编写基本的CGI脚本非常简单。

首先,创建一个cgi-bin目录:

$ sudo mkdir -vp /usr/lib/cgi-bin/

然后,创建CGI脚本文件:

sudo vi /usr/lib/cgi-bin/hello.cgi

代码如下:

#!/usr/bin/env bash
echo "Content-type: text/html"
echo ""
now="$(date)"
echo '<html><head><title>Hello World - CGI app</title></head>'
echo '<body>'
 
echo '<h2>Hello World!</h2>'
echo "Computer name : $HOSTNAME<br/>"
echo "The current date and time : ${now}<br/>"
echo '</body>'
echo '</html>'

设置可执行权限:

$ sudo chmod +x -v /usr/lib/cgi-bin/hello.cgi

通过浏览器,执行cgi脚本

https://your-domain-here/cgi-bin/hello.cgi
https://www.onitroad.local/cgi-bin/hello.cgi

为Nginx配置fcgiwrap

为FastCGI文件创建一个新配置:

sudo vi /etc/nginx/fcgiwrap.conf

添加以下配置:

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;
}

编辑nginx配置文件nginx.conf或虚拟域配置文件。

sudo nano /etc/nginx/nginx.conf
## 或者
sudo nano /etc/nginx/sites-enabled/default

找到"服务器"部分,并添加以下指令:

include /etc/nginx/fcgiwrap.conf;

重新加载或者重启Nginx服务器:

$ sudo nginx -t
$ sudo nginx -s reload
如何在Ubuntu 20.04上为Nginx安装fcgiwrap

fcgiwrap是用于通过FastCGI运行CGI应用程序的简单服务器。我们可以使用它为Nginx Web服务器提供干净的CGI支持。

日期:2020-03-23 08:03:54 来源:oir作者:oir