Running C/C++ Code as a CGI Script

When I first had the realization that this was not only possible, but really simple, I was very excited. Using C/C++ to create dynamic web applications isn’t new. In fact, C and Perl were originally the primary method for dynamic web based applications. Well, the Internet has been around long enough, that even Perl is beginning to become obsolete (Thank God).

C++ Web applications are not going to be a game-changer. If this were true, it would have happened a long time ago. Will I forget about PHP? Of course not! Many (probably most) shared hosting solutions won’t even allow you (with very good reason) to execute arbitrary binary files on their servers. Mine won’t. You also can’t (to my knowledge) do this technique on Windows servers.

I WILL, probably, write a few C++ web applications to run on my laptop, just because I’m a nerd, and the satisfaction I’d get if from just knowing every time I hit the Home button, I’m seeing web pages dished up by a C++ script… I’ll stop there.

What kind of solutions do I see this as a replacement for PHP (or Ruby, or Python, or ASP, or JSP, or whatever you use)?

  • You are looking to build a robust web application to be run on your own private web servers.
  • I’m thinking, if Twitter was written in C/C++, we wouldn’t have as many Twitter-outages :) .

  • You are looking to build a high-end distributable web-based software package.
  • For example, a software package, where the codebase sits on a server, and clients (or internal employees), interface with it through a web browser. Now we’re talking APPLICATION, more than just a website.

  • You are looking to build a web application, and have the ability to run arbitrary binary scripts on your server (such as a private server), and runtime speed is crucial.
  • With FastCGI and precompiled binary scripts, well-written C/C++ code will trump compile-on-the-fly approaches of PHP, Perl, Python, etc. (Of course PHP has memcache..)

Enough jibber-jabber. Let’s create a C++ CGI script! First, you’ll need to configure Apache to execute CGI scripts. It’s general practice (but you may not care.. I don’t) to create a single directory, and only allow CGI scripts in that directory to be executed. You’ve probably seen a lot of cgi-bin/ directories on various websites. Let’s say we create a directory at /var/www/cgi. In this directory, we’ll put our CGI scripts. Let’s tell Apache.

You’ll want to edit your httpd.conf file (on Ubuntu, it’s in /etc/apache2). Add this (as root/sudo):

<Directory /var/www/cgi>
    Options ExecCGI+
    AddHandler cgi-script .cgi
</Directory>

The Options ExecCGI+ line is the one that allows CGI scripts to be executed. If instead of using a single directory, you opted to make the whole ServerRoot allow CGI scripts (like I did), you’ll want to make sure the Options aren’t overridden elsewhere in the server conf. Namely, check for something in /etc/apache2/sites-available/default (or where ever else your system may store Apache config). In this file, You may see another <Directory> block for your server root. Add ExecCGI to the Options list.

You can also create arbitrary file extensions for your CGI scripts with the AddHandler directive. Imagine the possibilities.

Now, restart Apache. On Ubuntu:

sudo /etc/init.d/apache2 restart

When Apache comes back up, you should be ready to roll. You may feel like throwing in a test Perl script before we get to the C++, just to make sure things are working as expected. If you aren’t a Perl Monk (most of us aren’t), do this:

which perl

Will tell you where perl is installed (if at all). It’s probably /usr/bin/perl. So then, create this Perl script:

#!/usr/bin/perl

print qq(Content-type: text/html\n\n);
print qq(Hello, world!);

Make sure to chmod that bad boy to at least 755, and hit it in the browser, you should see “Hello, world!”. If not, you probably got one of these:

  • You saw the perl code
  • That means the CGI script didn’t attempt to execute, check back over the steps, make sure you restarted Apache.

  • You got Forbidden
  • You either aren’t allowed to execute CGI scripts, or didn’t get the right permissions.

  • Internal Server Error
  • Perl code is probably messed up. Check out tail /var/logs/apache2/error.log for what SHOULD be a more detailed error message.

  • File not found
  • You probably have a typo in the filename or the address bar :) .

Well, hopefully you have that working now, lets throw down some C++ code.

I’m not going to teach you C++, so if this code doesn’t make sense, you should look into learning C++ before approaching this technique (obviously).


#include <iostream>
#include <cstdlib>

using namespace std;

int main() {

	cout << "Content-type: text/html\n\n";
	cout << "Hello World (Wide Web)<br />" << endl;

	cout << getenv("REMOTE_ADDR") << endl;

}

Save this as hello.C, or whatever you want.. and compile the code:

g++ -o hello.cgi hello.C

Now, make sure that hello.cgi is in /var/www/cgi (or wherever you specified), and hit it in the web browser. You should see an output something like:

Hello World (Wide Web)
127.0.1.1

One of the biggest pitfalls I can foresee, is that server-side scripting is not an interactive technique. Thats why scripting languages are perfect for dynamic web pages. C++, not being a scripting language by nature may cause you some headaches. Just be sure to write smart, efficient code.

You can also download a C++ CGI library, to help out with accessing header data, such as Cookies, GET and POST variables, etc. Here is a link to an ANSI C library for CGI Programming.

3 Responses to “Running C/C++ Code as a CGI Script”

  1. [...] Jordan web developer « Bought a New Book about Website Optimization Running C/C++ Code as a CGI Script [...]

  2. [...] Running C/C++ Code as a CGI Script « Sean Jordan—web developer By admin | category: cgi script, scripts | tags: apache, cgi, cgi script, [...]

  3. Roji says:

    Thanks for the tips

Leave a Reply