"getting started" Nginx + FastCGI program (c + +) to build a high-performance Web service demo and deployment release

Source: Internet
Author: User
Tags fpm openssl

http://blog.csdn.net/allenlinrui/article/details/19419721

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/18References 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

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.