[go: up one dir, main page]

0% found this document useful (0 votes)
160 views2 pages

Apache CGI Guide

The document discusses setting up an Apache web server to run compiled C/C++ applications as CGI scripts for improved performance over scripting languages. It explains enabling the CGI module in Apache and configuring file extensions to trigger CGI scripts. An example C++ program is provided that outputs a simple HTML page to demonstrate a native, fast script running via Apache. The document concludes by noting CGI scripts now have unlimited possibilities and can access databases faster than through scripting languages.

Uploaded by

Phil Martin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
160 views2 pages

Apache CGI Guide

The document discusses setting up an Apache web server to run compiled C/C++ applications as CGI scripts for improved performance over scripting languages. It explains enabling the CGI module in Apache and configuring file extensions to trigger CGI scripts. An example C++ program is provided that outputs a simple HTML page to demonstrate a native, fast script running via Apache. The document concludes by noting CGI scripts now have unlimited possibilities and can access databases faster than through scripting languages.

Uploaded by

Phil Martin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

[ Apache: C/C++ script ]

In this short howto I will explain how to setup your apache webserver to run any compiled
application as an CGI script and show a little example program to explain the main concepts.
So why does someone might have the idea to execute an application? There are so many
server side scripts available like PHP and they make most tasks much easier to accomplish
than a complicated programming language like C++.

But In some cases it's really worth to consider using a compiled application as this could lead
to massive speed gains. A server side script application first has to parse the script and only
after that translates it to corresponding compiled functions. This takes time and if you need a
production environement that gets as much as possible out of your server, why not create an
highly optimized C++ script?

Apache setup
In this howto I will just show you to setup this feature with Apache as it's the most widely
used webserver. If you're using some other webserver, setup steps will be somewhat similiar
so you might want to take a look at the documentation.

First of all, you'll have to check whether the cgi module is enabled. Either you've compiled cgi
in or it's available as a module. In the latter case, you'll have to add something like this to you
httpd.conf:

LoadModule cgi_module /usr/lib/apache2/modules/mod_cgi.so

Of course this depends from your architecture and operating system. This line is taken from a
Debian Linux system. Besides that you'll have to tell Apache that every file with a certain
extension - in our case .bin or .exe depending on what you'd like to name your executable - is
treated as a cgi script so as a exeutable file. This has to be in you apache configuration file:

AddHandler cgi-script .bin


AddHandler cgi-script .exe

You can, of course, set any file extension you'd like. There is something you'll have to consider
when your system is running under Linux: apache will only execute those files that are marked
as executable in the filesystem so a chmod 700 might help in case nothing happens.

Your Apache webserver is now setup to accept any precompiled script! Next comes a little
example that shows how to get your native and fast script up'n running.
Example C++ script
The possibilities now are unlimited: you could use the MySQL client libraries to access your
database like you did before with PHP but as the script will be precompiled, the whole process
will be much faster. Anyway the base of the script will be somewhat similiar to this example
code:

#include <iostream>

using namespace std;

int main(int argc, char* argv[])


{
cout << "Content-type: text/html" << endl << endl << endl;

cout << "<html><head><title>Hello!</title></head><body>" << endl


<< "Hi man! See my C++ CGI app?!" << endl
<< "<h1>Good!</h1>" << endl
<< "</body>";

return 0;
}

Compile this script - under Linux with g++ -o test.bin test.cpp - and copy it into your
apache htdocs/ folder and finito! Of course you could use argc and argv to get the parameters
passed to the script via the HTTP request! Happy c0ding!

You might also like