How to install fcgiwrap for Nginx on Ubuntu 20.04

The fcgiwrap is a simple server for running CGI apps over FastCGI. We can use it to provide clean CGI support to the nginx webserver. It is a lightweight server with zero-configuration options, making it possible to use the same pool to run different sites. This page explains how to install fcgiwrap on Ubuntu 20.04.

CGI means Common Gateway Interface, and it is one early method for creating dynamic pages. Another good example is telling your web servers to execute Unix programs and more. This tutorial assumes that you have installed and configured the Nginx server on Ubuntu Linux 20.04 LTS.
Tutorial requirements
Operating system/appUbuntu Linux
Root privileges requiredYes
DifficultyEasy (rss)
Estimated completion time5m
Table of contents

ADVERTISEMENTS

Installing fcgiwrap on Ubuntu 20.04

Open the terminal application and then type the following commands to update installed packages for security on Ubuntu 20.04:
$ sudo apt update
$ sudo apt upgrade

To install the fcgiwrap packages for Nginx, run the following command as root user with the help of [nixmcd name=”apt”]:
$ sudo apt install fcgiwrap
Sample outputs:

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) ...

Turn on fcgiwrap service on Ubuntu 20.04

Use the systemctl command as follows:
$ 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.

Configuring fcgiwrap for Nginx

Now, that we installed fcgiwrap, it is time to create a new configuration for FastCGI file as follows:
sudo nano /etc/nginx/fcgiwrap.conf
Append the following configurations:

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

Edit your nginx.conf or virtual domain config file. For example:
sudo nano /etc/nginx/nginx.conf
## OR ##
sudo nano /etc/nginx/sites-enabled/default

Next, locate the Server section and add the following directive:

## Trun on /cgi-bin/ support to run CGI apps ##
include /etc/nginx/fcgiwrap.conf;

Save and close the file. Reload or restart the Nginx server:
$ sudo nginx -t
$ sudo nginx -s reload

Writing your first CGI script

Writing a basic CGI script using FastCGI is pretty straightforward. But, first, we must create a cgi-bin directory under /usr/lib/ using the mkdir command:
$ sudo mkdir -vp /usr/lib/cgi-bin/
mkdir: created directory '/usr/lib/cgi-bin/'

Hello World CGI script in Bash

Open a text editor of your choice and create the following file:
sudo vi /usr/lib/cgi-bin/hello.cgi
Append the following bash code:

#!/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>'

Set the executable permission on the /usr/lib/cgi-bin/hello.cgi using chmod command and chown command:
$ 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)

Test it. Open a web-browser and type url:
https://your-domain-here/cgi-bin/hello.cgi
## For instance ##
https://www.cyberciti.biz/cgi-bin/hello.cgi

Test and install fcgiwrap for Nginx on Ubuntu 20.04
You can write CGI apps or script in any programming languages of your wish, such as Perl or C. At a glance, CGI for apps/script may appear simple and easy, but care must be taken to write such apps. I would suggest reading owasp webpages to understand attacks on web apps or scripts.

Conclusion

I explained to you how to installed fcgiwrap for Nginx on Ubuntu 20.04 Linux server. See Simple FastCGI wrapper for the CGI scripts project home page here.

🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallCentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNCentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
0 comments… add one

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.