LoginSignup
2
0

More than 3 years have passed since last update.

NGINXのCGI有効化設定(RHEL7以降編)

Last updated at Posted at 2020-11-29

はじめに

GNU GLOBALで作成したWebページの検索窓はCGIを使用するため、
今回はNGINXを使用しCGIを使用できるように構築して行きます。

Webサーバ構築

NGINXのインストール

参考にしたサイトは以下になります。
How to install NGINX in Red Hat Enterprise Linux
https://access.redhat.com/solutions/1211673

以下のコマンドを実行しNGINXをインストールします。

dnf install -y nginx

以下のコマンドを実行しNGINXのステータスを確認します。

systemctl status nginx

実行結果

● nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
   Active: inactive (dead)

以下のコマンドを実行しNGINXを起動します。

systemctl start nginx

以下のコマンドを実行しステータスを確認します。
★印を確認するとステータスが"active (running)"になっていることが確認できます。

# systemctl status nginx

実行結果

● nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
   Active: active (running) since Sun 2020-11-29 22:57:15 JST; 5s ago ★
  Process: 1553 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
  Process: 1551 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
  Process: 1549 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
 Main PID: 1554 (nginx)
    Tasks: 3 (limit: 23529)
   Memory: 14.6M
   CGroup: /system.slice/nginx.service
           tq1554 nginx: master process /usr/sbin/nginx
           tq1555 nginx: worker process
           mq1556 nginx: worker process

NGINXのサービス自動起動設定確認

以下のコマンドを確認しNGINXが自動起動設定されているか確認します。

systemctl list-unit-files | grep nginx

実行結果

nginx.service                              disabled

自動起動設定されていないことが確認できます。

NGINXのサービス自動起動有効化

以下のコマンドを実行しNGINXの自動起動を有効化します。

systemctl enable nginx

実行結果

Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.

NGINXのサービス自動起動有効化確認

以下のコマンドを実行しNGINXの自動起動を有効化されたことを確認します。

systemctl list-unit-files | grep nginx

実行結果

nginx.service                              enabled

Webブラウザでテストページが表示されるか確認

以下のアドレスをWebブラウザに入力しテストページが表示されることを確認します。

http://サーバのIPアドレス/

トップページ一部抜粋

You should now put your content in a location of your choice
and edit the root configuration directive in the nginx configuration file /etc/nginx/nginx.conf.

上記の記述がありますので/etc/nginx/nginx.confが設定ファイルであると確認できます。

CGI有能化

NGINXのCGI有効化設定

GNU GLOBALで作成されたWebページの検索窓はCGIを使用しているのでNGINXのCGIを有効化します。
今回はfcgiwrapを使用しています。

参考にしたURLは以下になります。

FCGI Wrap
https://mogile.web.fc2.com/nginx_wiki/start/topics/examples/fcgiwrap/

arch linux wiki
https://wiki.archlinux.jp/index.php/Nginx#CGI_.E3.82.92.E5.8B.95.E3.81.8B.E3.81.99

赤帽エンジニアブログ
https://rheb.hatenablog.com/entry/2019/08/16/EPEL8%E3%81%8C%E3%83%AA%E3%83%AA%E3%83%BC%E3%82%B9%E3%81%95%E3%82%8C%E3%81%BE%E3%81%97%E3%81%9F%E3%80%82%E4%BD%BF%E3%81%84%E5%A7%8B%E3%82%81%E3%81%BE%E3%81%97%E3%82%87%E3%81%86%E3%80%82

第3章 SYSTEMD によるサービス管理
https://access.redhat.com/documentation/ja-jp/red_hat_enterprise_linux/8/html/configuring_basic_system_settings/managing-services-with-systemd_configuring-basic-system-settings

EPELリポジトリ有効化

fcgiwrapパッケージはEPELパッケージに含まれていますのでEPELリポジトリを有効化します。

以下のコマンドを実行しEPELリポジトリを追加します。

dnf install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm

fcgiwrapパッケージを検索

以下のコマンドを実行しfcgiwrapパッケージを検索します。

dnf search fcgiwrap

実行結果

Updating Subscription Management repositories.
メタデータの期限切れの最終確認: 0:16:32 時間前の 2020年11月29日 23時06分19秒 に実施しました。
========================================================== 名前 完全一致: fcgiwrap ==========================================================
fcgiwrap.x86_64 : Simple FastCGI wrapper for CGI scripts

fcgiwrapパッケージの提供リポジトリ確認

以下のコマンドを実行しfcgiwrapパッケージの提供先を確認します。

dnf list fcgiwrap

実行結果

Updating Subscription Management repositories.
メタデータの期限切れの最終確認: 0:16:20 時間前の 2020年11月29日 23時06分19秒 に実施しました。
利用可能なパッケージ
fcgiwrap.x86_64                                             1.1.0-12.20181108git99c942c.el8                                              epel

fcgiwrapパッケージインストール

以下のコマンドを実行しfcgiwrapパッケージをインストールします。

dnf install -y fcgiwrap

fcgiwrapパッケージインストール完了後、/usr/share/doc/fcgiwrap/SETUPを確認すると
以下の記述があるためNGINX用にunitファイルをNGINX用にカスタムunitファイルを作成します。

以下のコマンドを確認しfcgiwrapの関連ドキュメントの中身を確認します。

cat /usr/share/doc/fcgiwrap/SETUP

実行結果(一部抜粋)

2. The systemd unit files provided with this package are instantiated and must
   be started by specifiying the desired web server user account. For example,
   when using nginx, the fgciwrap service is enabled then started like so:

      systemctl enable fcgiwrap@nginx.socket
      systemctl start fcgiwrap@nginx.socket

fcgiwrapのunitファイルコピー

fcgiwrapのベースとなるunitファイルがあるため以下のコマンドを実行しunitファイルをコピーします。

cp -p /usr/lib/systemd/system/fcgiwrap@.service /etc/systemd/system/fcgiwrap.service
cp -p /usr/lib/systemd/system/fcgiwrap@.socket /etc/systemd/system/fcgiwrap.socket

以下のコマンドを実行しfcgiwrap.socketのUnitファイルを編集します。

vi /etc/systemd/system/fcgiwrap.socket

fcgiwrap.socket変更前

[Unit]
Description=fcgiwrap Socket

[Socket]
ListenStream=/run/fcgiwrap/fcgiwrap-%i.sock
RuntimeDirectory=fcgiwrap
SocketUser=%i
SocketMode=0660

[Install]
WantedBy=sockets.target

★印の部分が変更箇所になります。

fcgiwrap.socket変更後

[Unit]
Description=fcgiwrap Socket

[Socket]
ListenStream=/run/fcgiwrap/fcgiwrap.sock ★名前変更
RuntimeDirectory=fcgiwrap
SocketUser=nginx ★名前変更
SocketMode=0660

[Install]
WantedBy=sockets.target

以下のコマンドを実行しfcgiwrap.serviceのUnitファイルを編集します。

vi /etc/systemd/system/fcgiwrap.service

fcgiwrap.service変更前

[Unit]
Description=Simple CGI Server
After=nss-user-lookup.target

[Service]
EnvironmentFile=/etc/sysconfig/fcgiwrap
ExecStart=/usr/sbin/fcgiwrap ${DAEMON_OPTS} -c ${DAEMON_PROCS}
User=%i

[Install]
Also=fcgiwrap@%i.socket

★印の部分が変更箇所になります。

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 ★名前変更

[Install]
Also=fcgiwrap.socket ★名前変更

fcgiwrapサービスの自動起動確認

以下のコマンドを実行しfcgiwrapの自動起動を確認します。

systemctl list-unit-files | grep fcgiwrap

fcgiwrap関連が自動起動設定されていないことが確認できます。

fcgiwrap.service                           indirect
fcgiwrap@.service                          indirect
fcgiwrap.socket                            disabled
fcgiwrap@.socket                           disabled

fcgiwrap関連のサービス自動起動有効化

以下のコマンドを実行しfcgiwrapの自動起動を有効化します。

systemctl enable fcgiwrap
Created symlink /etc/systemd/system/sockets.target.wants/fcgiwrap.socket → /etc/systemd/system/fcgiwrap.socket.

fcgiwrap関連のサービス自動起動有効化確認

以下のコマンドを実行しfcgiwrapの自動起動を有効化されたことを確認します。

systemctl list-unit-files | grep fcgiwrap

fcgiwrap.socketが有効化されたことを確認できます。
fcgiwrap.socketはfcgiwrap.service起動の前提条件のためfcgiwrap.socketのみ有効化で問題ないです。

fcgiwrap.service                           indirect
fcgiwrap@.service                          indirect
fcgiwrap.socket                            enabled
fcgiwrap@.socket                           disabled

systemdに設定反映

以下のコマンドを実行し新規に作成したUnitファイルをsystemdに読み込ませます。

systemctl daemon-reload

fcgiwrapサービス起動

以下のコマンドを実行しfcgiwrapサービスを起動します。

systemctl start fcgiwrap

fcgiwrapサービス起動確認

以下のコマンドを実行しfcgiwrapサービスを起動確認します。

systemctl status fcgiwrap

実行結果

● fcgiwrap.service - Simple CGI Server
   Loaded: loaded (/etc/systemd/system/fcgiwrap.service; indirect; vendor preset: disabled)
   Active: active (running) since Sun 2020-11-29 23:51:49 JST; 5s ago
 Main PID: 2097 (fcgiwrap)
    Tasks: 1 (limit: 23529)
   Memory: 440.0K
   CGroup: /system.slice/fcgiwrap.service
           mq2097 /usr/sbin/fcgiwrap -f -c 1

NGINXのCGI有効化設定

以下のコマンドを実行しnginx.confを編集します。

vi /etc/nginx/nginx.conf

nginx.conf変更前

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

nginx.conf変更後

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /usr/share/nginx/html;

----------  追記ここから  ----------
        location ~ \.cgi$ {
        fastcgi_pass   unix:/run/fcgiwrap/fcgiwrap.sock;
        include        /etc/nginx/fastcgi.conf;
        }
----------  追記ここまで  ----------

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

NGINXの構文チェック

以下のコマンドを実行しnginx.confの構文をチェックします。
ここで構文に問題があれば"nginx -t"で"syntax is ok"が表示されるまで
トラブルシューティングします。

nginx -t

実行結果

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

NGINX、fcgiwrapの設定反映

以下のコマンドを実行しNGINX、fcgiwrapそれぞれサービスを再起動および起動確認をします。

systemctl restart nginx
systemctl restart fcgiwrap
systemctl status nginx
systemctl status fcgiwrap

WebページのCGI有効化されたことの確認

以下のコマンドを実行しCGI実行テストページを作成します。

vi test.cgi

ファイルの中身は以下になります。

#!/usr/bin/perl
print "Content-type: text/html \n\n";
print "Hello World";

以下のコマンドを実行しテストページに読み取りおよび実行権限を付与します。

chmod 755 test.cgi

ブラウザから以下のアドレスにアクセスし"Hello World"と表示されたことを確認します。

http://サーバIP/test.cgi

まとめ

NGINXはApacheに比べてCGIを有効化するためにひと手間掛かることを初めて知りました。
今回の調査および実機確認は想定以上に時間掛かりましたが何とか設定出来て良かったと思います。
次回は、ソースパッケージを使用してGNU GLOBALでHTMLファイルを作成していきたいと思います。

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0