C++ CGI Example working on Apache

CK CK 3788 
Created at  
484 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++ Facebook X
Comments 0
Similar posts
  1. Simple C# CGI working on Apache
    480
  2. Simple CGI Programming in C
    545
  3. How to send binary data through C# CGI app?
    994
  4. Fast and Good Keyboard/Mouse Test without the message handler
    982
  5. Creating and Using a CAsyncSocket Object to use CAsyncSocket
    472
  6. MFC based World Wide Web HTTP Server Source Code
    459
  7. Double Linked List based in C++
    796
  8. Binary Search Sample Code
    405
  9. How to run shell command by MFC ?
    435
  10. How to search file on certain directory ?
    629
  11. How to call SetTimer function on MFC CDialog class ?
    650
  12. Makefile to automate the build process for C++
    279
  13. How to load HTML resource on MFC ?
    841
  14. URL Encode, Decode function for MFC
    604
  15. KMP String Matching Algorithm
    672
  1. String Replace Function For C#
    1,547
  2. How to print character in C# by ASCII code
    476
  3. MFC based World Wide Web HTTP Server Source Code
    459
  4. Creating and Using a CAsyncSocket Object to use CAsyncSocket
    472
  5. UDP Send and Receive Using CAsyncSocket
    1,204
  6. Using the shell to receive notification of removable media being inserted or removed
    680
  7. Adding Custom Paper Sizes To Named Printers
    646
  8. Print HTML In C# With Or Without The Web Browser Control And The Print Dialog
    972
  9. A Customizable Printing Text Class
    432
  10. Simplified .NET Printing In C#
    692
  11. How To Print Text In C#
    455
  12. C# Lists
    1,151
  13. Simple Example of IF/THEN using C#
    1,265
  14. Fast and Good Keyboard/Mouse Test without the message handler
    982
  15. Simple CGI Programming in C
    545
  16. Simple C# CGI working on Apache
    480
  17. How to send binary data through C# CGI app?
    994
  18. Interacting With TinyPic From C#
    1,303
  19. Creating A Watched Folder With Assigned Events
    1,658
  20. Calling Your Main Thread From A Worker Thread In C#
    546
  21. HashTable Tutorial In C#
    1,787
  22. Stack Tutorial In C#
    668
  23. Queue Tutorial In C#
    1,737
  24. Create PDF Files On the Fly in C#
    1,002
  25. How to call Visual C/C++ implemented DLL functions in C# - Simple DLLImport
    1,160
  26. Send Email Using C#
    1,377
  27. How To Read And Write A Cookie
    363
  28. Using C# With Cookies
    625
Recently updated
  1. Why 100% SEO Guarantees Better User Engagement and How to Achieve It in Your Service
    40
  2. Why SPF and DKIM Are Essential for Linux Email Servers?
    34
  3. How do I create animated background for Google Meet?
    20,611
  4. MaxMind GeoLite2 vs. DB-IP Lite vs. ip-api.com: Choosing the Right Free IP Geolocation Solution
    244
  5. Meet Valkey: Why Redis Changed Its License, Why Valkey is Better, and How to Use It as a Drop-in Replacement
    94
  6. How Rogue Web Scrapers Are Stealing My Content and Destroying My Server
    157
  7. Array of arguments passed to script in PHP
    90
  8. Invalid Auction Storms in Ad Tech
    98
  9. Building MRC-Compliant Instrumentation in Ad Tech: Implementation Steps and Key Challenges
    202
  10. The Two Clocks of Ad Server Architecture
    70
  11. Frequency Cap Architecture Overview
    514
  12. Collecting Proof-of-Play from 100,000 Screens
    146
  13. Vector Search in Relational Databases (RDBs): Trends & Usage Environments
    287
  14. What is RedisVL?
    109
  15. Designing a Skill Registry Architecture: Minimize Token Usage and Maximize AI Performance
    230
  16. TPU & GPU Cluster Management Tools
    243
  17. The Concept of Scaffolding in AI Agents
    233
  18. Beyond Static Pipelines: Architecting LLM Multi-Agent Systems for Autonomous Workflow Orchestration
    583
  19. AI-DLC vs. AI-SDLC: Model-Centric vs. System-Centric AI Development
    258
  20. Fine-Tuning Open-Weight LLMs: Training, Licensing, and Deploying Custom Models with Ollama
    339
  21. Inside the Next-Generation Streaming Architecture: Ultra-Low Latency, SGAI, and AI Codecs
    202
  22. Machine Learning: Training vs. Ranking
    221
  23. The Continuous Learning Machine: Designing a Loop That Actually Turns
    222
  24. The Next-Gen Low-Latency Standards: LL-HLS & MoQ
    195
  25. Designing a Data Curation System Around a Vector Database
    220
  26. Extracting Context from Live Video: The Architecture Got Simpler When We Stopped Building Features
    256
  27. The New Ad Stack Behind Live Sports Streaming: Inside SGAI, EABN, and the Standards Race to Fix Live Monetization
    656
  28. Designing MLOps Architectures to Handle Seasonal Data Drift in EV Fleet Models
    195
  29. From 15 to 40+ FPS: Accelerating PyTorch Object Detection on Automotive ECUs
    216
  30. Understanding Data Lineage
    260