CentOS7配置Nginx+Spawn-fcgi+fcgi(新)

注:全局使用超级用户root进行配置

1.准备

nginx-1.12.2.tar.gz(来源:wget http://nginx.org/download/nginx-1.12.2.tar.gz)

spawn-fcgi-1.6.4.tar.gz(来源:wget http://download.lighttpd.net/spawn-fcgi/releases-1.6.x/spawn-fcgi-1.6.4.tar.gz)

fcgi-2.4.0.tar.gz(来源:wget http://down1.chinaunix.net/distfiles/fcgi-2.4.0.tar.gz)

tar -xzvf 压缩包  #进行解压

2.安装Nginx

(1)执行:yum -y install pcre-devel  #HTTP rewrite module requires the PCRE library

(2)执行:yum -y install zlib-devel  #HTTP gzip module requires the zlib library

(3)执行:yum -y install openssl-devel  #SSL modules require the OpenSSL library

(4)执行:./configure --with-http_ssl_module

运行结果:

Configuration summary
+ using system PCRE library
+ using system OpenSSL library
+ using system zlib library

nginx path prefix: "/usr/local/nginx"
nginx binary file: "/usr/local/nginx/sbin/nginx"
nginx modules path: "/usr/local/nginx/modules"
nginx configuration prefix: "/usr/local/nginx/conf"
nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
nginx pid file: "/usr/local/nginx/logs/nginx.pid"
nginx error log file: "/usr/local/nginx/logs/error.log"
nginx http access log file: "/usr/local/nginx/logs/access.log"
nginx http client request body temporary files: "client_body_temp"
nginx http proxy temporary files: "proxy_temp"
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"

(5)执行:make && make install  #编译及安装

(6)查看安装位置:whereis nginx

结果:

nginx: /usr/local/nginx

3.spawn-fcgi安装

(1)执行:./configure

(2)执行:make && make install

(3)查看版本:spawn-fcgi -v

 结果:

spawn-fcgi v1.6.4 - spawns FastCGI processes

4.fcgi库安装

(1)执行:./configure

(2)修改include/fcgio.h文件,加入#include <stdio.h>  #防止出现编译错误:error: 'EOF' was not declared in this scope

(3)执行:make && make install

5.fcgi编程

(1)样例代码

#include <fcgi_stdio.h>

int main(int argc, char *argv[])
{
        while( FCGI_Accept() >=0 )
        {
                FCGI_printf("Content-Type: text/html\r\n\r\n");
                FCGI_printf("Hello world!");
        }

        return 0;
}

(2)编译:g++ -o test.fcgi test.cpp -L/usr/local/lib -lfcgi

(3)测试运行:./test.fcgi

如果出现类似错误:

./test.fcgi: error while loading shared libraries: libfcgi.so.0: cannot open shared object file: No such file or directory

 则进行如下处理:

将路径/usr/local/lib添加到文件/etc/ld.so.conf的末尾

文件打开命令可以使用:visudo  #vi /etc/ld.so.conf  //或者用vi命令

变更后,形如:

include ld.so.conf.d/*.conf
/usr/local/lib

 运行ldconfig命令马上生效

(参考:https://www.cnblogs.com/chris-cp/p/3591306.html

 5.启动fcgi应用程序

(1)将fcgi程序统一存储至一个目录,例如:cp test.fcgi /usr/local/nginx/fcgi-bin

(2)启动程序:spawn-fcgi -a 127.0.0.1 -p 9001 -F 10 -f /usr/local/nginx/fcgi-bin/test.fcgi

 6.启动nginx

(1)打开配置文件:vi conf/nginx.conf

(2)将原配置:

        #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;
        #}

  改为:

        location = /test.fcgi {
        #    root           html;
            fastcgi_pass   127.0.0.1:9001;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            include        fastcgi_params;
        }

(3)启动:./nginx  #./nginx -s reload //重新加载

7.Http访问

 8.服务器外部访问

打开服务器外部访问端口,重启防火墙:

firewall-cmd --add-port=80/tcp --permanent
firewall-cmd --reload

  

posted @ 2018-01-18 14:04  凤凰涅磐欲重生  阅读(714)  评论(0编辑  收藏  举报