本人使用的是ubuntu系统
1、安装nginx服务器
http://nginx.org/download/nginx-1.9.2.tar.gztar -zvxf nginx-1.9.2.tar.gzcd nginx./configuremake && make install
默认安装路径:/usr/local/nginx
nginx简单配置:/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 $body_bytes_sent $http_referer ' '$http_user_agent $http_x_forwarded_for'; access_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; //修改成自己的ip地址跟端口 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 page /50x.html # error_page 500 502 503 504 /50x.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 to .htaccess files, if apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} }}
启动nginx:/usr/local/nginx/sbin/nginx 关闭nginx:kill `ps aux|grep nginx|sed -n '1p'| awk '{print $2}'`
2、安装spawn-fcgi
apt-get install apawn-fcgi
3、安装fast-cgiwget http://www.fastcgi.com/dist/fcgi.tar.gztar -xvzf fcgi.tar.gz
此时不能编译安装cd fcgi-2.4.1/include打开fcgio.h 添加#include ./configuremake && make install
复制 编译好的spawn-fcgi到 nginx目录
cp./src/spawn-fcgi/usr/local/nginx/sbin/cp/usr/local/lib/libfcgi.so.0 /usr/lib
每个人安装的目录未必一致,请自行搜索按安装的目录进行 拷贝修改
4、编写cgi程序
#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;}
编译:
g++ -o test.cgi test.cpp -l /usr/local/lib/ -lfcgi
将test.cgi 拷贝到/usr/local/nginx/html/ 下启动spqwn-fcgi程序
spawn-fcgi -a 127.0.0.1 -p 7000 -u nobody -f ./test.cgi
在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; }
打开浏览器输入:http://192.168.88.134:8080/test.cgi
打印出:hello world in c
文章参考:
http://wiki.ubuntu.org.cn/nginx
http://blog.chinaunix.net/uid-30030431-id-4669581.html
http://terry831010.blog.163.com/blog/static/6916117120126185428827/
版权声明:本文为博主原创文章,未经博主允许不得转载。
以上就介绍了nginx服务器下编写cgi程序,包括了方面的内容,希望对php教程有兴趣的朋友有所帮助。
