实战:Nginx + FastCGI 程序(C/C++) 搭建高性能web server

1.介绍

    Nginx - 高性能web server,这个不用多说了,大家都知道。

    FastCGI程序 - 常驻型CGI程序,它是语言无关的、可伸缩架构的CGI开放扩展,其主要行为是将CGI解释器进程保持在内存中并因此获得较高的性能。

    Nginx要调用FastCGI程序,需要用到FastCGI进程管理程序(因为nginx不能直接执行外部的cgi程序,我们可使用lighttpd中的spawn-fastcgi来让nginx可支持外部cgi运行。也有其他方法安装nginx-fcgi来让nginx支持cgi,这里是使用spawn-fastcgi的方法),来达到调用FastCGI程序的目的。Nginx本身没有集成类似的模块,而Apache具备该功能模块,所以不需要额外安装FastCGI进程管理程序。

  因此,nginx 通过 spawn-fastcgi 调用 fastcgi

2.工作原理

    Nginx不支持对外部程序的直接调用或者解析,所有的外部程序(包括PHP)必须通过FastCGI接口来调用。FastCGI接口在Linux下是socket(这个socket可以是文件socket,也可以是ip socket)。为了调用CGI程序,还需要一个FastCGI wrapper(wrapper可以理解为用于启动另一个程序的程序),这个wrapper绑定在某个固定socket上,如端口或者文件socket。

    当Nginx将CGI请求发送给这个socket的时候,通过FastCGI接口,wrapper接收到请求,然后派生出一个新的线程,这个线程调用解释器或者外部程序处理脚本并读取返回数据;接着,wrapper再将返回的数据通过FastCGI接口,沿着固定的socket传递给Nginx;最后,Nginx将返回的数据发送给客户端。这就是Nginx+FastCGI的整个运作过程,如图1所示。

             

FastCGI接口方式在脚本解析服务器(CGI应用程序服务器)上启动一个或者多个守护进程对动态脚本进行解析,这些进程就是FastCGI进程管理器,或者称为FastCGI引擎。 spawn-fcgi与PHP-FPM都是FastCGI进程管理器(支持PHP和C/C++​)。​

 

3.环境部署 :  需要安装nginx、spawn-fastcgi、fastcgi

a)nginx的安装,参考本博客nginx模块安装教程

b) spawn-fastcgi的安装

    spawn_fastcgi  https://github.com/lighttpd/spawn-fcgi 

    这里使用的是1.6.3的版本 https://github.com/lighttpd/spawn-fcgi/releases/tag/v1.6.3

    下载以后解压并安装(请记得看README)

    如果没有configure,请先执行./autogen.sh,生成configure

    ./configure

    make

    编译好以后,将可执行文件移动到nginx的sbin目录下

    cp ./src/spawn-fcgi /usr/local/nginx/sbin/ (cp到nginx的安装目录下)

c)  fastcgi库的安装

   访问https://github.com/FastCGI-Archives/fcgi2地址下载fastcgi库源码

  ./configure    如果没有configure,请先执行./autogen.sh

   make

   make install

 

4.Demo和web发布

4.1.Demo程序

#include <fcgi_stdio.h>  
#include <unistd.h>
#include <stdlib.h>  
  
int main() {  
    int count = 0;  
    while (FCGI_Accept() >= 0) {  
        printf("Content-type: text/html\r\n"  
                "\r\n"  
                ""  
                "FastCGI Hello!"  
                "Request number %d running on host%s "  
                "Process ID: %d\n", ++count, getenv("SERVER_NAME"), getpid());  
    }  
    return 0;  
}  

 [编译]   g++ demo.cc -o demo  -lfcgi​  

 

报错:缺少库libfcgi.so.0 

解决办法:把so的库路径添加到/etc/ld.so.conf,并执行ldconfig更新一下

参考:https://www.cnblogs.com/zhming26/p/6164131.html

4.2.Web发布

    1)将CGI可执行程序移动到nginx的安装目录下 /usr/local/nginx/cgibin (文件夹不存在则自己创建) 

    2)启动spawn-fcgi管理进程,并绑定server IP和端口(不要跟nginx的监听端口重合)

     /usr/local/nginx/sbin/spawn-fcgi -a 127.0.0.1 -p 8088 -f /usr/local/nginx/cgibin/demo

    查看一下9002端口是否已成功:netstat -na | grep 8088

    3)更改nginx.conf配置文件,让nginx转发请求

    在http节点的子节点- server 节点中下添加配置

    location ~ \.cgi$ {
        fastcgi_pass 127.0.0.1:8088;
        fastcgi_index index.cgi;
        fastcgi_param SCRIPT_FILENAME fcgi$fastcgi_script_name;

        include fastcgi_params;

    }

 

加入了一行配置   fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

这个配置的意思是 在浏览器中访问的.php文件,实际读取的是 $document_root(网站根目录)下的.php文件 -- 也就是说当访问127.0.0.1/index.php的时候,需要读取网站根目录下面的index.php文件,如果没有配置这一配置项时,nginx不回去网站根目录下访问.php文件,所以返回空白

配置项目中:include fastcgi_params; fastcgi_params 文件中含有各个nginx常量的定义,默认情况 SCRIPT_FILENAME = $fastcgi_script_name

 

  注意: 如果使用gedit编辑器,注意要用tab键缩进,空白键会报错。

  

 

4)重启nginx或者重新加载配置文件

5)打开浏览器访问一下吧

       http://localhost/demo.cgi

6)效果

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我知道你的问题了。针对你的问题,我可以给你一些简单的部署步骤: 1.安装nginx和PHP 在CentOS 6.8中安装nginx和PHP的方式如下: ``` sudo yum install nginx sudo yum install php-fpm ``` 2.配置nginx 在配置nginx之前,你需要备份一下默认的nginx配置文件。备份命令如下: ``` sudo cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak ``` 然后,你可以通过编辑/etc/nginx/nginx.conf文件来配置nginx。 下面是一个简单的nginx配置示例,可以将它添加到/etc/nginx/nginx.conf文件中: ``` user nginx; worker_processes auto; error_log /var/log/nginx/error.log; pid /run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; keepalive_timeout 65; include /etc/nginx/conf.d/*.conf; server { listen 80; server_name localhost; root /usr/share/nginx/html; index index.php index.html index.htm; location / { try_files $uri $uri/ /index.php; } location ~ \.php$ { try_files $uri =404; fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } } ``` 在这个示例配置中,我们将nginx的根目录设置为/usr/share/nginx/html,并且将index.php添加到默认文档列表中。同时,我们还配置了一个location块,用于处理PHP文件。 3.配置PHP 在配置PHP之前,你需要备份一下默认的php-fpm配置文件。备份命令如下: ``` sudo cp /etc/php-fpm.d/www.conf /etc/php-fpm.d/www.conf.bak ``` 然后,你可以通过编辑/etc/php-fpm.d/www.conf文件来配置PHP。 下面是一个简单的PHP配置示例,可以将它添加到/etc/php-fpm.d/www.conf文件中: ``` [www] user = nginx group = nginx listen = /var/run/php-fpm/php-fpm.sock listen.owner = nginx listen.group = nginx pm = dynamic pm.max_children = 50 pm.start_servers = 5 pm.min_spare_servers = 5 pm.max_spare_servers = 35 ``` 在这个示例配置中,我们将PHP-FPM的用户和组设置为nginx,并将PHP-FPM的监听套接字设置为/var/run/php-fpm/php-fpm.sock。 4.重启服务 完成配置后,你需要重启nginx和php-fpm服务,以使配置生效。重启命令如下: ``` sudo systemctl restart nginx sudo systemctl restart php-fpm ``` 到这里,就完成了nginx+PHP的简单部署。同时,为了进一步提升性能,你可以使用Nginx+PHP+FastCGI加速模式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值