Hack Your Design!

Apache & Perl でhello world する(CentOS 6)

「え? 今更パール?」って感じですが動かす機会があったのでメモっとく。

前提

  • CentOS 6.3 (さくらVPS)
  • apache + mod_perl
  • 目標はURIアクセスして「hello world」を出力するまで

apache

まずはapacheから。

# yum install httpd.x86_64
# service httpd start
Starting httpd:                                            [  OK  ]

index.html を設置。

# cd /var/www/html/
# ll
total 0
# vim index.html

適当にこんな感じで。

<html>
<body>
hello world!
</body>
</html>

HTTPアクセスします。

hello world の画面が出てくればOK. 次はapache にperlの設定をしていきます。

mod_perl

perlモジュールインストール。

# yum install mod_perl.x86_64

次はapacheの設定

# vim /etc/httpd/conf/httpd.conf

AddHandler追加。

AddHandler cgi-script .cgi .pl

DirecctoryにExecCGIのオプション追加。

Options Indexes FollowSymLinks +ExecCGI

リスタート。

# service httpd restart
Stopping httpd:                                            [  OK  ]
Starting httpd:                                            [  OK  ]
# vim index.pl

perlを書く。

#!/usr/bin/perl

print "Content-type:text/html\n\n";
print 'hello world';

どうやらポイントは

  • シェバンをつけること。
  • HTTPレスポンスとして返すのでHTTPレスポンスヘッダ,Content-typeを出力すること。

のようです。

再アクセス。「Internal Server Error」。エラーログはこう。

(13)Permission denied: exec of '/var/www/html/index.pl' failed

所有者をapacheにして実行権限を持たせてやる。

# chown apache:apache index.pl
# chmod 744 index.pl

再アクセス。これにてperlで、hello world が確認できました。めでたしめでたし。

  • このエントリーをはてなブックマークに追加