"Getting Started" Nginx + FastCGI Program (c + +) to build a high-performance Web service demo and deployment release

Source: Internet
Author: User
Tags fpm

http://blog.csdn.net/allenlinrui/article/details/19419721Category:2014-02-18 17:58 3875 people reading reviews (0) favorite reports Cc++web Development nginxfastcgi As a result of the recent work needs, I learned to use high-performance Web Server-nginx, to publish C + + written fastcgi program, the details are as follows. 1. IntroductionNginx-high-performance Web server, this needless to say, we all know.    FASTCGI Program-resident CGI program, which is a language-independent, extensible architecture of CGI open-extension, whose main behavior is to keep the CGI interpreter process in memory and thus achieve high performance. Nginx to invoke the FASTCGI program, you need to use the FASTCGI process Manager (because Nginx can not directly execute the external CGI program, we can use the spawn-fastcgi in lighttpd to allow Nginx to support external CGI operation. There are other ways to install nginx-fcgi to let Nginx support CGI, here is the method of using spawn-fastcgi, to achieve the purpose of invoking the FASTCGI program. Nginx itself does not have the integration of similar modules, and Apache has the function module, so there is no need to install the FASTCGI process management program. 2. Working principleNginx does not support direct invocation or parsing of external programs, and all external programs (including PHP) must be called through the FastCGI interface. The FastCGI interface is a socket under Linux (the socket can be either a file socket or an IP socket).    In order to invoke a CGI program, you also need a fastcgi wrapper (wrapper can be understood as the program used to start another program), which is bound to a fixed socket, such as a port or a file socket. When Nginx sends the CGI request to the socket, through the FastCGI interface, the wrapper receives the request, and then derives a new thread, which invokes the interpreter or the external program to process the script and reads the return data; The wrapper then passes the returned data through the FastCGI interface, passing it to nginx along a fixed socket, and finally, Nginx sends the returned data to the client. This is the whole process of nginx+fastcgi, as shown in 1. Figure 1 nginx+fastcgi running process?  The FastCGI interface mode initiates one or more daemons on the Script resolution server (CGI application server) to parse the dynamic script, which is the FASTCGI process Manager, or the fastcgi engine. Both spawn-fcgi and PHP-FPM are fastcgi process managers (PHP and C + +).           Introduction to Here, everyone should all have a certain understanding of the model, the following start the actual combat!        3. Environment Deployment 3.1.Nginx installation, deployment and configuration     Nginx Download Catalogue http://nginx.org/en/download.html This is what we're using nginx-1.5.10. [Install]Unzip and install after download (please remember to read the Readme) ./configure(Note similar to the checking for * * * ... not found item, which may be dependent on the package is not required to install the dependent package) missing Pcre, an additional installation of http://www.pcre.org/(or installation with Apt-get or yum) is required, missing zlib, an additional installation of http://www.zlib.net/(or installation with Apt-get or yum) is required, missing OpenSSL, you will need to install http://www.openssl.org (or install with apt-get or yum). If you need to configure the installation of additional functional modules, you can refer to thisHttp://wiki.codemongers.com/NginxChsInstall Make Make Install(Installed by default to /usr/local/nginx[Configuration and management]1) Execution Options
-C </path/to/config> Specify a configuration file for Nginx instead of the default. The default configuration file is used if you do not enter it.
-T does not run, but only tests the configuration file. Nginx will check the correctness of the configuration file syntax and try to open the file referenced in the configuration file.
-V Displays the version of Nginx.
-V Displays the Nginx version, compiler version, and configuration parameters.
2) Check the configuration file sudo./nginx-t
Nginx:the configuration file/usr/local/nginx/conf/nginx.conf syntax is OK
Nginx:configuration file/usr/local/nginx/conf/nginx.conf Test is successful
3) Start-Default and Special /usr/local/nginx/sbin/nginx(default startup mode) /usr/local/nginx/sbin/nginx-c/usr/local/nginx/conf/nginx.conf(Specify profile Start) 4) View Nginx process number (master is the main process) ps-ef | grep nginx
5) Reload the configuration file sudo kill-hup [nginx main process number]
Control Nginx via the system's signal
You can use a signaling system to control the main process. By default, Nginx writes the PID of its main process to the/usr/local/nginx/logs/nginx.pid file. Change the location of the file by passing parameters to the./configure or by using the PID directive.
The main process can handle the following signals: command description notes
term, INT quick close
QUIT calmly close
HUP Reload configuration Start a new worker process with a new configuration gracefully close the old worker process
USR1 Reopen log file
USR2 Smooth Upgrade Executable program
WINCH gracefully close the work process
6) Default directory structure home directory: /usr/local/nginx/
Configuration directory: /usr/local/nginx/conf/
Root directory: /usr/local/nginx/html/
Executable file path: /usr/local/nginx/sbin/ installation, deployment, and configuration of 3.2.spawn_fastcgispawn_fastcgi https://github.com/lighttpd/spawn-fcgi     Here is the version of 1.6.3 https://github.com/lighttpd/spawn-fcgi/releases/tag/v1.6.3 download and then unzip and install it (please remember to read the Readme) if there is no Configure, please first perform ./autogen.sh, Generate configure ./configure MakeAfter compiling, move the executable file to the Nginx sbin directory CP./src/spawn-fcgi/usr/local/nginx/sbin/(CP to nginx installation directory) 3.3.fastcgi Library Installation (library is definitely not necessary, think the technology good Daniel can write himself)     Library Address  Http://www.fastcgi.com/dist/fcgi.tar.gz    After download, unzip and install (default installation) ./configure Make Make Install      4.Demo and Web publishing 4.1.Demo Program [CGI program] [CPP]View Plaincopy
  1. #include <fcgi_stdio.h>
  2. #include <stdlib.h>
  3. int main () {
  4. int count = 0;
  5. While (fcgi_accept () >= 0) {
  6. printf ("content-type:text/html\r\n"
  7. "\ r \ n"
  8. ""  
  9. "FastCGI hello!"
  10. "Request number%d running on host%s"
  11. "Process ID:%d\n", ++count, getenv ("SERVER_NAME"), Getpid ());
  12. }
  13. return 0;
  14. }
[Compile] g++ demo.cc-o demo-lfcgi?Run the executable directly to see if it works. If there is a missing library libfcgi.so.0, you need to manually /usr/local/lib/libfcgi.so.0The library establishes a link to the/usr/lib/directory: ln-s/usr/local/libfcgi.so.0/usr/lib/(or add So's library path to/etc/ld.so.conf and perform ldconfig update) 4.2.Web Release1) Move the CGI executable to the Nginx installation directory/usr/local/nginx/cgibin (the folder does not exist) 2) Start the SPAWN-FCGI management process and bind the server IP and port (do not coincide with Nginx listening port ) /usr/local/nginx/sbin/spawn-fcgi-a 127.0.0.1-p 8088-f/usr/local/nginx/cgibin/demo
Check to see if Port 9002 is successful: Netstat-na | grep 8088? 3) Change nginx.confConfig file, let Nginx forward the request
Add a configuration in the child node of the HTTP node-"Server section" point Location ~ \.cgi$ {
Fastcgi_pass 127.0.0.1:8088;
Fastcgi_index index.cgi;
Fastcgi_param script_filename Fcgi$fastcgi_script_name;

include Fastcgi_params;     }       4) Restart Nginx or reload configuration file Reload profile sudo kill-hup [pid]Or     Re-start Nginx      killall Nginx ? ./nginx       5) Open a browser to access it http://localhost/demo.cgi
         take care of the work, the heart of a small excitement!   Allenrlin 2014/2/18 References and materials [1]nginx+fastcgi operating principle? Http://book.51cto.com/art/201202/314840.htm? [2] Under Nginx configuration fastcgi?  http://www.cppblog.com/woaidongmao/archive/2011/06/21/149090.html [3]nginx+fastcgi+c/c++ Build a high-performance web framework?    ? http://blog.csdn.net/marising/article/details/3932938? [4] What are CGI, FastCGI, php-cgi, PHP-FPM, spawn-fcgi? http://www.mike.org.cn/articles/what-is-cgi-fastcgi-php-fpm-spawn-fcgi/?

"Getting Started" Nginx + FastCGI Program (c + +) to build a high-performance Web service demo and deployment release

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.