bitbitbit
Posts: 30
Joined: Thu Aug 30, 2012 3:18 am

CGI on Pi

Fri Oct 12, 2012 8:26 am

hi everybody....

sorry for asking dumb questions....but i'm not very familiar with webpage editing....

I'm trying to get a CGI (written by C language) to work as a webpage.....

Say I have a hello world program named hello.c, I have complied it to "hello.cgi" and moved it to /var/www

So when I open my browser and enter address /hostname/hello.cgi.......it starts downloading the "hello.cgi" file instead of showing the file as a webpage......

I guess I have to config the apache????
I read through http://httpd.apache.org/docs/2.0/howto/cgi.html but I really have no idea what it's talking about.......

Perhaps anybody can point me to the right direction? or where the apache config file for CGI is located??

Many thanks!!!

User avatar
jackokring
Posts: 816
Joined: Tue Jul 31, 2012 8:27 am
Location: London, UK
Contact: ICQ

Re: CGI on Pi

Fri Oct 12, 2012 1:27 pm

the cgi-bin directory is served from where, as all executable cgi code has to be there. You'll have to look at the apache config files. httpd.conf and others.
Pi[NFA]=B256R0USB CL4SD8GB Raspbian Stock.
Pi[Work]=A+256 CL4SD8GB Raspbian Stock.
My favourite constant 1.65056745028

User avatar
stevepdp
Posts: 285
Joined: Fri Oct 28, 2011 7:41 am
Contact: Website Twitter

Re: CGI on Pi

Fri Oct 12, 2012 3:56 pm

I'm sort of new to this myself too, although I've been experimenting with Python scripts rather than C.

Still, the info I have on this may prove useful to you.

Assuming you're running Raspbian (Debian), you'll have website definitions in the directory /etc/apache2/sites-available/. I for example have a file called stevepdp in there which is the development site for my personal website.

That file, looks like this:

Code: Select all

<VirtualHost *:80>
	ServerAdmin webmaster@localhost
*	ServerName stevepdp.local

*	DocumentRoot /home/stevepdp/data/projects/www/stevepdp
*	<Directory /home/stevepdp/data/projects/www/stevepdp/>
*		Options +ExecCGI
*		DirectoryIndex index.py
		AllowOverride None
		Order allow,deny
		allow from all
*		AddHandler cgi-script .py
*		PythonDebug On
	</Directory>

	ErrorLog ${APACHE_LOG_DIR}/error.log

	# Possible values include: debug, info, notice, warn, error, crit,
	# alert, emerg.
	LogLevel warn

	CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
The lines you'll most likely need to consider are those I've marked with a *. For C programming for example, you'll probably be need to add a .cgi handler like so:

Code: Select all

AddHandler cgi-script .cgi
Your directory index line could therefore be:

Code: Select all

DirectoryIndex index.cgi
Once you have a complete sites-available file, you must add the site to Apache by running:

Code: Select all

a2ensite stevepdp

Code: Select all

sudo service apache2 restart
I then have a hosts file entry (/etc/hosts) which lists each of my development sites and allows me to browse to them at addresses like http://stevepdp.local or http://norwichrpi.local.

The rules look like this:

Code: Select all

127.0.1.1 stevepdp.local

Code: Select all

127.0.1.1 norwichrpi.local
The other thing to note is that your CGI script, if it prints out to the browser, must print the HTML content-type before anything else. The following C++ example came from StackOverflow:

Code: Select all

#include <iostream>
using namespace std;

int main()
{
    cout << "Content-Type: text/html" << endl << endl;
    cout << "Hello to Apache and Firefox!" << endl;
    return 0;
}
Use the /etc/apache2/sites-available/default file as a template and work from there.

I hope this info helps.

User avatar
stevepdp
Posts: 285
Joined: Fri Oct 28, 2011 7:41 am
Contact: Website Twitter

Re: CGI on Pi

Fri Oct 12, 2012 4:07 pm

jackokring wrote:the cgi-bin directory is served from where, as all executable cgi code has to be there. You'll have to look at the apache config files. httpd.conf and others.
Could you please explain why it's often recommended that all cgi scripts go into their own cgi-bin directory? I've been able to run a directory hierarchy like this with no problem:

Code: Select all

stevepdp/
   css/
      style.css
   mod/
      __init.py__
      css.py
      foot.py
      nav.py
   index.py
   about.py
   contact.py
   favicon.ico
   projects.py
Similarly, I've also written php based websites with a similar structure and those have operated fine.

Is it simply a matter of security? Why?

Return to “General programming discussion”