Serving CGI Scripts With Nginx On Debian Squeeze/Ubuntu 11.04
|
Submitted by falko (Contact Author) (Forums) on Thu, 2011-09-29 17:07. :: Debian | Ubuntu | Web Server | nginx
Serving CGI Scripts With Nginx On Debian Squeeze/Ubuntu 11.04Version 1.0 This tutorial shows how you can serve CGI scripts (Perl scripts) with nginx on Debian Squeeze/Ubuntu 11.04. While nginx itself does not serve CGI, there are several ways to work around this. I will outline three solutions: the first is to proxy requests for CGI scripts to Thttpd, a small web server that has CGI support, while the second and third solution are very similar - both use a CGI wrapper to serve CGI scripts. I do not issue any guarantee that this will work for you!
1 Preliminary NoteI'm using the website www.example.com here with the document root /var/www/www.example.com/web/ and the Nginx vhost configuration file /etc/nginx/sites-enabled/www.example.com.vhost. A note for Ubuntu users: Because we must run all the steps from this tutorial with root privileges, we can either prepend all commands in this tutorial with the string sudo, or we become root right now by typing sudo su
2 Using ThttpdIn this chapter I am going to describe how to configure nginx to proxy requests for CGI scripts (extensions .cgi or .pl) to Thttpd. I will configure Thttpd to run on port 8000. First we install Thttpd: apt-get install thttpd The nginx ThttpdCGI page says that Thttpd should be patched - fortunately this patch is already included in Debian's and Ubuntu's thttpd package, so we don't need to do this. Next we open /etc/default/thttpd... vi /etc/default/thttpd ... and set ENABLED to yes:
Then we make a backup of the original /etc/thttpd/thttpd.conf file and create a new one as follows: mv /etc/thttpd/thttpd.conf /etc/thttpd/thttpd.conf_orig
This will make Thttpd listen on port 8000 on 127.0.0.1; its document root is /var/www. Start Thttpd: /etc/init.d/thttpd start Next create /etc/nginx/proxy.conf: vi /etc/nginx/proxy.conf
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:
Reload nginx: /etc/init.d/nginx reload Because Thttpd's document root is /var/www, location /cgi-bin translates to the directory /var/www/cgi-bin (this is true for all your vhosts, which means each vhost must place its CGI scripts in /var/www/cgi-bin; this is a drawback for shared hosting environments; the solution is to use a CGI wrapper as described in chapters 3 and 4 instead of Thttpd). Create the directory... mkdir /var/www/cgi-bin ... and then place your 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/cgi-bin/hello_world.cgi
chmod 755 /var/www/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:
|





Recent comments
21 hours 20 min ago
1 day 4 hours ago
1 day 7 hours ago
1 day 9 hours ago
1 day 17 hours ago
2 days 3 hours ago
2 days 4 hours ago
2 days 7 hours ago
2 days 12 hours ago
2 days 12 hours ago