如何在 Ubuntu 22.04 / 20.04 上为 Nginx 安装 fcgiwrap
CGI 表示通用网关接口,它是创建动态页面的早期方法之一。另一个很好的例子是告诉您的 Web 服务器执行 Unix 程序等。本教程假设您已在 Ubuntu Linux 20.04 和 22.04 LTS 上安装和配置了 Nginx 服务器。
教程要求 | |
---|---|
要求 | Ubuntu Linux |
Root 权限 | 是的 |
难度等级 | 简单的 |
先决条件 | Nginx |
操作系统兼容性 | Debian • Mint • Pop!_OS • Ubuntu |
预计阅读时间 | 4 分钟 |
在 Ubuntu 20.04/22.04 上安装 fcgiwrap
打开终端应用程序,然后键入以下命令来更新 Ubuntu 20.04 上已安装的安全软件包:要为 Nginx 安装 fcgiwrap 包,请在apt 命令
的帮助下以 root 用户身份运行以下命令:示例输出:
$ sudo apt update
$ sudo apt upgrade
$ sudo apt install fcgiwrap
Reading package lists... Done Building dependency tree Reading state information... Done The following additional packages will be installed: libfcgi-bin libfcgi0ldbl The following NEW packages will be installed: fcgiwrap libfcgi-bin libfcgi0ldbl 0 upgraded, 3 newly installed, 0 to remove and 0 not upgraded. Need to get 182 kB of archives. After this operation, 584 kB of additional disk space will be used. Do you want to continue? [Y/n] y Get:1 http://archive.ubuntu.com/ubuntu focal/universe amd64 libfcgi0ldbl amd64 2.4.0-10build1 [155 kB] Get:2 http://archive.ubuntu.com/ubuntu focal/universe amd64 fcgiwrap amd64 1.1.0-12 [16.8 kB] Get:3 http://archive.ubuntu.com/ubuntu focal/universe amd64 libfcgi-bin amd64 2.4.0-10build1 [10.5 kB] Fetched 182 kB in 1s (217 kB/s) Selecting previously unselected package libfcgi0ldbl:amd64. (Reading database ... 19280 files and directories currently installed.) Preparing to unpack .../libfcgi0ldbl_2.4.0-10build1_amd64.deb ... Unpacking libfcgi0ldbl:amd64 (2.4.0-10build1) ... Selecting previously unselected package fcgiwrap. Preparing to unpack .../fcgiwrap_1.1.0-12_amd64.deb ... Unpacking fcgiwrap (1.1.0-12) ... Selecting previously unselected package libfcgi-bin. Preparing to unpack .../libfcgi-bin_2.4.0-10build1_amd64.deb ... Unpacking libfcgi-bin (2.4.0-10build1) ... Setting up libfcgi0ldbl:amd64 (2.4.0-10build1) ... Setting up libfcgi-bin (2.4.0-10build1) ... Setting up fcgiwrap (1.1.0-12) ... Processing triggers for libc-bin (2.31-0ubuntu9) ... Processing triggers for systemd (245.4-4ubuntu3.1) ...
在 Ubuntu 20.04 上开启 fcgiwrap 服务
使用systemctl命令如下:
$ sudo systemctl enable fcgiwrap
$ sudo systemctl start fcgiwrap
$ sudo systemctl status fcgiwrap
● fcgiwrap.service - Simple CGI Server Loaded: loaded (/lib/systemd/system/fcgiwrap.service; indirect; vendor preset: enabled) Active: active (running) since Fri 2020-06-05 08:59:45 UTC; 1min 20s ago TriggeredBy: ● fcgiwrap.socket Main PID: 113093 (fcgiwrap) Tasks: 1 (limit: 115783) Memory: 608.0K CGroup: /system.slice/fcgiwrap.service └─113093 /usr/sbin/fcgiwrap -f Jun 05 08:59:45 utls-wp-mg-www-cbz systemd[1]: Started Simple CGI Server.
为 Nginx 配置 fcgiwrap
现在,我们已经安装了 fcgiwrap,是时候为 FastCGI 文件创建一个新的配置了,如下所示:
$ sudo nano /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.conf 或虚拟域配置文件。例如:
接下来,找到该部分并添加以下指令:
$ sudo nano /etc/nginx/nginx.conf
## OR ##
$ sudo nano /etc/nginx/sites-enabled/default
Server
## Trun on /cgi-bin/ support to run CGI apps ## include /etc/nginx/fcgiwrap.conf;
保存并关闭文件。重新加载或重新启动 Nginx 服务器:
$ sudo nginx -t
$ sudo nginx -s reload
编写第一个 CGI 脚本
使用 FastCGI 编写基本 CGI 脚本非常简单。但首先,我们必须使用mkdir 命令在 /usr/lib/ 下创建一个 cgi-bin 目录:
$ sudo mkdir -vp /usr/lib/cgi-bin/
mkdir: created directory '/usr/lib/cgi-bin/'
Bash 中的 Hello World CGI 脚本
打开您选择的文本编辑器并创建以下文件:
$ sudo vi /usr/lib/cgi-bin/hello.cgi
附加以下 bash 代码:
#!/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>'
/usr/lib/cgi-bin/hello.cgi使用 chmod 命令和 chown 命令设置可执行权限:
测试一下。打开 Web 浏览器并输入 url:
您可以使用任何编程语言(例如 Perl 或 C)编写 CGI 应用程序或脚本。乍一看,应用程序/脚本的 CGI 可能看起来简单易用,但编写此类应用程序时必须小心谨慎。我建议阅读owasp 网页以了解对 Web 应用程序或脚本的攻击。
$ sudo chmod +x -v /usr/lib/cgi-bin/hello.cgi
mode of '/usr/lib/cgi-bin/hello.cgi' changed from 0644 (rw-r--r--) to 0755 (rwxr-xr-x)
https://your-domain-here/cgi-bin/hello.cgi
## For instance ##
https://www.example.com/cgi-bin/hello.cgi
结论
我向您解释了如何在 Ubuntu 20.04 或 22.04 LTS Linux 服务器上为 Nginx 安装 fcgiwrap。请参阅此处的 CGI 脚本项目主页的简单 FastCGI 包装器。