搭建nginx
安裝fastcgi
安裝fastcgi的進程管理器spawn-cgi
編寫運行程序
編譯運行
工欲善其事,必先利其器,首先搭建環境把!
通過閱讀不少的博客內容找到了最簡單的安裝步驟,好多都是通過下載源代碼,然后通過make進行編譯,不過對于這些比較常用的庫,軟件包中已經集成了。
C 開發環境安裝
apt-get install build-essential
nginx
apt-get install nginx
fastcgi
sudo apt-get install libfcgi-dev
spawn-fcgi
apt-get install spawn-fcgi
編寫運行程序
#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
啟動nginx
nginx -c /usr/local/nginx/conf/nginx.conf
通過瀏覽器驗證nginx是否正常啟動http://*******:80
啟動spwan-cgi進程
spawn-fcgi -a 127.0.0.1 -C 20 -p 7070 ./cgidemo
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對三五互聯的支持。
更多關于云服務器,域名注冊,虛擬主機的問題,請訪問三五互聯官網:m.shinetop.cn