0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

OCIのOracle LinuxにいれたWebサーバでプログラム(cgi)を動かす(nginx + cgi[perl])

0
Posted at

前の記事

OCIのOracle LinuxでWebサーバ(nginx)を動かす(HTTPまで)

cgi

Webサーバで変化するコンテンツを作るには、cgi(Common Gateway Interface)という仕組みが必要です。
20260601-cgi.PNG
※ChromeやEdgeなどのWebブラウザ上で変化するコンテンツはJava Scriptとかほかの仕組みが必要。

nginx-mod-http-perl

nginxでcgiを動かすにはnginx-mod-http-perlが必要

1.nginx-mod-http-perl

1.1.nginx-mod-http-perlのインストール前チェック

yum でインストール済みかチェック

yum listコマンドを使う。
nginxがインストールされているか?も併せて確認。

[opc@test-server ~]$ yum list installed | grep nginx
nginx.x86_64                              2:1.20.1-24.0.1.el9_7.3             @ol9_appstream
nginx-core.x86_64                         2:1.20.1-24.0.1.el9_7.3             @ol9_appstream
nginx-filesystem.noarch                   2:1.20.1-24.0.1.el9_7.3             @ol9_appstream
[opc@test-server ~]$ yum list installed | grep nginx-mod-http-perl
[opc@test-server ~]$

nginxはインストール済み。
fcgiwrap(cgiで検索)はインストールされていない。

yum でパッケージが対象になっているかチェック

yum listコマンドを使う。
nginxパッケージが対象か?も併せて確認。

[opc@test-server ~]$ yum list nginx
Last metadata expiration check: 0:06:30 ago on 99 XXX 2026 99:99:99 AM JST.
Installed Packages
nginx.x86_64    2:1.20.1-24.0.1.el9_7.3                           @ol9_appstream
Available Packages
nginx.x86_64    2:1.20.1-28.0.1.el9_8.2                           ol9_appstream
nginx.src       2:1.26.3-9.0.1.module+el9.8.0+90931+68e8e4c5.1    ol9_appstream
[opc@test-server ~]$ yum list nginx-mod-http-perl
Last metadata expiration check: 1:46:50 ago on 99 XXX 2026 99:99:99 AM JST.
Available Packages
nginx-mod-http-perl.x86_64         2:1.20.1-28.0.1.el9_8.2         ol9_appstream

nginxはインストールパッケージの対象。
nginx-mod-http-perlもインストールパッケージの対象。

1.2.nginx-mod-http-perlをインストール

yum installでのインストール

nginx-mod-http-perlインストール

yum installでnginx-mod-http-perlインストール

[opc@test-server ~]$ sudo yum install -y nginx-mod-http-perl
Last metadata expiration check: 0:02:41 ago on XXX 99 Jun 2026 99:99:99 AM JST.
Dependencies resolved.
================================================================================
 Package               Arch     Version                   Repository       Size
================================================================================
Installing:
 nginx-mod-http-perl   x86_64   2:1.20.1-28.0.1.el9_8.2   ol9_appstream    37 k

Transaction Summary
================================================================================
Install  1 Package

Total download size: 37 k
Installed size: 67 k
Downloading Packages:
nginx-mod-http-perl-1.20.1-28.0.1.el9_8.2.x86_6 165 kB/s |  37 kB     00:00
--------------------------------------------------------------------------------
Total                                           162 kB/s |  37 kB     00:00
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                        1/1
  Installing       : nginx-mod-http-perl-2:1.20.1-28.0.1.el9_8.2.x86_64     1/1
  Running scriptlet: nginx-mod-http-perl-2:1.20.1-28.0.1.el9_8.2.x86_64     1/1
  Verifying        : nginx-mod-http-perl-2:1.20.1-28.0.1.el9_8.2.x86_64     1/1

Installed:
  nginx-mod-http-perl-2:1.20.1-28.0.1.el9_8.2.x86_64

Complete!
[opc@test-server ~]$

1.3.moduleの設定

moduleが動作するように設定できているかの確認

実行ファイル確認

[opc@test-server yum.repos.d]$ cat /etc/nginx/nginx.conf
# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}
(以下略)

include /usr/share/nginx/modules/*.conf;があることを確認

mod-http-perl設定ファイルがあることを確認

[opc@test-server ~]$ ll /usr/share/nginx/modules/*
-rw-r--r--. 1 root root 64 May 30 00:44 /usr/share/nginx/modules/mod-http-perl.conf

mod-http-perl.confがあればOK

mod-http-perl設定ファイルの中身を確認

[opc@test-server ~]$ cat /usr/share/nginx/modules/mod-http-perl.conf
load_module "/usr/lib64/nginx/modules/ngx_http_perl_module.so";

load_module設定でgx_http_perl_module.soがあればOK

2.nginx設定

2.1.mod-http-perl設定変更前の動作確認

nginxの設定ファイル確認

変更前

[opc@test-server ~]$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

syntax is ok、test is successfulになって問題なし。

2.2.mod-http-perl動作のための設定

Perlスクリプト(.pmファイル)配置ディレクトリ設定追加

Perlスクリプト(.pmファイル)配置ディレクトリ設定追加

変更前

[opc@test-server ~]$ cat /etc/nginx/nginx.conf
# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 4096;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

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

変更:include /etc/nginx/conf.d/*.conf;と、server {の間に、

    # Perlスクリプト(.pmファイル)を配置するディレクトリを指定
    perl_modules /etc/nginx/perl;

    # 読み込むPerlスクリプト(修正するときはnginxの再起動が必要)
    perl_require MyWebScript.pm;    

を入れる。

変更後

[opc@test-server ~]$ sudo vi /etc/nginx/nginx.conf
(ここまで略)
    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    # Perlスクリプト(.pmファイル)を配置するディレクトリを指定
    perl_modules /etc/nginx/perl;

    # 読み込むPerlスクリプト(修正するときはnginxの再起動が必要)
    perl_require MyWebScript.pm;
    
    server {
        listen       80;
        listen       [::]:80;
        server_name  _;
        root         /usr/share/nginx/html;
(以下略)

変更後確認

[opc@test-server ~]$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
Perlスクリプト(.pmファイル)location設定追加

Perlスクリプト(.pmファイル)配置ディレクトリ設定追加

変更前

[opc@test-server ~]$ cat /etc/nginx/nginx.conf
(略)
    server {
        listen       80;
        listen       [::]:80;
        server_name  _;
        root         /usr/share/nginx/html;

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

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

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

変更:serverのlocationの後ろに、スクリプト呼び出しのlocation設定を追加

    # Perlスクリプト(.pmファイル)
    location /run-perl {
        perl MyWebScript::handler;
    }

を入れる。

変更後

[opc@test-server ~]$ sudo vi /etc/nginx/nginx.conf
(ここまで略)
    server {
        listen       80;
        listen       [::]:80;
        server_name  _;
        root         /usr/share/nginx/html;

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

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

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

        # Perlスクリプト(.pmファイル)
        location /run-perl {
            perl MyWebScript::handler;
        }
    }
(以下略)

「MyWebScript」は変更できます。ただし、以降の「MyWebScript」はすべて同じに合わせる必要があります。

変更後確認

[opc@test-server ~]$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

2.3.perlファイルの設定

perlファイル用ディレクトリ作成
[opc@test-server ~]$ sudo mkdir /etc/nginx/perl
[opc@test-server ~]$ sudo chown -R nginx:nginx /etc/nginx/perl
[opc@test-server ~]$ sudo chmod 755 /etc/nginx/perl
perlファイル作成

/var/www/perl/MyWebScript.pmを作成する。

[opc@test-server ~]$ sudo vi /etc/nginx/perl/MyWebScript.pm
package MyWebScript;
use nginx;

sub handler {
    my $r = shift;
    $r->send_http_header("text/html");
    $r->print("Hello from external Perl script!");
    return OK;
}
1;
__END__

nginxの再起動

[opc@test-server www]$ sudo systemctl stop nginx
[opc@test-server www]$ sudo systemctl start nginx

2.3.curlコマンドで動作確認

curlコマンドで動作確認 ```shell-session [opc@test-server ~]$ curl http://localhost/run-perl Hello from external Perl script! ```

最後に

記事の内容はCC BY-SA 4.0(著作者の情報とCCライセンス継承はお願いします。商用利用・改変・再配布は問題なし)です。

0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?