25

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?

user229044
  • 232,980
  • 40
  • 330
  • 338
Suyash Mohan
  • 483
  • 1
  • 4
  • 12
  • 7
    Technically yes, but trust me: You don't want to. – user229044 Aug 02 '11 at 13:22
  • 5
    Why so? I just love c++ so I thought why don't start using it for web sites also? – Suyash Mohan Aug 02 '11 at 13:28
  • 4
    What they don't tell you when they say php/python/etc. is that all the plumbing that does the real work is done in C++ or something similar. Web servers, web service,s and databases are not written in python. – Jay Aug 02 '11 at 14:09
  • 2
    @Jay *completely* irrelevant. By that logic we'd all be using assembly, since that's what the "plumbing" is written in. – user229044 Aug 02 '11 at 14:22
  • 1
    @meager and Griwes, I'm not saying you are wrong but saying "trust me" is not really convincing. – Eelke Aug 02 '11 at 18:49
  • @meagar: I believe in the right tool for the right job. Python is not the only tool and is not right for every job. As you noted C++ is not the right tool for every job either. It's important that the question have a balanced answer. – Jay Aug 02 '11 at 19:21
  • @Jay The underlying implementation of PHP/Python in C++ remains irrelevant, and I still can't see why you thought that was a point worth making in the context of "C++ for web development". Your original comment has nothing to do with "the right tool for the right job". – user229044 Aug 02 '11 at 19:22
  • @Eelke I said "trust me" because the fact that C++ is a *very* poor choice for web development is so self-evident that I don't know where to start. – user229044 Aug 02 '11 at 19:25
  • 5
    As I said the right tool for the right job. If the job is a site that will see low use python/php/etc. are fine. If you're implementing a search engine it's an entirely different matter. You cannot truthfully answer this question with "X is ALWAYS the correct language choice" without knowing the requirements for the developed site. I really distrust anyone who says the answer is "self evident". If it was I wouldn't be posting a rebuttal. – Jay Aug 02 '11 at 20:06

6 Answers6

22

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.

Barryvdh
  • 6,419
  • 2
  • 26
  • 23
Hello World
  • 925
  • 7
  • 18
  • Thanks for the answer! I needed to write some C++ and have it run in-process with the HTTP server, and this is the only answer that addresses both of those concerns. However, the official guide you linked to doesn't discuss C++ at all. I'm having a bit of trouble making it work, and I've opened a question [here](http://stackoverflow.com/q/42605138/791604) detailing my attempt; if you have successfully made a C++ module before, I'd love your input on what I've done wrong. – Daniel Wagner Mar 05 '17 at 06:19
  • You only need to say how to tell apache which file extensions must be parsed by the loaded module. Besides, you didn't explain how must the C++/Apache interface be. – ABu Jul 08 '17 at 18:22
15

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.

Didier Trosset
  • 36,376
  • 13
  • 83
  • 122
  • 5
    It's only written in the last line of this answer, but the "Wt" framework is the key. Have a look at it, it's really good ! – Offirmo Sep 02 '11 at 13:50
  • 1
    According to this web page CGI performs the worst. But an Apache module (see SAPI answer on this thread comes out top). http://wso2.com/library/articles/high-performance-web-services-c/ – S Meaden Sep 12 '15 at 20:08
5

If you want to code up a web site, you really want a pretty deep library, which all those "other" languages provide out of the box. If you're using Apache for most of that functionality, C++ is not the best option.

If you are still feeling adventurous and you want to use C++ to create your own custom web server, try boost::asio. An example http server is provided that will parse requests as paths and return html files from the file system.

Pro: Nothing other than C or assembler will match the low-level control you get with C++. For example, my web server handles a very specific RESTful API, and nothing else.

Con: Rather than deep library support, you will be doing a lot of work on your own, so be ready for that. For example, I just added Basic Authentication - I had to look up the appropriate HTTP RFCs, code up my own Basic header, and drop in Base64 encoding to encode the username and password. But I like that - I know exactly what is going on down to the last byte.

moodboom
  • 6,225
  • 2
  • 41
  • 45
1

After reading all the answers, I came across an easy-most idea for using C++ instead of PHP/Python/Perl.

For making the argument, I will use PHP syntax and conventions.

Php extension are written in C/C++ and are compiled. So instead of wasting time on making a bridge between the front-end server and our C code, we just write our whole web-site logic in C, and convert it into a PHP extension, or Python/Perl library. As any-body will use C/C++ instead PHP/Python/Perl for improving the speed, this solution is a nice choice.

Our PHP code will just call an initiating function exposed by our C code, packed as an extension.

This is not only alternative, but will also prevent amateurs from adding unwanted bugs while exposing their C code directly to Apache.

Ravinder Payal
  • 2,884
  • 31
  • 40
0

One of the best options is to use SWIG to generate PHP or Perl module for Apache. In that way, one can directly interface C++ class or C/C++ method or variable to PHP and access it from web server. In this example they move computationally hard part into dynamic C++ library http://novorado.com/2014/12/custom-c-module-for-apache-web-server/

John
  • 1
0

Some of your options are: (Fast)CGI, writing an Apache module or using some higher level C++ framework that works with Apache.

Nemanja Trifunovic
  • 24,346
  • 3
  • 50
  • 88