macOS Sierraでrubyのcgiを動かす際に躓いたこと

2019年8月24日


こちらのサイト様を参考にmacでrubyのcgiを動かそうとしたのですが、私のmacのバージョンではうまく行かなかったのでその解決法をまとめました。

ユーザーディレクトリの設定

以前のバージョンでは

<directory ="" users="" [user_name]="" sites="">
   AllowOverride All
   Options Indexes FollowSymLinks MultiViews +ExecCGI
   AddHandler cgi-script .cgi
   Order allow,deny
   Allow from all
</directory>

となっているコードをみていきます。
apachectl configtestで文法チェックを行うと以下のエラーがでます。

$ apachectl configtest
AH00526: Syntax error on line 3 of /private/etc/apache2/users/[user_name].conf:
Either all Options must start with + or -, or no Option may.

これはオプションをすべてに付けるか、すべてに付けないかのどちらかにしなさいということらしいので、以下のように修正しました。
[修正前]

Options Indexes FollowSymLinks MultiViews +ExecCGI

[修正後]

Options Indexes FollowSymLinks MultiViews ExecCGI

それから、

apache2.4系ではアクセス許可の書き方が違う

らしいのでこれも修正します。
[修正前]

<directory "/home/www">
   Order allow,deny
   Allow from all
</directory>

[修正後]

<directory "/home/www">
   Require all granted
</directory>

以上の修正をした[user_name].confが以下になります。

<directory="" users="" [user_name]="" sites="">
   AllowOverride All
   Options Indexes FollowSymLinks MultiViews ExecCGI
   AddHandler cgi-script .cgi
   Require all granted
</directory>

CGIファイルの1行目

長い時間躓いてしまったapacheのエラーがこれでした。
No such file Directory
これは1行目の指定がまちがっているようなので修正したところ、動くようになりました。
[修正前]

#!/usr/local/bin/ruby                                                             
print "Content-Type: text/html\n\n"
str = "Hello World"
print str
print "\n"

[修正後]

#!/usr/bin/ruby                                                             
print "Content-Type: text/html\n\n"
str = "Hello World"
print str
print "\n"

参考にした記事