搭建環境
注意發現碰釘可以看看相應程序內的README
安裝:nginx、spawn-fcgi、fastcgi、fcgiwrap
nginx
sudo apt-get install nginx-full
fastcgi
安裝fastcgi的時候報EOF錯誤可以在include/fcgio.h中包含頭文件cstdio
wget http://www.fastcgi.com/dist/fcgi.tar.gz
tar -zxvf fcgi.tar.gz
./configure
make
sudo make install
無法下載的話,可以去百度雲下載:
鏈接:https://pan.baidu.com/s/1nEzOkFC0-rfVMDy_BygLWg
提取碼:ty0e
spawn-fcgi
git clone https://github.com/lighttpd/spawn-fcgi.git
sudo apt-get install autoconf
autoreconf -i
./autogen.sh
./configure
make
sudo make install
fcgiwrap
git clone https://github.com/gnosek/fcgiwrap.git
autoreconf -i
./configure
make
sudo make install
配置環境
配置nginx
vim /etc/nginx/sites-enabled/default
整個文件改成這樣
server {
listen 80 ;
server_name nextbin.com;
location / {
root /home/zebin/nginx/htdoc/;
index index.html index.htm;
try_files $uri $uri/ =404;
}
location ~ ^/cgi-bin/.*$ {
#cgi path: /home/zebin/nginx/cgi-bin/
root /home/zebin/nginx/;
fastcgi_pass 127.0.0.1:9000;
#configure path: /etc/nginx/fastcgi.conf
#include fastcgi.conf;
include fastcgi_params;
}
}
配置hosts
vim /etc/hosts
追加
127.0.0.1 nextbin.com
配置庫文件鏈接
sudo ln -s /usr/local/lib/libfcgi.so.0.0.0 /usr/local/lib/libfcgi.so.0
sudo ln -s /usr/local/lib/libfcgi.so.0.0.0 /usr/lib/libfcgi.so.0
sudo mkdir /usr/lib64
sudo ln -s /usr/local/lib/libfcgi.so.0.0.0 /usr/lib64/libfcgi.so.0
測試cgi
編寫cgi程序編譯後將可執行程序放在相應目錄(如/home/zebin/nginx/cgi-bin/demo)
demo.cpp
#include "fcgi_stdio.h"
#include <stdlib.h>
int main(int argc, char* argv[]){
int count = 0;
while (FCGI_Accept() >= 0){
printf("Content-type: text/html\r\n"
"\r\n"
"<title>FastCGI Hello!</title>"
"<h1>FastCGI Hello!</h1>"
"Request number %d running on host <i>%s</i>\n",
++count, getenv("SERVER_NAME"));
}
return 0;
}
編譯:g++ demo.cpp -lfcgi -o demo
加載fcgi-application
spawn-fcgi -p 9000 -C 10 -f /usr/local/sbin/fcgiwrap
瀏覽器訪問 nextbin.com/cgi-bin/demo 都成功
=========小結=========
一些用得上的命令
ps -aux | grep cgi
nginx -t
ln --help
因爲用spawn-fcgi 啓動後不能喲 -u root 啓動FastCGI進程。所以修改了源代碼:
下載代碼
wget http://download.lighttpd.net/spawn-fcgi/releases-1.6.x/spawn-fcgi-1.6.3.tar.gz
解壓後進入目錄,在文件src/spawn-fcgi.c中註釋掉一段代碼:
/*
if (my_uid == 0) {
fprintf(stderr, "spawn-fcgi: I will not set uid to 0\n");
return -1;
}
*/
類似的還有兩處要註釋。
運行
./configure
然後編譯
make
修改鏈接:
rm /etc/alternatives/spawn-fcgi
ln -s /usr/src/spawn-fcgi-1.6.3/src/spawn-fcgi /etc/alternatives/spawn-fcgi
這樣就解決了困擾我的redmine在root下運行經常crash的問題。
運行:
./spawn-fcgi -a 127.0.0.1 -p 9000 -C 10 -u root -f /usr/share/nginx/tmp/fastcgi/
出錯的時候請加-n參數前臺顯示
如
./spawn-fcgi -a 127.0.0.1 -p 9000 -C 10 -u root -f /usr/share/nginx/tmp/fastcgi/ -n
提示:sh: exec: line 1: /usr/share/nginx/tmp/fastcgi/: Permission denied