LoginSignup
3
1

More than 3 years have passed since last update.

CGIプログラムをローカルで動かすときの設定等(自分用)

Last updated at Posted at 2020-06-01

はじめに

CGIプログラム完全初心者の自分用メモです.
こういう記事を書くのは初めてなので,お見苦しい点あるかと思いますがお許しください.
アドバイスやご指摘がありましたら,コメントくださると幸いです.

モチベーション

Rubyで記述されたCGIプログラムを,ローカルサーバ環境にて実行テストしたい.

サーバの決定

サーバには,ApacheのHTTPサーバを使う.

AN-HTTPDを使う方法(参考:http://www.aikis.or.jp/~s-suzuki/cgilabo/localserver/
もあるみたいだけど,入手先のURLが死んでる.
一応アーカイブからダウンロードできるよってことらしい
(参考:https://www.nishishi.com/blog/2019/02/an_httpd_waybac.html )けど,
試したけどなんかうまくいかなかった.

環境

  • Windows 10
  • Perl 5.28.1
  • Ruby 2.7.1-1
  • Apache 2.4.43

ActivePerlのインストール

今回のモチベには関係ないけどCGIプログラムにはPerlが定番っぽい(そりゃそうか)ので入れた.

ActivePerlのサイト(https://www.activestate.com/ )から最新版(5.28.1)をダウンロード

Sign In (ログイン必要/GitHubアカウントでログイン可)
-> Featured Projects & Languages
-> ActivePerl 5.28 の枠中の Windows
-> Windows10 の .msi をクリックして ActivePerl-5.28.1.0000-MSWin32-x64-b462fde1.msi
をダウンロードしてインストール

☆インストール中
Setup TypeはTypical
Setup Optionsでは,
「Add Perl to the PATH environment variable」(パス通す)と
「Create Perl file extension association」(plファイル関連付け)にはチェックを入れる.
(参考:https://www.hiskip.com/pg-notes/how-to-install/develop-kit/active-perl526.html

インストール後,C:の下にPerl64ってディレクトリが置かれる(C:\Perl64).

Rubyのインストール

RubyInstaller(https://rubyinstaller.org/downloads/ )から
最新版(2.7.1-1)をダウンロード

rubyinstaller-2.7.1-1-x64.exeをダウンロードしてインストール

☆インストール中
Perlの時と同じ理由で,
「Add Ruby exexutables to your PATH」と
「Associate .rb and .rbw files with this Ruby installation」にはチェックを入れる.

インストール後,C:の下にRuby27-x64ってディレクトリが置かれる(C:\Ruby27-x64).

ApacheのHTTPサーバのインストール

ApacheのHTTPサーバのダウンロードサイト(http://httpd.apache.org/download.cgi )から
最新版(2.4.43)をダウンロード

Apache HTTP Server Server 2.4.43 (httpd): 2.4.43 is the latest available version
-> Files for Microsoft Windows
-> Apache Lounge (ダウンロード先の指定,何でもいいと思う)
-> httpd-2.4.43-win64-VS16.zip
をダウンロード,展開

展開したら,httpd-2.4.43-win64-VS16のディレクトリの下にApache24っていうディレクトリが
できるのでC:の下に置く(C:/Apache24).

Apacheの httpd.conf の設定

Apache24/conf/http.conf は,Apache(httpd)の設定ファイルである.
これを編集する.
以下,編集点

#CGIの実行場所の設定
ScriptAlias /cgi-bin/ "C:/Apache24/cgi-bin/"

#CGIの実行を許可
<Directory "C:/Apache24/cgi-bin">
AllowOverride All
Options Indexes FollowSymLinks ExecCGI
Require all granted
</Directory>

#拡張子.cgi、.pl .rbが使えるようする
AddHandler cgi-script .cgi .pl .rb

#拡張子.cgi、.pl .rbが使えるようするver2
AddType application/x-httpd-cgi .cgi
AddType application/x-httpd-cgi .pl
AddType application/x-httpd-cgi .rb

(参考1:https://phpjavascriptroom.com/?t=php&p=cgi#google_vignette
(参考2:https://qiita.com/hirotoyoshidome/items/6d103e04dd07e90519d2
上2つともスペシャルサンクス

AddHandlerとAddTypeの違いとかについてはこちら(https://senooken.jp/post/2019/06/01/

Apacheの起動

Apache24/bin/httpd.exeを実行すると,ApacheのHTTPサーバを起動できる.

私はhttpd.exeを実行できなくて,(http://t12488mac.blogspot.com/2011/06/windowsapache.html )を参照した.
Apache24\binのディレクトリでhttpd.exe -tを叩いたら
httpd.confにダメな記述があって実行できないよ~」って教えてくれた.
直したら直った(あたりまえ)

CGIプログラムの置き場・記述

CGIプログラムは
Apache24/cgi-binの下に置く.

  • rubyのCGIプログラムには1行目に以下の記述
    #!/Ruby27-x64/bin/ruby
    または
    #!C:/Ruby27-x64/bin/ruby

  • perlのCGIプログラムには1行目に以下の記述
    #!/perl64/bin/perl
    または
    #!C:/Ruby27-x64/bin/ruby

ブラウザからアクセス

以下の3つを満たす状態にたどり着けたら,いよいよブラウザで表示できる.

  • Apacheのhttpd.confを適切に編集済の状態
  • ApacheのHTTPサーバを起動(httpd.exeを実行)した状態
  • Apache24/cgi-bin下に何らかのCGIプログラム(**.cgi)を置いた状態

ブラウザのURLバーに
http://127.0.0.1/cgi-bin/**.cgi
または
http://localhost/cgi-bin/**.cgi
と入力すると,自分が書いたページが,ブラウザ上に表示される.

これを以て,ローカルサーバ環境での動作完了とする.

ディレクトリ配置

本記事で紹介したものに関連するディレクトリ配置を以下に示す.

C:/
├ Perl64
│ ├ bin
│ │ ├ Perl.exe
│ : :

├ Ruby27-x64
│ ├ bin
│ │ ├Ruby.exe
│ : :

├ Apache24
│ ├ bin
│ │ ├ httpd.exe
│ │ :
│ │
│ ├ conf
│ │ ├ httpd.conf
│ │ :
│ │
│ ├cgi-bin
│ : └**.cgi (自作CGIファイル)
│ :
::

おわりに

手探りながらも,Rubyで記述されたCGIプログラムを,ローカルサーバ環境でテストできた.
疲れた.

3
1
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
3
1