How to install fcgiwrap for Nginx on Ubuntu 20.04

Fcgiwrap is a simple server used to run CGI applications via FastCGI. We can use it to provide clean CGI support for nginx webserver. Here's how to install fcgiwrap on Ubuntu 20.04.

Fcgiwrap is a simple server used to run CGI applications via FastCGI. We can use it to provide clean CGI support for nginx webserver. It is a lightweight server with zero configuration options, allowing to use the same group to run different websites. Here's how to install fcgiwrap on Ubuntu 20.04.

CGI (Common Gateway Interface) is the original method for creating dynamic websites. The instructions below assume that you have installed and configured the Nginx server on Ubuntu Linux 20.04 LTS.

Install fcgiwrap on Ubuntu 20.04

Open a terminal and enter the following commands to update security installed packages on Ubuntu 20.04:

$ sudo apt update $ sudo apt upgrade

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

$ sudo apt install fcgiwrap

Enable fcgiwrap service on Ubuntu 20.04

Use the systemctl command as follows:

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

Configure fcgiwrap for Nginx

Now we have fcgiwrap installed, followed by creating a new configuration for the FastCGI file:

sudo nano /etc/nginx/fcgiwrap.conf

Connect 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 nginx.conf or virtual domain file. For example:

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

Next, locate the server 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 basic CGI scripts

Writing a basic CGI script using FastCGI is quite simple. First, we have to create a cgi-bin directory in / 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

How to install fcgiwrap for Nginx on Ubuntu 20.04 Picture 1How to install fcgiwrap for Nginx on Ubuntu 20.04 Picture 1 Hello World - CGI app

Open a text editor 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="06/07/2020" echo 'Hello World - CGI app' echo '' echo '

Hello World!

' echo "Computer name : $HOSTNAME
" echo "The current date and time : ${now}
" echo '' echo ''

Set execution rights on /usr/lib/cgi-bin/hello.cgi with chmod and chown:

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

Check by opening the web browser and entering the url:

https://your-domain-here/cgi-bin/hello.cgi ## For instance ## https://www.cyberciti.biz/cgi-bin/hello.cgi

You can write CGI applications or scripts in any programming language. CGI for applications / scripts can appear simple and easy, but to write those applications is not simple.

4 ★ | 24 Vote