Ubuntu 22.04
Sponsored Link

Nginx : Use CGI Scripts
2022/05/10
 
Configure CGI executable Environment on Nginx.
[1] Install FastCGI Wrap and Configure Nginx for it.
root@www:~#
apt -y install fcgiwrap
root@www:~#
vi /etc/nginx/fcgiwrap.conf
# create new
# for example, enable CGI under [/cgi-bin]

location /cgi-bin/ {
    gzip off;
    root  /var/www;
    fastcgi_pass  unix:/var/run/fcgiwrap.socket;
    include /etc/nginx/fastcgi_params;
    fastcgi_param SCRIPT_FILENAME  $document_root$fastcgi_script_name;
}

root@www:~#
mkdir /var/www/cgi-bin

root@www:~#
chmod 755 /var/www/cgi-bin
# add settings into [server] section of a site definition

root@www:~#
vi /etc/nginx/sites-available/default
server {
        .....
        .....
        include fcgiwrap.conf;
}

root@www:~#
systemctl enable fcgiwrap

root@www:~#
systemctl restart nginx

[2] Create a test scripts with a language (example below is Python3) under the directory you set CGI executable ([/usr/share/nginx/cgi-bin] on this example) and Access to it to verify CGI works normally.
root@www:~#
vi /var/www/cgi-bin/index.cgi
#!/usr/bin/python3

print("Content-type: text/html\n")
print("<html>\n<body>")
print("<div style=\"width: 100%; font-size: 40px; font-weight: bold; text-align: center;\">")
print("CGI Script Test Page")
print("</div>")
print("</body>\n</html>")

root@www:~#
chmod 705 /var/www/cgi-bin/index.cgi

Matched Content