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/www.example.com.vhost … 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/www.example.com; # 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/www.example.com/cgi-bin because we defined root /var/www/www.example.com; in the location /cgi-bin {} container:

mkdir /var/www/www.example.com/cgi-bin 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 -> hello_world.pl):

vi /var/www/www.example.com/cgi-bin/hello_world.cgi #!/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/www.example.com/cgi-bin/hello_world.cgi Open a browser and test the script:

http://www.example.com/cgi-bin/hello_world.cgi If all goes well, you should get the following output:

https://www.howtoforge.com/images/serving_cgi_scripts_with_nginx_debian_squeeze_ubuntu_11.04/1.png

Referências