We can integrate php, perl, python with开发者_JS百科 apache and build sites in them.
Is it also possible to use C/C++ with apache and build web sites in it?
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 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.
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.
Some of your options are: (Fast)CGI, writing an Apache module or using some higher level C++ framework that works with Apache.
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/
精彩评论