[版权声明] 本站内容采用 知识共享署名-非商业性使用-相同方式共享 3.0 中国大陆 (CC BY-NC-SA 3.0 CN) 进行许可。
部分内容和资源来自网络,纯学习研究使用。如有侵犯您的权益,请及时联系我,我将尽快处理。
如转载请注明来自: Broly的博客,本文链接: 搭建CentOS+NGINX+Spawn-fcgi+CPP开发环境
搭建RPC服务器的方案有很多,其中就是有基于C++语言的。今天尝试搭建了个CentOS+NGINX+Spawn-fcgi+CPP的开发环境。
一、安装CentOS、Nginx
参考我之前的博文《CENTOS+NGNIX+TOMCAT+MARIADB+JAVA环境搭建》
二、安装GCC、GCC-C++等工具
1 |
yum install gcc gcc-c++ autoconf automake |
三、安装Spawn-fcgi
1 |
yum install fcgi-devel spawn-fcgi |
四、编写FCGI程序并编译
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#include <stdlib.h> #include <fcgi_stdio.h> int main(int argc, char **argv) { int count = 0; while(FCGI_Accept() >= 0) printf("Content-type: text/html\n\n" "<html><head><title>FastCGI Hello!</title></head>\n" "<body><h1>FastCGI Hello!</h1>\n" "Request number %d running on host <i>%s</i></body></html>\n", ++count, getenv("SERVER_NAME")); return 0; } |
保存为hello.cpp,然后编译:
1 |
gcc -o hello.cgi hello.cpp -lfcgi |
五、Spawn-fcgi执行FCGI程序
1 2 |
[root@localhost ~]# spawn-fcgi -p 3000 -f /data/web/fcgi/hello.cgi spawn-fcgi: child spawned successfully: PID: 2956 |
监听3000端口,执行刚刚编译的FGCI程序。可以看到执行完进程ID是2956。如果要结束进程,用kill命令即可。
六、配置Nginx
1 |
vim /etc/nginx/conf.d/default.conf |
修改配置为:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
server { listen 80; server_name localhost; #charset koi8-r; #access_log /var/log/nginx/log/host.access.log main; #location / { # root /usr/share/nginx/html; # index index.html index.htm; #} #Broly: FCGI Config location / { root /data/web/fcgi; index index.cgi; } location ~ \.cgi$ { fastcgi_pass 127.0.0.1:3000; fastcgi_index index.cgi; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/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:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } |
保存并退出。然后重启Nginx:
1 |
systemctl restart nginx.service |
七、浏览FCGI页面
成功!
参考文档:《How to setup a FastCGI development environment in CentOS 6》