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?
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:
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:
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:
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!