We can integrate php, perl, python with apache and build sites in them.
Is it also possible to use C/C++ with apache and build web sites in it?
It works. You can do basic stuff using CGI: for every request to an address on your site, Apache starts a new process with a given executable. This executable can be C++.
In contrast to a typical commercial web server's hundreds of thousands of lines of code, the HAWS web services engine consists of approximately 300 lines of code versus about 200,000 for the Apache.
Three solutions exist: Cgi, Fastcgi, SAPI. I shall explain the last one.
Server Application Programming Interface (SAPI) is the generic term used to designate direct module interfaces to web server applications such as the Apache HTTP Server, Microsoft IIS or iPlanet.
In other words, you can write a C/C++ library (Not a "real" library, just a file) which is loaded by your web server. I will explain how this can be done with Apache2 on Linux:
0. prerequisites: Apache2, Linux, command-line access.
1. Get apxs2, which automatically compiles and generates an Apache2 compatible module (.so
file) out of the C/C++ file. The easiest way to obtain it on Ubuntu/Debian is sudo apt-get install apache2-threaded-dev
2. Write your C/C++ code as explained in the official guide. Alternatively, you can quickly auto-generate a sample code with: apxs2 -g -n sample
. This will produce several files, the only one of interest is mod_sample.c
3. Compile:
apxs2 -a -c mod_sample.c
If you've written your own file, modify mod_sample.c
accordingly. The resulting .so is Apache2 compatible and will be stored in your Apache modules directory.
4. Tell apache to load the module by modifying /etc/apache2/apache2.conf
and adding:
LoadModule poc_rest_module /usr/lib/apache2/modules/mod_poc_rest.so <Location /poc_rest> SetHandler poc_rest </Location>
Your paths may differ (/etc...
and /usr/lib...
) depending on your distro and installation settings. Also note that poc_rest_module
is just the name of the module and may be changed. Finally, note that in this example the module will be called only when one navigates to example.com/poc_rest
.
5. restart Apache in order to reload the config: sudo service apache2 restart
.
It works.
You can do basic stuff using CGI: for every request to an address on your site, Apache starts a new process with a given executable. This executable can be C++. Disadvantage is that a new process is created for every request. For better results, you can use FastCGI, where the CGI process can run for several different requests.
For advanced sites (read web 2.0) in C++, have a look at Wt.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With