C++ 如何在apache服务器上使用C++

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6912761/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 20:52:34  来源:igfitidea点击:

How to use C++ for apache server

c++apache

提问by Suyash Mohan

We can integrate php, perl, python with apache and build sites in them.

我们可以将 php、perl、python 与 apache 集成并在其中构建站点。

Is it also possible to use C/C++ with apache and build web sites in it?

是否也可以将 C/C++ 与 apache 一起使用并在其中构建网站?

采纳答案by Didier Trosset

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.

您可以使用 CGI 执行基本操作:对于对您站点上的地址的每个请求,Apache 都会使用给定的可执行文件启动一个新进程。此可执行文件可以是 C++。缺点是为每个请求创建一个新进程。为了获得更好的结果,您可以使用 FastCGI,其中 CGI 进程可以针对多个不同的请求运行。

For advanced sites (read web 2.0) in C++, have a look at Wt.

对于 C++ 中的高级站点(阅读 web 2.0),请查看Wt

回答by Hello World

Three solutions exist: Cgi, Fastcgi, SAPI. I shall explain the last one.

存在三种解决方案:Cgi、Fastcgi、SAPI。我将解释最后一个。

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.

服务器应用程序编程接口(SAPI) 是用于指定 Web 服务器应用程序(如 Apache HTTP Server、Microsoft IIS 或 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:

换句话说,您可以编写一个由 Web 服务器加载的 C/C++ 库(不是“真正的”库,只是一个文件)。我将解释如何在 Linux 上使用 Apache2 完成此操作:

0. prerequisites:Apache2, Linux, command-line access.

0.先决条件:Apache2、Linux、命令行访问。

1. Get apxs2,which automatically compiles and generates an Apache2 compatible module (.sofile) out of the C/C++ file. The easiest way to obtain it on Ubuntu/Debian is sudo apt-get install apache2-threaded-dev

1、获取apxs2,它会自动.so从C/C++文件中编译生成一个Apache2兼容模块(文件)。在 Ubuntu/Debian 上获得它的最简单方法是sudo apt-get install apache2-threaded-dev

2. Write your C/C++ codeas 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

2.按照官方指南中的说明编写 C/C++ 代码。或者,你可以快速自动生成带有示例代码:apxs2 -g -n sample。这将产生几个文件,唯一感兴趣的是mod_sample.c

3. Compile:

3.编译:

apxs2 -a -c mod_sample.c

If you've written your own file, modify mod_sample.caccordingly. The resulting .so is Apache2 compatible and will be stored in your Apache modules directory.

如果您编写了自己的文件,请进行相应的修改mod_sample.c。生成的 .so 与 Apache2 兼容,并将存储在您的 Apache 模块目录中。

4. Tell apache to load the moduleby modifying /etc/apache2/apache2.confand adding:

4.通过修改/etc/apache2/apache2.conf添加告诉apache加载模块

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_moduleis 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.

您的路径可能会有所不同(/etc.../usr/lib...),具体取决于您的发行版和安装设置。另请注意,这poc_rest_module只是模块的名称,可能会更改。最后,请注意,在此示例中,仅当导航到 时才会调用模块example.com/poc_rest

5. restart Apachein order to reload the config: sudo service apache2 restart.

5.重新启动Apache以重新加载配置:sudo service apache2 restart

回答by moodboom

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.

如果你想编写一个网站,你真的需要一个非常深的库,所有那些“其他”语言都提供了开箱即用的库。如果您将 Apache 用于大部分功能,则 C++ 不是最佳选择。

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 serveris provided that will parse requests as paths and return html files from the file system.

如果您仍然喜欢冒险并且想使用 C++ 创建您自己的自定义 Web 服务器,请尝试boost::asio。提供了一个示例 http 服务器,它将请求解析为路径并从文件系统返回 html 文件。

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.

优点:除了 C 或汇编程序之外,没有任何东西可以与您使用 C++ 获得的低级控制相匹配。例如,我的 Web 服务器处理一个非常具体的 RESTful API,仅此而已。

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.

缺点:您将自己做很多工作,而不是深入的库支持,所以要为此做好准备。例如,我刚刚添加了基本身份验证 - 我必须查找适当的 HTTP RFC,编码我自己的基本标头,并使用 Base64 编码对用户名和密码进行编码。但我喜欢这样 - 我确切地知道最后一个字节发生了什么。

回答by Ravinder Payal

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

阅读完所有答案后,我发现了一个最简单的想法,即使用 C++ 而不是 PHP/Python/Perl。

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

为了进行论证,我将使用 PHP 语法和约定。

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.

Php 扩展是用 C/C++ 编写并编译的。因此,与其浪费时间在前端服务器和我们的 C 代码之间建立桥梁,我们只需用 C 编写我们的整个网站逻辑,然后将其转换为 PHP 扩展或 Python/Perl 库。由于任何人都会使用 C/C++ 而不是 PHP/Python/Perl 来提高速度,因此该解决方案是一个不错的选择。

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

我们的 PHP 代码将只调用由我们的 C 代码公开的启动函数,打包为扩展。

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

这不仅是替代方案,而且还可以防止业余爱好者在将其 C 代码直接暴露给 Apache 时添加不需要的错误。

回答by Nemanja Trifunovic

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

您的一些选择是:(快速)CGI、编写Apache 模块或使用一些与 Apache 一起使用的更高级别的 C++ 框架。

回答by John

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/

最好的选择之一是使用 SWIG 为 Apache 生成 PHP 或 Perl 模块。通过这种方式,可以直接将 C++ 类或 C/C++ 方法或变量连接到 PHP 并从 Web 服务器访问它。在这个例子中,他们将计算困难的部分移入动态 C++ 库http://novorado.com/2014/12/custom-c-module-for-apache-web-server/