Ubuntu 18.04
Sponsored Link

Nginx : Configure CGI executable Env2018/07/02

 
Configure CGI executable Environment on Nginx.
[1] Install FastCGI Wrap and Configure Nginx for it.
root@www:~#
apt -y install fcgiwrap
root@www:~#
cp /usr/share/doc/fcgiwrap/examples/nginx.conf /etc/nginx/fcgiwrap.conf

root@www:~#
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)
  # change
  root  /var/www;

  # 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)
  # change
  fastcgi_param SCRIPT_FILENAME  $document_root$fastcgi_script_name;
}

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

root@www:~#
chmod 755 /var/www/cgi-bin

root@www:~#
vi /etc/nginx/sites-available/default
# add into the [server] section

server {
        .....
        .....
        include fcgiwrap.conf;
}

root@www:~#
systemctl restart nginx

[2] Create a test scripts under the directory you set CGI executable (on this example, it's [var/www/cgi-bin]) abd Access to it to verify CGI works normally.
root@www:~#
vi /var/www/cgi-bin/index.cgi
#!/usr/bin/perl

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

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

[3] It's possible to use Ruby or Python scripts as CGI under the directory you set.
root@www:~#
vi /var/www/cgi-bin/index.py
#!/usr/bin/env python

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

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

Matched Content