C++ CGI Example working on Apache

CK1820 
Created at Aug 27, 2007 04:55:32 
60   0   0   0  
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!

Tags: C# CGI C# CGI Example C# CGI Sample C++ Share on Facebook Share on X

◀ PREVIOUS
Simple C# CGI working on Apache
▶ NEXT
Simple CGI Programming in C
  Comments 0
Login for comment
SIMILAR POSTS

Simple C# CGI working on Apache (created at Aug 27, 2007)

Simple CGI Programming in C (created at Aug 27, 2007)

How to send binary data through C# CGI app? (created at Aug 27, 2007)

Fast and Good Keyboard/Mouse Test without the message handler (created at Aug 27, 2007)

Using the shell to receive notification of removable media being inserted or removed (created at Aug 28, 2007)

UDP Send and Receive Using CAsyncSocket (created at Aug 28, 2007)

Creating and Using a CAsyncSocket Object to use CAsyncSocket (created at Aug 28, 2007)

MFC based World Wide Web HTTP Server Source Code (created at Aug 28, 2007)

Double Linked List based in C++ (created at Sep 08, 2007)

Binary Search Sample Code (created at Sep 08, 2007)

How to run shell command by MFC ? (created at Sep 09, 2007)

How to search file on certain directory ? (created at Jun 15, 2008)

How to call SetTimer function on MFC CDialog class ? (created at Jul 30, 2008)

How to load HTML resource on MFC ? (updated at Dec 17, 2023)

URL Encode, Decode function for MFC (updated at Dec 20, 2023)

KMP String Matching Algorithm (created at Jul 21, 2010)

OTHER POSTS IN THE SAME CATEGORY

String Replace Function For C# (created at Aug 28, 2007)

How to print character in C# by ASCII code (created at Aug 28, 2007)

MFC based World Wide Web HTTP Server Source Code (created at Aug 28, 2007)

Creating and Using a CAsyncSocket Object to use CAsyncSocket (created at Aug 28, 2007)

UDP Send and Receive Using CAsyncSocket (created at Aug 28, 2007)

Using the shell to receive notification of removable media being inserted or removed (created at Aug 28, 2007)

Adding Custom Paper Sizes To Named Printers (created at Aug 27, 2007)

Print HTML In C# With Or Without The Web Browser Control And The Print Dialog (created at Aug 27, 2007)

A Customizable Printing Text Class (created at Aug 27, 2007)

Simplified .NET Printing In C# (created at Aug 27, 2007)

How To Print Text In C# (created at Aug 27, 2007)

C Sharp Lists (created at Aug 27, 2007)

Simple Example Of IF/THEN Using C# (created at Aug 27, 2007)

Fast and Good Keyboard/Mouse Test without the message handler (created at Aug 27, 2007)

Simple CGI Programming in C (created at Aug 27, 2007)

Simple C# CGI working on Apache (created at Aug 27, 2007)

How to send binary data through C# CGI app? (created at Aug 27, 2007)

Interacting With TinyPic From C# (created at Aug 26, 2007)

Creating A Watched Folder With Assigned Events (created at Aug 26, 2007)

Calling Your Main Thread From A Worker Thread In C# (created at Aug 26, 2007)

HashTable Tutorial In C# (created at Aug 26, 2007)

Stack Tutorial In C# (created at Aug 26, 2007)

Queue Tutorial In C# (created at Aug 26, 2007)

Create PDF Files On the Fly In C Sharp (created at Aug 26, 2007)

How to call Visual C/C++ implemented DLL functions in C# - Simple DLLImport (created at Aug 26, 2007)

Send Email Using C# (created at Aug 26, 2007)

How To Read And Write A Cookie (created at Aug 26, 2007)

Using C# With Cookies (created at Aug 26, 2007)

UPDATES

Creating a Pinterest-Style Card Layout with Bootstrap and Masonry (created at Apr 24, 2024)

Mastering Excel Data Importation in PHP (updated at Apr 24, 2024)

JSON format control in PHP (updated at Apr 24, 2024)

Equal Height Blocks in Bootstrap with JavaScript (created at Apr 22, 2024)

How to convert integer to text string ? (updated at Apr 22, 2024)

Checking similarity between two strings in PHP (updated at Apr 21, 2024)

Create Blob Image in HTML based on the given Text, Width and Height in the Center of the Image without saving file (updated at Apr 21, 2024)

How do I determine the client IP type (IPv4/IPv6) in PHP (updated at Apr 16, 2024)

How do I determine the client IP type in Python - IPv4 or IPv6 (updated at Apr 13, 2024)

Getting Started with PyTorch: A Beginner's Guide to Building Your First Neural Network (updated at Apr 09, 2024)

Predicting Buyer Preferences with PyTorch: A Deep Learning Approach (updated at Apr 09, 2024)

Forecasting the Weather with PyTorch: A Beginner's Guide to Temperature Prediction (created at Apr 09, 2024)

PyTorch example to Forcast Stock Price based on 10 days Dataset (created at Apr 09, 2024)

Mastering Model Persistence: Saving and Loading Trained Machine Learning Models in Python (created at Apr 08, 2024)

Harnessing the Power of Random Forest Algorithm in Python (created at Apr 08, 2024)

Understanding and Implementing K-Nearest Neighbors (KNN) Algorithm in Python (created at Apr 08, 2024)

Forecasting with Linear Regression and KNN Regression in Python (updated at Apr 07, 2024)

What is 302 Found Redirection in HTTP 1.1? (created at Apr 04, 2024)

Mastering Random Forest Regression: A Comprehensive Guide with Python Examples (updated at Apr 01, 2024)

Python Implementation of Linear Regression (updated at Apr 01, 2024)

Mastering Supervised Machine Learning with Python: A Comprehensive Guide (created at Apr 01, 2024)

Mastering AI: A Beginner's Guide to Python Programming and Beyond (created at Apr 01, 2024)

How do I create animated background for Google Meet? (updated at Mar 28, 2024)

Building a Simple DNS Server in Delphi with TTL Support (created at Mar 16, 2024)

How to force cookies, disable php sessid in URL ? (updated at Mar 16, 2024)

Implementing a Versatile DNS Server in PHP: Handling A, AAAA, CNAME, and TXT Records (updated at Mar 16, 2024)

Implementing a Versatile DNS Server in Python: Handling A, AAAA, CNAME, and TXT Records (created at Mar 16, 2024)

Building a Basic DNS Server in PHP/Python: A Beginner's Guide (updated at Mar 15, 2024)

Dynamic DNS Made Easy: Building a Python-Based Solution (created at Mar 15, 2024)

Exploring the Depths of Data Transfer: sendfile vs. kTLS (created at Mar 15, 2024)