Nginx : CGI スクリプトを利用する2022/05/26 |
|
Nginx で CGI が実行可能なように設定します。
|
|
| [1] | FastCGI Wrap をインストールして Nginx に設定します。 |
|
[root@www ~]#
dnf -y install fcgiwrap
[root@www ~]#
vi /etc/nginx/fcgiwrap.conf # 新規作成 # 例として [/cgi-bin] 配下は CGI を有効にする
location /cgi-bin/ {
gzip off;
root /usr/share/nginx;
fastcgi_pass unix:/var/run/fcgiwrap.socket;
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
[root@www ~]#
vi /etc/nginx/conf.d/ssl.conf # 設定をしたいサイト定義の [server] セクション内に追記
server {
.....
.....
include fcgiwrap.conf;
}
[root@www ~]# systemctl restart nginx |
| [2] | FastCGI Wrap サービス用の Systemd ファイルを作成して起動します。 |
|
[root@www ~]#
vi /usr/lib/systemd/system/fcgiwrap.service # 新規作成
[Unit]
Description=Simple CGI Server
After=nss-user-lookup.target
Requires=fcgiwrap.socket
[Service]
EnvironmentFile=/etc/sysconfig/fcgiwrap
ExecStart=/usr/sbin/fcgiwrap ${DAEMON_OPTS} -c ${DAEMON_PROCS}
User=nginx
Group=nginx
[Install]
Also=fcgiwrap.socket
[root@www ~]#
vi /usr/lib/systemd/system/fcgiwrap.socket # 新規作成 [Unit] Description=fcgiwrap Socket [Socket] ListenStream=/run/fcgiwrap.socket [Install] WantedBy=sockets.target systemctl enable --now fcgiwrap
|
| [3] | SELinux を有効にしている場合は、ポリシーの変更が必要です。 |
|
[root@www ~]#
vi nginx-server.te # 以下の内容で新規作成
module nginx-server 1.0;
require {
type unconfined_service_t;
type var_run_t;
type httpd_t;
class sock_file write;
class unix_stream_socket connectto;
}
#============= httpd_t ==============
allow httpd_t unconfined_service_t:unix_stream_socket connectto;
allow httpd_t var_run_t:sock_file write;
checkmodule -m -M -o nginx-server.mod nginx-server.te [root@www ~]# semodule_package --outfile nginx-server.pp --module nginx-server.mod [root@www ~]# semodule -i nginx-server.pp |
| [4] | 設定したディレクトリ (当例では [/usr/share/nginx/cgi-bin]) 配下で、任意の言語で CGI テストページ (下例は Python3 スクリプト) を作成して動作確認をします。正常にページが表示されれば OK です。 |
|
[root@www ~]#
vi /usr/share/nginx/cgi-bin/index.cgi
#!/usr/bin/python3
print("Content-type: text/html\n")
print("<html>\n<body>")
print("<div style=\"width: 100%; font-size: 40px; font-weight: bold; text-align: center;\">")
print("CGI Script Test Page")
print("</div>")
print("</body>\n</html>")
chmod 755 /usr/share/nginx/cgi-bin/index.cgi |
|
| Sponsored Link |
|
|