Compile cgi programs on the nginx server

Source: Internet
Author: User
: This article describes how to write cgi programs on the nginx server. For more information about PHP tutorials, see. I am using the Ubuntu system.

1. install the nginx server

http://nginx.org/download/nginx-1.9.2.tar.gztar -zvxf nginx-1.9.2.tar.gzcd nginx./configuremake && make install

Default installation path:/usr/local/nginx

Simple nginx configuration:/usr/local/nginx/conf/nginx. conf

# User nobody; worker_processes 4; error_log logs/error. log; error_log logs/error. log notice; error_log logs/error. log info; pid logs/nginx. pid; events {worker_connections 1024;} http {include mime. types; default_type application/octet-stream; log_format main '$ remote_addr-$ remote_user [$ time_local] "$ request" ''$ status $ response" $ http_referer "'' "$ http_user_agent" "$ http_x_forwarded_for"'; Ccess_log logs/access. log main; sendfile on; tcp_nopush on; # keepalive_timeout 0; keepalive_timeout 90; # gzip on; server {listen 192.168.88.134: 8080; // modify it to your own IP address and server_name 192.168.88.134; # charset KOI8-R; access_log logs/host. access. log main; location/{root html; index index.html index.htm index. php; # autoindex on ;}# error_page 404/404 .html; # redirect server error pages to the static p Age/50x.html # error_page 500 502 503 504/50 x.html; location =/50x.html {root html;} # proxy the PHP scripts to Apache listening on 127.0.0.1: 80 # location ~ \. Php $ {# proxy_pass http://127.0.0.1 #}# Pass the PHP scripts to FastCGI server listening on 127.0.0.1: 9000 # location ~ \. Php $ {root html; fastcgi_pass 127.0.0.1: 9999; fastcgi_index index. php; fastcgi_param SCRIPT_FILENAME/scripts $ fastcgi_script_name; include fastcgi_params;} location ~ \. Cgi $ {root html; fastcgi_pass 127.0.0.1: 7000; fastcgi_index index. php; fastcgi_param SCRIPT_FILENAME/scripts $ fastcgi_script_name; include fastcgi_params;} # deny access. htaccess files, if Apache's document root # concurs with nginx's one # location ~ /\. Ht {# deny all ;#}}}
Start nginx:/usr/local/nginx/sbin/nginx

Disable nginx: kill 'PS aux | grep nginx | sed-n '1p' | awk' {print $2 }''

2. install spawn-fcgi

apt-get install apawn-fcgi
3. install fast-cgi
wget http://www.fastcgi.com/dist/fcgi.tar.gztar -xvzf fcgi.tar.gz
In this case, you cannot compile and install cd fcgi-2.4.1/include to open fcgio. h add # include
 
  
./Configuremake & make install
 
Copy the compiled spawn-fcgi to the nginx directory.
Cp./src/spawn-fcgi/usr/local/nginx/sbin/

Cp/usr/local/lib/libfcgi. so.0/usr/lib

The directories installed by each user may not be the same. please search and copy and modify the directories by yourself

4. write cgi programs

#include 
 
  #include 
  
   using namespace std;int main(int argc, char *argv[]){    while(FCGI_Accept() >=0){FCGI_printf( "Status: 200 OK\r\n" );FCGI_printf( "Content-Type: text/html\r\n\r\n" );FCGI_printf( "Hello world in C\n" );}    return 0;}
  
 
Compile:

g++ -o test.cgi test.cpp -L /usr/local/lib/ -lfcgi 
Copy test. cgi to/usr/local/nginx/html /.

Start the spqwn-fcgi program

spawn-fcgi -a 127.0.0.1 -p 7000 -u nobody -f ./test.cgi
Add in nginx. conf
location ~ \.cgi$ {           root           html;           fastcgi_pass   127.0.0.1:7000;           fastcgi_index  index.php;           fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;           include        fastcgi_params;        }

Open the browser and enter: http: // 192.168.88.134: 8080/test. cgi.

Output: Hello world in C

Reference:

Http://wiki.ubuntu.org.cn/Nginx

Http://blog.chinaunix.net/uid-30030431-id-4669581.html

Http://terry831010.blog.163.com/blog/static/6916117120126185428827/




Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

The above introduces the compilation of cgi programs under the nginx server, including some content, and hope to be helpful to friends who are interested in PHP tutorials.

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.