======利用spawn-fcgi管理服務執行cgi程式======
spawn-fcgi是管理CGI程序的程式,它本來是[[wp>Lighttpd]](Web Server)其中一部分,現在它獨立出來可以供其他Web Server使用。
若要知道其他更多cgi差異可以看這篇[[http://cecilyen.blogspot.com/2011/11/cgifastcgiphp-cgiphp-fpmspawn-fcgi.html|什麼是CGI、FastCGI、PHP-CGI、PHP-FPM、Spawn-FCGI?]]
=====需要套件=====
本實作是搭配Nginx Web服務(Nginx是編譯安裝)。
* fcgi
* fcgi-devel
* fcgiwrap
* spawn-fcgi
=====安裝=====
* 安裝fcgi 、 fcgi-devel及spawn-fcgi#yum install fcgi fcgi-devel spawn-fcgi
* 安裝fcgiwrap#cd /usr/local/src/
#git clone git://github.com/gnosek/fcgiwrap.git
#cd fcgiwrap
#autoreconf -i
#./configure
#make && make install
=====spawn-fcgi設定檔配置=====
#vim /etc/sysconfig/spawn-fcgi
FCGI_SOCKET=/var/run/fcgiwrap.socket
FCGI_PROGRAM=/usr/local/sbin/fcgiwrap
FCGI_USER=nginx
FCGI_GROUP=nginx
FCGI_EXTRA_OPTIONS="-M 0700"
OPTIONS="-u $FCGI_USER -g $FCGI_GROUP -s $FCGI_SOCKET -S $FCGI_EXTRA_OPTIONS -F 1 -P /var/run/spawn-fcgi2.pid -- $FCGI_PROGRAM"
=====Nginx 設定檔配置=====
#vim /usr/local/nginx/conf/nginx.conf
server {
~略~
location /cgi-bin/ {
gzip off;
root /usr/local/nginx/html;
fastcgi_pass unix:/var/run/fcgiwrap.socket;
include fastcgi_params;
fastcgi_index index.cgi;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
~略~
}
#mkdir -p /usr/local/nginx/html/cgi-bin/
#cd /usr/local/nginx/html/cgi-bin/
#vim index.cgi
~內容~
#!/usr/bin/perl -w
# Tell perl to send a html header.
# So your browser gets the output
# rather then (command line
# on the server.)
print "Content-type: text/html\n\n";
# print your basic html tags.
# and the content of them.
print "Hello World!! \n";
print "Hello world
\n";
#chown -R nginx:nginx /usr/local/nginx/html/cgi-bin/
#chmod -R 755 /usr/local/nginx/html/cgi-bin/
=====啟動或重啟 Nginx及Spawn-fcgi 並測試cgi網頁=====
#/usr/local/nginx/sbin/nginx -t -->測試設定檔語法OK
#/usr/local/nginx/sbin/nginx -s reload
#/etc/init.d/spawn-fcgi start
#links http://your IP/cgi-bin/
======在cgi上使用其他程式======
以上步驟都焦點在Perl在CGI上,現在換一個php互動網頁程式來玩玩看。
安裝及配置與以上的步驟都大致相同。
=====安裝套件=====
與以上套件一樣。只另外加裝php相關套件
* php-common
* php-cli
* php
=====spawn-fcgi設定檔配置=====
#vim /etc/sysconfig/spawn-fcgi
SOCKET=/var/run/php-cgi.socket
OPTIONS="-u nginx -g nginx -s $SOCKET -S -M 0600 -C 32 -F 1 -P /var/run/spawn-fcgi.pid -- /usr/bin/php-cgi"
=====Nginx 設定檔配置=====
#vim /usr/local/nginx/conf/nginx.conf
server {
~略~
location ~ \.php$ {
root /usr/local/nginx/html;
fastcgi_pass unix:/var/run/php-cgi.socket;
include fastcgi_params;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
~略~
}
#cd /usr/local/nginx/html/
#vim index.php
#chown nginx:nginx index.php && chmod 755 index.php
之後,再仿照前一節步驟啟動或重啟 Nginx及Spawn-fcgi 並測試php網頁即可。
======參考資料======
- [[http://www.howtoforge.com/serving-cgi-scripts-with-nginx-on-centos-6.0-p2|Serving CGI Scripts With Nginx On CentOS 6.0]]
- [[http://nginx.localdomain.pl/wiki/FcgiWrap|Simple CGI support for Nginx (fcgiwrap)]]