Ubuntu 14.04
Sponsored Link

Perlスクリプトを利用する2014/04/30

 
CGI を有効にして Perl スクリプトが利用できるように設定します。
[1] Perl をインストールします。
root@www:~#
apt-get -y install perl
[2] CGI モジュールを有効にします。
root@www:~#
a2enmod cgi

Your MPM seems to be threaded. Selecting cgid instead of cgi.
Enabling module cgid.
To activate the new configuration, you need to run:
service apache2 restart
root@www:~#
/etc/init.d/apache2 restart

 * Restarting web server apache2
   ...done.
[3] CGI モジュールを有効にすると、デフォルトで「/usr/lib/cgi-bin」配下が CGI 実行許可されます。
よって、例えば「/usr/lib/cgi-bin/index.cgi」スクリプトを作成して配置することで、「http://(Apache2 サーバー)/cgi-bin/index.cgi」へアクセス可能となります。 なお、当設定は「/usr/lib/cgi-bin」配下のファイルを全て CGI と扱うため、CGI 以外のファイルは表示不可です。
# テストスクリプト作成

root@www:~#
cat > /usr/lib/cgi-bin/test_script <<'EOF'
#!/usr/bin/perl
print "Content-type: text/html\n\n";
print "Hello CGI\n";
EOF
root@www:~#
chmod 705 /usr/lib/cgi-bin/test_script
# 動作確認

root@www:~#
curl http://localhost/cgi-bin/test_script

Hello CGI
[4] 上記デフォルト以外のディレクトリで CGI の実行を許可する場合は以下のように設定します。
例として、「/var/www/html/cgi-enabled」配下で CGI の実行を許可します。なお、CGI として扱う拡張子を指定しているため、html 等も配置可能です。
root@www:~#
vi /etc/apache2/conf-available/cgi-enabled.conf
# 新規作成

# 拡張子 cgi および pl を CGI として扱う

<Directory "/var/www/html/cgi-enabled">
    Options +ExecCGI
    AddHandler cgi-script .cgi .pl
</Directory>

root@www:~#
mkdir /var/www/html/cgi-enabled

root@www:~#
a2enconf cgi-enabled

Enabling conf cgi-enabled.
To activate the new configuration, you need to run:
  service apache2 reload
[5] 設定したディレクトリで CGI テストページを作成して動作確認をします。以下のようなページが表示されれば OK です。
root@www:~#
vi /var/www/html/cgi-enabled/index.cgi
#!/usr/bin/perl

print "Content-type: text/html\n\n";
print "<html>\n<body>\n";
print "<div style=\"width: 100%; font-size: 40px; font-weight: bold; text-align: center;\">\n";
print "CGI Test Page";
print "\n</div>\n";
print "</body>\n</html>\n";

root@www:~#
chmod 705 /var/www/html/cgi-enabled/index.cgi

関連コンテンツ