搭建nginx
安裝fastcgi
安裝fastcgi的進(jìn)程管理器spawn-cgi
編寫運(yùn)行程序
編譯運(yùn)行
工欲善其事,必先利其器,首先搭建環(huán)境把!
通過閱讀不少的博客內(nèi)容找到了最簡(jiǎn)單的安裝步驟,好多都是通過下載源代碼,然后通過make進(jìn)行編譯,不過對(duì)于這些比較常用的庫,軟件包中已經(jīng)集成了。
C 開發(fā)環(huán)境安裝
apt-get install build-essential
nginx
apt-get install nginx
fastcgi
sudo apt-get install libfcgi-dev
spawn-fcgi
apt-get install spawn-fcgi
編寫運(yùn)行程序
#include <iostream>
#include fcgio.h
using namespace std;
int main(void) {
// Backup the stdio streambufs
streambuf * cin_streambuf = cin.rdbuf();
streambuf * cout_streambuf = cout.rdbuf();
streambuf * cerr_streambuf = cerr.rdbuf();
FCGX_Request request;
FCGX_Init();
FCGX_InitRequest(&request, 0, 0);
while (FCGX_Accept_r(&request) == 0) {
fcgi_streambuf cin_fcgi_streambuf(request.in);
fcgi_streambuf cout_fcgi_streambuf(request.out);
fcgi_streambuf cerr_fcgi_streambuf(request.err);
cin.rdbuf(&cin_fcgi_streambuf);
cout.rdbuf(&cout_fcgi_streambuf);
cerr.rdbuf(&cerr_fcgi_streambuf);
cout << Content-type: text/html\\\\r\\\\n
<< \\\\r\\\\n
<< <html>\\\\n
<< <head>\\\\n
<< <title>Hello, World!</title>\\\\n
<< </head>\\\\n
<< <body>\\\\n
<< <h2>Hello, World!</h2>\\\\n
<< </body>\\\\n
<< </html>\\\\n;
}
cin.rdbuf(cin_streambuf);
cout.rdbuf(cout_streambuf);
cerr.rdbuf(cerr_streambuf);
return 0;
編譯程序
g cgi.cpp -o cgidemo -lfcgi
修改nginx 配置文件
vi /usr/local/nginx/conf/nginx.conf
啟動(dòng)nginx
nginx -c /usr/local/nginx/conf/nginx.conf
通過瀏覽器驗(yàn)證nginx是否正常啟動(dòng)http://*******:80
啟動(dòng)spwan-cgi進(jìn)程
spawn-fcgi -a 127.0.0.1 -C 20 -p 7070 ./cgidemo
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對(duì)西部數(shù)碼的支持。
更多關(guān)于云服務(wù)器,域名注冊(cè),虛擬主機(jī)的問題,請(qǐng)?jiān)L問西部數(shù)碼官網(wǎng):m.ps-sw.cn