跳转到帖子
登录关注  
墨香年少

linux下nginx+cgi的环境搭建

已推荐帖子

1.安装spawn-fcgi-1.6.5

wget https://download.lighttpd.net/spawn-fcgi/releases-1.6.x/spawn-fcgi-1.6.5.tar.xz
tar -xf spawn-fcgi-1.6.5.tar.xz 
cd spawn-fcgi-1.6.5
./configure 
make
make install

2.安装fastcgi++-2.1

wget http://download.savannah.nongnu.org/releases/fastcgipp/fastcgi++-2.1.tar.bz2
tar -xf fastcgi++-2.1.tar.bz2 
cd fastcgi++-2.1
./configure --disable-shared --enable-static
make && make install

3.安装boost_1_64_0

wget https://sourceforge.net/projects/boost/files/boost/1.64.0/boost_1_64_0.tar.gz
tar -zxvf boost_1_64_0.tar.gz
cd boost_1_64_0
./bootstrap.sh
./b2 install

编写一个程序测试boost

#include <string>
#include <iostream>
#include <boost/version.hpp>
#include <boost/config.hpp>

using namespace std;

int main()
{
    cout << BOOST_VERSION << endl;    //Boost版本号
    cout << BOOST_LIB_VERSION << endl;   //Boost版本号
    cout << BOOST_PLATFORM << endl;    //操作系统
    cout << BOOST_COMPILER << endl;    //编译器
    cout << BOOST_STDLIB << endl;         //标准库
}

编译

g++ test_boost.cpp  -o test_boost

成功会出现如下界面

test_boost.png

 

接下来,写一个cgi文件来测试cgi环境是不是生效

文件名称为main.cpp,代码如下:

#include <boost/date_time/posix_time/posix_time.hpp>
#include <fstream>
#include <fastcgi++/request.hpp>
#include <fastcgi++/manager.hpp>
void error_log(const char* msg)
{
   using namespace std;
   using namespace boost;
   static ofstream error;
   if(!error.is_open())
   {
      error.open("/tmp/errlog", ios_base::out | ios_base::app);
      error.imbue(locale(error.getloc(), new posix_time::time_facet()));
   }
   error << '[' << posix_time::second_clock::local_time() << "] " << msg << endl;
}
class HelloWorld: public Fastcgipp::Request<wchar_t>
{
   bool response()
   {
      out << "Content-Type: text/html; charset=utf-8\r\n\r\n";
      out << "<html><head><meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
      out << "<title>fastcgi++: Hello World in UTF-8</title></head><body>";
      out << "<h2>this is a test fastcgi demo</h2>";
	  out << "</body></html>";
      err << "Hello apache error log";
      return true;
   }
};
int main()
{
   try
   {
      Fastcgipp::Manager<HelloWorld> fcgi;
      fcgi.handler();
   }
   catch(std::exception& e)
   {
      error_log(e.what());
   }
}

使用如下命令:

g++ main.cpp -o main -I/server/cgi/include -L/server/cgi/lib  -lfastcgipp -lboost_system -lboost_thread -lpthread

生成了main文件后,配置nginx

nginx.png

使用浏览器查看cgi

cgi.png


目之所及,皆是回忆,心之所想,皆是过往

分享这篇帖子


链接帖子
分享到其他站点

/server/cgi/include目录下为boost和fastcgi++的include目录

/server/cgi/lib为boost和fastcgi的lib文件


目之所及,皆是回忆,心之所想,皆是过往

分享这篇帖子


链接帖子
分享到其他站点

使用g++命令,将c++文件生成执行文件后,如何部署上去

1. 生成执行文件

g++ test.cpp -o test -I/server/cgi/include -L/server/cgi/lib  -lfastcgipp -lboost_system -lboost_thread -lpthread

2. 复制文件到nginx的cgi-bin目录

cp ./test /usr/local/nginx/cgi-bin/

3. 找到原来的进程,杀死

netstat -lntp

比如,进程号是1234

kill -9 1234

这样原来的进程就被杀死了

4. 启动新的进程

spawn-fcgi -a 127.0.0.1 -p 7001 -f /usr/local/nginx/cgi-bin/test

a是address 地址

p是port 端口

f是文件

不过一般还会跟上一个大写的P参数来制定一个pid的位置,这样杀死已有进程的时候,就不需要去查找进程id了,直接杀死进程位置的文件就可以了

cgi_cmd.thumb.png.0227fe4a72d0cd8883aa936af67775c1.png


目之所及,皆是回忆,心之所想,皆是过往

分享这篇帖子


链接帖子
分享到其他站点

也可以加上如下命令使用c++11标准库

g++ test.cpp -o test -I/server/cgi/include -L/server/cgi/lib  -lfastcgipp -lboost_system -lboost_thread -lpthread -std=c++11

 


目之所及,皆是回忆,心之所想,皆是过往

分享这篇帖子


链接帖子
分享到其他站点

created_pid.thumb.png.35ee4c67022bce417b2d344bdf30a66a.png

这样就是通过大写的P参数创建pid文件来创建和结束CGI进程


目之所及,皆是回忆,心之所想,皆是过往

分享这篇帖子


链接帖子
分享到其他站点

获取一些信息的完整代码

/***************************************************************************
* Copyright (C) 2007 Eddie Carle [eddie@erctech.org]                       *
*                                                                          *
* This file is part of fastcgi++.                                          *
*                                                                          *
* fastcgi++ is free software: you can redistribute it and/or modify it     *
* under the terms of the GNU Lesser General Public License as  published   *
* by the Free Software Foundation, either version 3 of the License, or (at *
* your option) any later version.                                          *
*                                                                          *
* fastcgi++ is distributed in the hope that it will be useful, but WITHOUT *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or    *
* FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public     *
* License for more details.                                                *
*                                                                          *
* You should have received a copy of the GNU Lesser General Public License *
* along with fastcgi++.  If not, see <http://www.gnu.org/licenses/>.       *
****************************************************************************/

#include <fstream>
#include <boost/date_time/posix_time/posix_time.hpp>

#include <fastcgi++/request.hpp>
#include <fastcgi++/manager.hpp>

// I like to have an independent error log file to keep track of exceptions while debugging.
// You might want a different filename. I just picked this because everything has access there.
void error_log(const char* msg)
{
	using namespace std;
	using namespace boost;
	static ofstream error;
	if(!error.is_open())
	{
		error.open("/tmp/errlog", ios_base::out | ios_base::app);
		error.imbue(locale(error.getloc(), new posix_time::time_facet()));
	}

	error << '[' << posix_time::second_clock::local_time() << "] " << msg << endl;
}

// Let's make our request handling class. It must do the following:
// 1) Be derived from Fastcgipp::Request
// 2) Define the virtual response() member function from Fastcgipp::Request()

// First things first let's decide on what kind of character set we will use.
// Since we want to be able to echo all languages we will use unicode. The way this
// library handles unicode might be different than some are used to but it is done
// the way it is supposed to be. All internal characters are wide. In this case UTF-32.
// This way we don't have to mess around with variable size characters in our program.
// A string with 10 wchar_ts is ten characters long. Not up in the air as it is with UTF-8.
// Anyway, moving right along, the streams will code convert all the UTF-32 data to UTF-8
// before it is sent out to the client. This way we get the best of both worlds.
//
// So, whenever we are going to use UTF-8, our template parameter for Fastcgipp::Request<charT>
// should be wchar_t. Keep in mind that this suddendly makes
// everything wide character and utf compatible. Including HTTP header data (cookies, urls, yada-yada).

class Echo: public Fastcgipp::Request<wchar_t>
{
	bool response()
	{
		using namespace Fastcgipp;
		wchar_t cookieString[] = { '<', '"', 0x0440, 0x0443, 0x0441, 0x0441, 0x043a, 0x0438, 0x0439, '"', '>', ';', 0x0000 };

		// Let's make our header, note the charset=utf-8. Remember that HTTP headers
		// must be terminated with \r\n\r\n. NOT just \n\n.
		// Let's set a cookie just for fun too, in UTF-8.
		out << "Set-Cookie: echoCookie=" << encoding(URL) << cookieString << encoding(NONE) << "; path=/\n";
		out << "Content-Type: text/html; charset=utf-8\r\n\r\n";

		// Now it's all stuff you should be familiar with
		out << "<html><head><meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
		out << "<title>fastcgi++: Echo in UTF-8</title></head><body>";

		// This environment().data structure is defined in fastcgi++/http.hpp
		out << "<h1>Environment Parameters</h1>";
		out << "<p><b>FastCGI Version:</b> " << Protocol::version << "<br />";
		out << "<b>fastcgi++ Version:</b> " << version << "<br />";
		out << "<b>Hostname:</b> " << encoding(HTML) << environment().host << encoding(NONE) << "<br />";
		out << "<b>User Agent:</b> " << encoding(HTML) << environment().userAgent << encoding(NONE) << "<br />";
		out << "<b>Accepted Content Types:</b> " << encoding(HTML) << environment().acceptContentTypes << encoding(NONE) << "<br />";
		out << "<b>Accepted Languages:</b> " << encoding(HTML) << environment().acceptLanguages << encoding(NONE) << "<br />";
		out << "<b>Accepted Characters Sets:</b> " << encoding(HTML) << environment().acceptCharsets << encoding(NONE) << "<br />";
		out << "<b>Referer:</b> " << encoding(HTML) << environment().referer << encoding(NONE) << "<br />";
		out << "<b>Content Type:</b> " << encoding(HTML) << environment().contentType << encoding(NONE) << "<br />";
		out << "<b>Root:</b> " << encoding(HTML) << environment().root << encoding(NONE) << "<br />";
		out << "<b>Script Name:</b> " << encoding(HTML) << environment().scriptName << encoding(NONE) << "<br />";
		out << "<b>Request URI:</b> " << encoding(HTML) << environment().requestUri << encoding(NONE) << "<br />";
		out << "<b>Request Method:</b> " << encoding(HTML) << environment().requestMethod << encoding(NONE) << "<br />";
		out << "<b>Content Length:</b> " << encoding(HTML) << environment().contentLength << encoding(NONE) << "<br />";
		out << "<b>Keep Alive Time:</b> " << encoding(HTML) << environment().keepAlive << encoding(NONE) << "<br />";
		out << "<b>Server Address:</b> " << encoding(HTML) << environment().serverAddress << encoding(NONE) << "<br />";
		out << "<b>Server Port:</b> " << encoding(HTML) << environment().serverPort << encoding(NONE) << "<br />";
		out << "<b>Client Address:</b> " << encoding(HTML) << environment().remoteAddress << encoding(NONE) << "<br />";
		out << "<b>Client Port:</b> " << encoding(HTML) << environment().remotePort << encoding(NONE) << "<br />";
		out << "<b>If Modified Since:</b> " << encoding(HTML) << environment().ifModifiedSince << encoding(NONE) << "</p>";

		// Let's see the Path Info
		out << "<h1>Path Data</h1>";
		if(environment().pathInfo.size())
		{
			std::wstring preTab;
			for(Http::Environment<wchar_t>::PathInfo::const_iterator it=environment().pathInfo.begin(); it!=environment().pathInfo.end(); ++it)
			{
				out << preTab << encoding(HTML) << *it << encoding(NONE) << "<br />";
				preTab += L"&nbsp;&nbsp;&nbsp;";
			}
		}
		else
			out << "<p>No Path Info</p>";

		// Let's see the GET data
		out << "<h1>GET Data</h1>";
		if(environment().gets.size())
			for(Http::Environment<wchar_t>::Gets::const_iterator it=environment().gets.begin(); it!=environment().gets.end(); ++it)
				out << "<b>" << encoding(HTML) << it->first << encoding(NONE) << ":</b> " << encoding(HTML) << it->second << encoding(NONE) << "<br />";
		else
			out << "<p>No GET data</p>";
		
		// Let's see the cookie data
		out << "<h1>Cookie Data</h1>";
		if(environment().cookies.size())
			for(Http::Environment<wchar_t>::Cookies::const_iterator it=environment().cookies.begin(); it!=environment().cookies.end(); ++it)
				out << "<b>" << encoding(HTML) << it->first << encoding(NONE) << ":</b> " << encoding(HTML) << it->second << encoding(NONE) << "<br />";
		else
			out << "<p>No Cookie data</p>";

		//Fastcgipp::Http::Post is defined in fastcgi++/http.hpp
		out << "<h1>POST Data</h1>";
		if(environment().posts.size())
		{
			for(Http::Environment<wchar_t>::Posts::const_iterator it=environment().posts.begin(); it!=environment().posts.end(); ++it)
			{
				out << "<h2>" << encoding(HTML) << it->first << encoding(NONE) << "</h2>";
				if(it->second.type==Http::Post<wchar_t>::form)
				{
					out << "<p><b>Type:</b> form data<br />";
					out << "<b>Value:</b> " << encoding(HTML) << it->second.value << encoding(NONE) << "</p>";
				}
				
				else
				{
					out << "<p><b>Type:</b> file<br />";
					// When the post type is a file, we have some additional information
					out << "<b>Filename:</b> " << encoding(HTML) << it->second.filename << encoding(NONE) << "<br />";
					out << "<b>Content Type:</b> " << encoding(HTML) << it->second.contentType << encoding(NONE) << "<br />";
					out << "<b>Size:</b> " << it->second.size() << "<br />";
					out << "<b>Data:</b></p><pre>";
					// We will use dump to send the raw data directly to the client
					out.dump(it->second.data(), it->second.size());
					out << "</pre>";
				}
			}
		}
		else
			out << "<p>No POST data</p>";

		out << "</body></html>";

		// Always return true if you are done. This will let apache know we are done
		// and the manager will destroy the request and free it's resources.
		// Return false if you are not finished but want to relinquish control and
		// allow other requests to operate. You might do this after an SQL query
		// while waiting for a reply. Passing messages to requests through the
		// manager is possible but beyond the scope of this example.
		return true;
	}
};

// The main function is easy to set up
int main()
{
	try
	{
		// First we make a Fastcgipp::Manager object, with our request handling class
		// as a template parameter.
		Fastcgipp::Manager<Echo> fcgi;
		// Now just call the object handler function. It will sleep quietly when there
		// are no requests and efficiently manage them when there are many.
		fcgi.handler();
	}
	catch(std::exception& e)
	{
		error_log(e.what());
	}
}

 


目之所及,皆是回忆,心之所想,皆是过往

分享这篇帖子


链接帖子
分享到其他站点

获取post数据:

class Echo: public Fastcgipp::Request<wchar_t>
{
	bool response()
	{
		using namespace Fastcgipp;

		//设置输出内容格式和编码
		out << "Content-Type: application/json; charset=utf-8\r\n\r\n";

		if(environment().posts.size())
		{
			auto username_it = environment().posts.find(L"username");
			if(username_it == environment().posts.end())
			{
				out << "username not found in post data";
			}
			else
			{
				out << username_it->first.data() << ": " << username_it->second.value;
			}
		}
		else
		{
			out << "{\"name\":\"zhangsan\",\"cn\":\"张三\",\"age\":38}";
		}
		
		return true;
	}

};

 


目之所及,皆是回忆,心之所想,皆是过往

分享这篇帖子


链接帖子
分享到其他站点

python脚本

#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
import sys
import time
if len(sys.argv) == 2 :
    #cgi文件参数
    cgi_name = sys.argv[1]
    
    #编译c++文件
    compile_args = ['g++ ',cgi_name,'.cpp -o ',cgi_name,' -I/server/cgi/include -L/server/cgi/lib  -lfastcgipp -lboost_system -lboost_thread -lpthread -std=c++11']
    compile_cmd = ''
    os.system(compile_cmd.join(compile_args))
    time.sleep(0.01)
    
    #删除原来的cgi文件
    rm_args = ['rm -rf /usr/local/nginx/cgi-bin/',cgi_name]
    rm_cmd = ''
    os.system(rm_cmd.join(rm_args))
    time.sleep(0.01)
    
    #移动新的cgi文件过去
    cp_args = ['cp ./',cgi_name,' /usr/local/nginx/cgi-bin/']
    cp_cmd = ''
    os.system(cp_cmd.join(cp_args))
    time.sleep(0.01)

    #杀死进程
    kill_args = ['kill -9 `cat /server/cgi/test/',cgi_name,'.pid`']
    kill_cmd = ''
    os.system(kill_cmd.join(kill_args))
    time.sleep(0.01)

    #生成spawn
    spawn_args = ['spawn-fcgi -a 127.0.0.1 -p 7001 -f /usr/local/nginx/cgi-bin/',cgi_name,' -P /server/cgi/test/',cgi_name,'.pid']
    spawn_cmd = ''
    os.system(spawn_cmd.join(spawn_args))
    
    print '== Complete! =='
else :
    print 'Missing CGI file name'

 


目之所及,皆是回忆,心之所想,皆是过往

分享这篇帖子


链接帖子
分享到其他站点

cgi下如何输出std::string?

需要转换下,代码如下:

if(environment().posts.size())
{
	auto username_it = environment().posts.find(L"username");
	if(username_it == environment().posts.end())
	{
		out << "username not found in post data";
	}
	else
	{
		std::wstring _username (username_it->second.value.begin(),username_it->second.value.end());
		const wchar_t *pw_username = _username.c_str();
		out << username_it->first.data() << " is " << pw_username; 

		std::string category = "只是一个测试分类";
		std::wstring wstr(category.begin(),category.end());
		const wchar_t *pwstr = wstr.c_str();
		out << "category:" << pwstr; 
	}
}
else
{
	out << "{\"code\":\"0\",\"msg\":\"OK\"}";
}

 


目之所及,皆是回忆,心之所想,皆是过往

分享这篇帖子


链接帖子
分享到其他站点

创建帐户或登录来提出意见

你需要成为会员才能提出意见

创建帐户

注册成为会员。只要几个简单步骤!

注册帐户

登录

已有帐户? 请登录。

现在登录
登录关注  

×
×
  • 创建新的...

重要信息

注册必须使用2-8个中文汉字作为账号