2017年1月9日 星期一

在 CentOS7/RHEL7上,為 Nginx 加上 Perl CGI 模組

設定提要:
  • Nginx Web Server 以及 php 套件安裝,請參考這一篇
快速設定流程:
  1. 安裝 EPEL 套件:
    #yum install epel-release
    #yum upgrade epel-release
    
  2. 利用 YUM ,安裝 fcgiwrap 以及 spawn-fcgi:
    #yum install fcgiwrap spawn-fcgi
    
  3. 設定系統參數,方便 spawn-fcgi 運用:
    #vim /etc/sysconfig/spawn-fcgi
    FCGI_SOCKET=/var/run/fcgiwrap.socket
    FCGI_PROGRAM=/usr/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-fcgi.pid -- $FCGI_PROGRAM"
    
  4. 啟動 spawn-fcgi 服務:
    #systemctl enable spawn-fcgi
    #systemctl start spawn-fcgi
    
  5. 修改 Nginx 設定檔內容:
    #
    server {
    (中間其他設定omit)
    location /monitorix/ {
            alias /var/lib/monitorix/www/;
            index index.html;
        }
        location /monitorix-cgi/ {
            gzip off;
            alias /var/lib/monitorix/www/cgi/;
            fastcgi_pass unix:/var/run/fcgiwrap.socket;
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        }
    (中間其他設定omit)
    }
    
  6. 重新啟動 nginx 服務:
    #systemctl restart nginx
    
  7. 寫一測試檔案:
    #vim /var/share/nginx/html/test.cgi
    #!/usr/bin/perl -w
    print "Content-type: text/html\n\n";    
    print "<html><head><title>Hello World!! </title></head>\n";
    print "<body><h1>Hello world</h1>
    </body></html>\n";
    
  8. 設定權限:
    #chmod 0755 /var/share/nginx/html/test.cgi
    #chown nginx.nginx /var/share/nginx/html/test.cgi
    
  9. 利用 firefox 測試!(http://localhost/test.cgi)

參考文獻:
  • https://www.howtoforge.com/serving-cgi-scripts-with-nginx-on-centos-6.0-p2