Back to TILs

Servindo CGI com NGINX no Debian

Date: 2020-01-23Last modified: 2023-03-07
Photo by Derek Oyen on Unsplash

apt-get install fcgiwrap

Fcgiwrap is another CGI wrapper that should work also for complex CGI scripts and - like Simple CGI - can be used for shared hosting environments because it allows each vhost to use its own cgi-bin directory.

After the installation, the fcgiwrap daemon should already be started; its socket is /var/run/fcgiwrap.socket. If it is not running, you can use the /etc/init.d/fcgiwrap script to start it.

Now open your vhost configuration file…

vi /etc/nginx/sites-enabled/ … and add a location /cgi-bin {} section to the server {} container:

server { […] 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 /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) fastcgi_param SCRIPT_FILENAME documentrootfastcgi_script_name; } […] } Reload nginx:

/etc/init.d/nginx reload Next we create our cgi-bin directory - /var/www/ because we defined root /var/www/; in the location /cgi-bin {} container:

mkdir /var/www/ Now we place our CGI scripts in it and make them executable. For testing purposes I will create a small Hello World Perl script (instead of hello_world.cgi you can also use the extension .pl ->

vi /var/www/ #!/usr/bin/perl -w # Tell perl to send a html header. # So your browser gets the output # rather then (command line # on the server.) print “Content-type: text/html\n\n”; # print your basic html tags. # and the content of them. print “Hello World!! \n”; print “

Hello world

\n”; chmod 755 /var/www/ Open a browser and test the script: If all goes well, you should get the following output: