以前 Ubuntu ではやったけど、今度は CentOS で。
fcgiwrap のインストール
標準のリポジトリには無いようなので EPEL からインストール。
[takatoh@rollo ~]$ sudo dnf -y install epel-release
[takatoh@rollo ~]$ sudo dnf --enablerep=epel -y install fcgiwrap
Nginx の設定
CGI を利用したいサイトの設定ファイルの server セクションにつぎを追加。
location ~ \.cgi$ {
root /var/www/app;
fastcgi_pass unix:/var/run/fcgiwrap.socket;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
で、Nginx をリスタート。
[takatoh@rollo ~]$ sudo systemctl restart nginx
fcgiwrap 用の設定ファイルを作成して起動。
/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=root
[Install]
Also=fcgiwrap.socket
/usr/lib/systemd/system/fcgiwrap.socket
[Unit] Description=fcgiwrap socket [Socket] ListenStream=/run/fcgiwrap.socket [Install] WantedBy=sockets.target
起動。
[takatoh@rollo system]$ sudo systemctl enable --now fcgiwrap
SELinux
SELinux を有効にしているので、ポリシーを作る。
nginx-server.te
module nginx-server 1.0;
require {
type httpd_t;
type var_run_t;
class sock_file write;
}
#======== httpd_t ========
allow httpd_t var_run_t:sock_file write;
[takatoh@rollo ~]$ sudo checkmodule -m -M -o nginx-server.mod nginx-server.te [takatoh@rollo ~]$ sudo semodule_package --outfile nginx-server.pp --module nginx-server.mod [takatoh@rollo ~]$ sudo semodule -i nginx-server.pp
CGI のテスト
設定したディレクトリ(今回は /var/www/app)の中に、hello.cgi を作成。
#!/usr/bin/env ruby
print "Content-type: text/html\n\n"
print <<EOC
<html>
<body>
<h1>Hello</h1>
<p>Hello, from CGI!</p>
</body>
</html>
EOC
実行権限をつけるのを忘れずに。
今回はスマホから確認した。OKだった。
ここにある情報が大変参考になり、非常に助かりました。
ありがとうございました。
一つ誤記を見つけましたのでお伝えいたします。
/usr/lib/systemd/system/fcgiwrap.service の9行目
User=ngix ⇒ nginx
修正していただけますと、後の閲覧者が助かると思います。
お役に立てて何よりです。
誤記の報告もありがとうございます。修正してきました。