2023-10-21

openSUSE: Nginx, fcgiwrap and Tcl CGI

Nginx 是非同步框架的網頁伺服器,在靜態檔案的效能上十分高效, 而且時常被用來作為反向代理、Http Cache、負載平衡器。

Install nginx (@openSUSE):

sudo zypper install nginx

如果要作為提供靜態檔案服務的 web server,openSUSE 的預設設定已經足夠, 下面只是我個人習慣更新 /etc/nginx/nginx.conf 加入下面的設定:

       server {
                listen 80;
                listen [::]:80;
                server_name localhost;
                
                #access_log /var/log/nginx/host.access.log main;
                
                location / {
                        root /srv/www/htdocs;
                        try_files $uri/ $uri =404;
                        index index.html index.htm;
                }

接下來使用自簽憑證設定 HTTPS。
首先建立 ssl.conf 設定檔:

[req]
prompt = no
default_md = sha256
default_bits = 2048
distinguished_name = dn
x509_extensions = v3_req

[dn]
C = TW
ST = Taiwan
L = Taipei
O = Orange Inc.
OU = IT Department
emailAddress = admin@example.com
CN = localhost

[v3_req]
subjectAltName = @alt_names

[alt_names]
DNS.1 = *.localhost
DNS.2 = localhost
IP.1 = 127.0.0.1

透過指令建立開發測試用途的自簽憑證:

openssl req -x509 -new -nodes -sha256 -utf8 -days 3650 \
-newkey rsa:2048 -keyout nginx.key -out nginx.crt -config ssl.conf

將 nginx.key 與 nginx.crt 複製到 /etc/nginx/ssl 目錄(需要使用 su 切換到 root 身份或者使用 sudo)。

更新 /etc/nginx/nginx.conf 加入下面的設定:

        server {
                listen 443 ssl;
                listen [::]:443 ssl;
                http2 on;
                server_name localhost;

                ssl_certificate /etc/nginx/ssl/nginx.crt;
                ssl_certificate_key /etc/nginx/ssl/nginx.key;

                ssl_protocols TLSv1.2 TLSv1.3;

        #       ssl_ciphers ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384;
        #       ssl_conf_command Ciphersuites TLS_CHACHA20_POLY1305_SHA256:TLS_AES_256_GCM_SHA384;
        #       ssl_prefer_server_ciphers on;

        #       ssl_ecdh_curve prime256v1;

        #       ssl_early_data on;

        #       ssl_session_cache shared:SSL:10m;
        #       ssl_session_timeout 10m;

                location / {
                        root /srv/www/htdocs;
                        index index.html index.htm;
                }
        }

        include vhosts.d/*.conf;

這樣就有一個支援 HTTPS 的 web server 可以用來測試。

如果要加入 HTTP/2 支援,更新 HTTPS 設定如下(NgINX 1.25.1 以上適用):

                listen 443 ssl;
                listen [::]:443 ssl;
                http2 on;;

啟動 Nginx:

sudo systemctl start nginx

如果要在重新開機後會自動啟動 nginx server,使用下列的指令:

sudo systemctl enable nginx

因為 Nginx 支援 FastCGI 但是不支援 CGI, 所以需要 fcgiwrap 將網頁請求透過 FastCGI 協定傳給 CGI 程式執行。 如果有執行 CGI 的需求才需要安裝 fcgiwrap。

Install fcgiwrap (@openSUSE):

sudo zypper install fcgiwrap fcgiwrap-nginx

fcgiwrap 安裝後需要啟動服務:

sudo service fcgiwrap start

如果要在重新開機後會自動啟動 fcgiwrap service,使用下列的指令:

sudo systemctl enable fcgiwrap

在 /etc/nginx 下加入 fcgiwrap.conf,檔案內容如下:

location /cgi-bin/ {
    gzip off;
    root /srv/www;
    fastcgi_pass unix:/var/run/fcgiwrap.sock;
    include /etc/nginx/fastcgi_params;
    fastcgi_param SCRIPT_FILENAME  $document_root$fastcgi_script_name;
}

在想要加入 CGI 支援的 server section 加入下面的設定:

        include fcgiwrap.conf;

重新啟動 Nginx:

sudo systemctl restart nginx

在 /srv/www/cgi-bin/ 撰寫 env.cgi 作為測試。

#!/usr/bin/tclsh
package require ncgi
package require html

::html::init
::ncgi::header

set title "Print Environment"
puts [::html::head $title]
puts [::html::bodyTag]
puts [::html::h1 $title]
puts [::html::tableFromArray env]
puts [::html::end]

需要將 env.cgi 的權限設為可執行。如果沒問題,就可以使用 Nginx 開發或者是執行 CGI 程式。


spawn-fcgi 用來啟動 FastCGI process。 spawn-fcgi 一開始是 Lighttpd 的一部份,不過現在已經獨立出來可以供其他 Web Server 使用。 當使用者撰寫了一個 FastCGI 服務,可以使用 spawn-fcgi 進行管理。

Install spawn-fcgi (@openSUSE):

sudo zypper install spawn-fcgi

接下來的設定是使用 spawn-fcgi 啟動我們撰寫的 FastCGI 服務。
這裡使用 tcl-fcgi (pure Tcl) 測試。 下面就是測試的程式 vclock.tcl,來自 tcl-fcgi 的 example(我將檔案放在 /srv/www/cgi-bin,需要將權限設為可執行):

#! /usr/bin/env tclsh
# vclock.tcl -- originally borrowed from Don Libes' cgi.tcl but rewritten
#


package require ncgi
package require textutil
package require Fcgi
package require Fcgi::helpers

namespace eval vclock {
    namespace path ::fcgi::helpers

    variable EXPECT_HOST    http://expect.sourceforge.net
    variable CGITCL         $EXPECT_HOST/cgi.tcl
    variable TEMPLATE [textutil::undent {
        <!doctype html>
        <html><head><title>Virtual Clock</title></head>
        <body>
        <h1>Virtual Clock - fcgi.tcl style</h1>
        <p>Virtual clock has been accessed <%& $counter %> times since
        startup.</p>
        <hr>
        <p>At the tone, the time will be <strong><%& $time %></strong></p>
        <% if {[dict get $query debug]} { %>
            <pre>     Query: <%& $query %>
            Failed: <%& $failed %></pre>
        <% } %>
        <hr>
        <h2>Set Clock Format</h2>
        <form method="post">
        Show:
        <% foreach name {day month day-of-month year} { %>
          <input type="checkbox" id="<%& $name %>" name="<%& $name %>"
                 <%& [dict get $query $name] ? {checked} : {} %>>
          <label for="<%& $name %>"><%& $name %></label>
        <% } %>
        <br>
        Time style:
        <% foreach value {12-hour 24-hour} { %>
          <input type="radio" id="<%& $value %>" name="type" value="<%& $value %>"
                 <%& [dict get $query type] eq $value ? {checked} : {} %>>
          <label for="<%& $value %>"><%& $value %></label>
        <% } %>
        <br>
        <input type="reset">
        <input type="submit">
        </form>
        <hr>
        See Don Libes' cgi.tcl and original vclock
        at the <a href="<%& $CGITCL %>"><%& $CGITCL %></a>
        </body>
        </html>
    }]
}


proc vclock::main {} {
    variable CGITCL
    variable TEMPLATE

    proc page {query failed counter time CGITCL} [tmpl_parser $TEMPLATE]

    set counter 0

    while {[FCGI_Accept] >= 0} {
        incr counter

        puts -nonewline "Content-Type: text/html\r\n\r\n"

        lassign [validate-params {
            day          boolean                   false
            day-of-month boolean                   false
            debug        boolean                   false
            month        boolean                   false
            type         {regexp ^(?:12|24)-hour$} 24-hour
            year         boolean                   false
        } [query-params {day day-of-month debug month type year}]] query failed

        set format [construct-format $query]
        set time [clock format [clock seconds] -format $format]

        puts [page $query $failed $counter $time $CGITCL]

        ncgi::reset
    } ;# while {[FCGI_Accept] >= 0}
}


proc vclock::construct-format query {
    if {[dict get $query type] eq {}} {
        return {%r %a %h %d '%y}
    }

    set format [expr {
        [dict get $query type] eq {12-hour} ? {%r} : {%T}
    }]

    foreach {name fragment} {
        day { %a}
        month { %h}
        day-of-month { %d}
        year { '%y}
    } {
        if {[dict get $query $name] ne {}} {
            append format $fragment
        }
    }

    return $format
}


# If this is the main script...
if {[info exists argv0] && ([file tail [info script]] eq [file tail $argv0])} {
    vclock::main
}

我們需要撰寫 spawn-fcgi 的 systemd service,在 /usr/lib/systemd/system 目錄下建立 spawnfcgi.service,內容如下:

[Unit]
Description=Spawn FCGI service
After=nss-user-lookup.target

[Service]
Type=forking
Environment=WORKERS=1
ExecStart=/usr/bin/spawn-fcgi \
    -F ${WORKERS} \
    -u nginx \
    -g nginx \
    -s /var/run/%p.sock \
    -P /var/run/%p.pid \
    -- /srv/www/cgi-bin/vclock.tcl
Restart=on-failure
RestartSec=5

[Install]
WantedBy=multi-user.target

在 /etc/nginx 目錄下加入 spawnfcgi.conf,內容如下:

location /vclock/ {
    gzip off;
    fastcgi_pass unix:/var/run/spawnfcgi.sock;
    include /etc/nginx/fastcgi_params;
}

在想要加入 spawn-fcgi 支援的 server section 加入下面的設定:

        include spawnfcgi.conf;

啟動 spawn-fcgi:

sudo systemctl start spawnfcgi

重新啟動 Nginx:

sudo systemctl restart nginx

使用瀏覽器瀏覽 http://localhost/vclock/,檢查結果是否正確。


我們可以注意到,上面的方案只能夠指定執行某一個 script,我們需要一個能夠解讀 script 並且執行的一般性方案。 下面是測試我自己寫的工具 rivet-fcgi 的設定。

首先需要修改 /usr/lib/systemd/system 目錄下的 spawnfcgi.servic。

For unix socket -

[Unit]
Description=Spawn FCGI service
After=nss-user-lookup.target

[Service]
Type=forking
Environment=WORKERS=3
ExecStart=/usr/bin/spawn-fcgi \
    -F ${WORKERS} \
    -u wwwrun \
    -g www \
    -s /var/run/%p.sock \
    -P /var/run/%p.pid \
    -- /usr/bin/rivet-fcgi
Restart=on-failure
RestartSec=5

[Install]
WantedBy=multi-user.target

For Tcp socket -

[Unit]
Description=Spawn FCGI service
After=nss-user-lookup.target

[Service]
Type=forking
Environment=WORKERS=3
ExecStart=/usr/bin/spawn-fcgi \
    -F ${WORKERS} \
    -u wwwrun \
    -g www \
    -a 127.0.0.1 -p 9000 \
    -P /var/run/%p.pid \
    -- /usr/bin/rivet-fcgi
Restart=on-failure
RestartSec=5

[Install]
WantedBy=multi-user.target

需要修改 /etc/nginx 目錄下的 spawnfcgi.conf。

For unix socket -

location ~ \.(rvt|tcl)$ {
    gzip off;
    root /srv/www/htdocs;
    try_files $uri/ $uri =404;
    fastcgi_pass unix:/var/run/spawnfcgi.sock;
    include /etc/nginx/fastcgi_params;
    fastcgi_param SCRIPT_FILENAME  $document_root$fastcgi_script_name;
}

For Tcp socket -

location ~ \.(rvt|tcl)$ {
    gzip off;
    root /srv/www/htdocs;
    try_files $uri/ $uri =404;
    fastcgi_pass 127.0.0.1:9000;
    include /etc/nginx/fastcgi_params;
    fastcgi_param SCRIPT_FILENAME  $document_root$fastcgi_script_name;
}

啟動 spawn-fcgi 與 NGINX,接下來進行測試。 如果成功,當副檔名名為 .rvt 或者是 .tcl,就會透過 FastCGI 協定傳送到 rivet-fcgi 執行。

PHP-FPM 也是類似的修改手法,PHP-FPM 需要設定好自己的設定 (FPP-FPM 有實作自己的 daemon,所以不需要使用 spawn-fcgi,只需要設定好相關的設定), 然後再設定 NGINX FastCGI 相關的部份即可。

沒有留言: