📜 ⬆️ ⬇️

Apache, fastcgi and c ++: "Hello, world"

img
Writing web applications in C / C ++ is a thankless task. Many say that this is complete madness when there is PHP and Perl. And I agree with them. It is very simple to write a website in PHP (especially using frameworks like Zend Framework).
But .. (there is always some "but").
Let's not forget that ease of use is not only made up of simple syntax. Many parameters are taken into account. And one of the weighty parameters is the availability of the articles “Get started with ...” with examples of the “hello, world” programs. I'm going to add a bit of simplicity to writing fastcgi in C / C ++. And if after reading this article at least one person says, “And this is not so difficult,” then I will consider my mission accomplished.

To go all the way from an empty source to the inscription on the browser screen, we will have to set up a web server (in our case Apache), install the mod_fastcgi on it, select the libfcgi library and finally write the “Hello, world” program.

Entry: FastCGI


You can read about fastcgi here . In short, this is a CGI program that processes requests in a loop. Its advantage over the usual CGI program is that it is run once to process multiple requests.

Web server


Any web server supporting fastcgi interface will work.
It so happened that all my attempts to interact with the web were carried out using the Apache web server. His choice for this article is due more likely to his presence and the work on him of other projects than any special characteristics.
')
Possible alternatives:
Nginx and Lighttpd have native support for the fastcgi interface, and their use is more preferable on production servers. You can also use MS IIS.

Mod_fastcgi, mod_fcgid


I know two modules with the help of which the fastcgi-interface in Apache is supported , it is mod_fastcgi and mod_fcgid .
Mod_fastcgi is developed by the Open Market company since 1997. The latest version 2.4.6 was updated on November 13th, 2007 and, as the authors claim, is very stable.
Mod_fcgi, judging by the domain, is developed by Chinese programmers. The latest version 2.2 is dated July 31, 2007. Distinctive features from mod_fastcgi are: a new model for managing fastcgi-programs, detecting errors in the operation of fastcgi-programs. The module has binary compatibility and therefore there is no need to recompile programs running under mod_fastgi.
Using the development kit with fastcgi.com to develop programs, I decided that it would be more appropriate to use mod_fastcgi, since they use the libfcgi shared library.

To connect the module mod_fastcgi you need to add to httpd.conf:
LoadModule fastcgi_module modules/mod_fastcgi.so
AddHandler fastcgi-script .fcg .fcgi .exe


Types of running fastcgi programs


Using mod_fastcgi, you can run programs in three different ways: dynamically, statically and remotely.

Dynamic startup method: At the time you start working, apache only creates a process manager (PM), waiting for incoming requests. As soon as the first request is received, the process manager creates an instance of the program to process it. Depending on the settings, after processing the request, the program can be completed by PM, or it can be used to process subsequent requests. Using the settings, you can set a lot of parameters of the created application pool, such as minimum and maximum size, application lifetime, maximum request queue size and others.
Directive: FastCgiConfig option [option ...]

Static method of launching: At the moment you start working, apache creates PM, which in turn creates a specified number of instances of the program to process incoming requests.
Directive: FastCgiServer filename [option ...]

Remote startup method: The application runs independently of apache and PM. PM acts as a proxy.
Directive: FastCgiExternalServer filename -host hostname:port [option ...]
FastCgiExternalServer filename -socket filename [option ...]
FastCgiExternalServer filename -host hostname:port [option ...]
FastCgiExternalServer filename -socket filename [option ...]


Interaction methods:



Stdin: Used when dynamically running fastcgi programs. The interaction takes place through the Standard in file descriptor.

Unix domain socket / Named pipe: It can be used, both for static and remote launching of fastcgi-programs. With the static socket method, a process manager is created; with the remote method, the socket must be created with a fastcgi program. To use this method, you must specify the –socket _ parameter.

TCP Socket: It can also be used as a Unix domain socket / named pipe, both with a static and remote method of running fastcgi-programs. To use in static mode, you must specify the parameter -port _tcp_ . For remote mode use the -host _:_tcp_ parameter -host _:_tcp_ .

I am primarily interested in working with tcp socket and a remote way to run a fastcgi program, because it provides compatibility with other web servers, and also provides a simpler debugging feature.

Fastcgi libraries


There are not so many libraries that help you create fastcgi programs in C / C ++. The most popular is libfcgi.lib , which comes as part of the development kit from fastcgi.com. The library, frankly, provides poor functionality for work.
There is also a Fastcgi ++ class library in C ++.
Since this is my first fastcgi program, I will use the old, proven library libfcgi.lib .

Hello_world.fcgi


The program uses TCP Socket for communication, opening port number 9000. The line “Fastcgi: Hello, world” is displayed in the browser.
Functions used:
int FCGX_Init ( void );
- Initializing the FCGX library
int FCGX_OpenSocket ( const   char * path, int backlog);
- Opens the listening socket (Parameters: path – , backlog – ).
int FCGX_InitRequest (FCGX_Request * request, int sock, int flags);
- Initialize the request structure for use inside FCGX_ Accept_r (Parameters: request – , sock – request, flags – ( : FCGI_FAIL_ACCEPT_ON_INTR – accept ).
int FCGX_Accept_r (FCGX_Request * request);
- Receives a new request for processing.

Full text of the program:
#include   <string><br/>
#include   "fcgi_stdio.h"<br/>
#include   <stdlib.h><br/>
#pragma   comment ( lib , "libfcgi.lib" )<br/>
<br/>
int main( int argc, char * const argv[] )<br/>
{<br/>
std::string port=
":9000" ; // TCP<br/>
     int listenQueueBacklog = 400; // <br/>
FCGX_Stream *in, *out, *err;<br/>
FCGX_ParamArray envp;<br/>
<br/>
<br/>
if (FCGX_Init()) exit(1); // .<br/>
<br/>
     int listen_socket = FCGX_OpenSocket(port.c_str(), listenQueueBacklog); // <br/>
     if (listen_socket < 0) exit(1);<br/>
<br/>
FCGX_Request request;<br/>
if (FCGX_InitRequest(&request, listen_socket, 0)) exit(1); // <br/>
<br/>
     while (FCGX_Accept_r(&request) == 0)<br/>
{<br/>
FCGX_FPrintF(request.out,
"Content-type: text/html\r\n\r\n<TITLE>fastcgi</TITLE>\n<H1>Fastcgi: Hello world.</H1>\n" );<br/>
<br/>
FCGX_Finish_r(&request);
// <br/>
}<br/>
<br/>
return 0;<br/>
}<br/>



Vhosts.conf


A piece of the vhost.conf configuration file responsible for helloworld.local :
NameVirtualHost 127.0.0.1: 80
< VirtualHost 127.0.0.1: 80 >
ServerAdmin mail @ localhost
DocumentRoot "C: / Apache2 / cgi-bin"
ServerName "helloworld.local"

< Directory "C: / Apache2 / cgi-bin" >
Options Indexes FollowSymLinks MultiViews ExecCGI
AllowOverride all
Order Deny , Allow
Deny from all
Allow from 127.0.0.1
</ Directory >

< Files hello_world.exe>
SetHandler fastcgi- script
</ Files >

FastCgiExternalServer C: /Apache2/cgi-bin/hello_world.exe -host 127.0.0.1: 9000
</ Virtualhost >

In the folder “C: / Apache2 / cgi-bin” I have a .htaccess file that directs all requests to helloworld.local to hello_world.exe.

Ending


Well, that's all, now in my browser the phrase “Fastcgi: Hello, world” is proudly highlighted.

Source: https://habr.com/ru/post/61532/


All Articles