Debian 11.2にCGI を有効にして Ruby スクリプトを利用する方法

環境
OSバージョンを確認します

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# cat /etc/debian_version
11.2
# cat /etc/debian_version 11.2
# cat /etc/debian_version
11.2

apache2バージョンを確認します

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# apache2 -v
Server version: Apache/2.4.53 (Debian)
Server built: 2022-03-14T16:28:35
# apache2 -v Server version: Apache/2.4.53 (Debian) Server built: 2022-03-14T16:28:35
# apache2 -v
Server version: Apache/2.4.53 (Debian)
Server built: 2022-03-14T16:28:35

Rubyスクリプトを利用する手順
1.Ruby をインストールします

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# apt -y install ruby
# apt -y install ruby
# apt -y install ruby

rubyバージョンを確認します

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# ruby --version
ruby 2.7.4p191 (2021-07-07 revision a21a3b7d23) [x86_64-linux-gnu]
# ruby --version ruby 2.7.4p191 (2021-07-07 revision a21a3b7d23) [x86_64-linux-gnu]
# ruby --version
ruby 2.7.4p191 (2021-07-07 revision a21a3b7d23) [x86_64-linux-gnu]

2.CGI モジュールを有効にします

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# a2enmod cgid
Enabling module cgid.
To activate the new configuration, you need to run:
systemctl restart apache2
# a2enmod cgid Enabling module cgid. To activate the new configuration, you need to run: systemctl restart apache2
# a2enmod cgid
Enabling module cgid.
To activate the new configuration, you need to run:
systemctl restart apache2

apache2を再起動します

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# systemctl restart apache2
# systemctl restart apache2
# systemctl restart apache2

3. テストスクリプトを作成します

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# cat > /usr/lib/cgi-bin/testscript <<'EOF'
#!/usr/bin/ruby
print "Content-type: text/html\n\n"
print "test cgi hello wolrd\n"
EOF
# cat > /usr/lib/cgi-bin/testscript <<'EOF' #!/usr/bin/ruby print "Content-type: text/html\n\n" print "test cgi hello wolrd\n" EOF
# cat > /usr/lib/cgi-bin/testscript <<'EOF'
#!/usr/bin/ruby
print "Content-type: text/html\n\n"
print "test cgi hello wolrd\n"
EOF

ファイルの権限を付与します
# chmod 705 /usr/lib/cgi-bin/testscript
動作確認

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# curl http://localhost/cgi-bin/testscript
test cgi hello wolrd
# curl http://localhost/cgi-bin/testscript test cgi hello wolrd
# curl http://localhost/cgi-bin/testscript
test cgi hello wolrd

4.デフォルト以外のディレクトリで CGI の実行を許可します。
ディレクトリを作成します

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# mkdir /var/www/html/rubytest
# mkdir /var/www/html/rubytest
# mkdir /var/www/html/rubytest

配置ファイルを設定します
# vi /etc/apache2/conf-available/rubyenabled.conf
以下の内容を編集します

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<Directory "/var/www/html/rubytest">
Options +ExecCGI
AddHandler cgi-script .cgi .rb
</Directory>
<Directory "/var/www/html/rubytest"> Options +ExecCGI AddHandler cgi-script .cgi .rb </Directory>
<Directory "/var/www/html/rubytest">
    Options +ExecCGI
    AddHandler cgi-script .cgi .rb
</Directory>

配置ファイルを有効にします

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# a2enconf rubyenabled
Enabling conf rubyenabled.
To activate the new configuration, you need to run:
systemctl reload apache2
# a2enconf rubyenabled Enabling conf rubyenabled. To activate the new configuration, you need to run: systemctl reload apache2
# a2enconf rubyenabled
Enabling conf rubyenabled.
To activate the new configuration, you need to run:
  systemctl reload apache2

apache2を再起動します

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# systemctl restart apache2
# systemctl restart apache2
# systemctl restart apache2

5.設定したディレクトリで CGI テストページを作成して動作確認をします
# vi /var/www/html/rubytest/index.rb
以下の内容を編集します

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#!/usr/bin/ruby
print "Content-type: text/html\n\n"
print "<html>\n<body>\n"
print "<div >\n"
print "Ruby study hellwo world"
print "\n</div>\n"
print "</body>\n</html>\n"
#!/usr/bin/ruby print "Content-type: text/html\n\n" print "<html>\n<body>\n" print "<div >\n" print "Ruby study hellwo world" print "\n</div>\n" print "</body>\n</html>\n"
#!/usr/bin/ruby

print "Content-type: text/html\n\n"
print "<html>\n<body>\n"
print "<div >\n"
print "Ruby study hellwo world"
print "\n</div>\n"
print "</body>\n</html>\n"

ファイルの権限を付与します

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# chmod 705 /var/www/html/rubytest/index.rb
# chmod 705 /var/www/html/rubytest/index.rb
# chmod 705 /var/www/html/rubytest/index.rb

6.動作確認

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
「http://192.168.71.144/rubytest/index.rb」にアクセスします。
「Ruby study hellwo world」が表示されます。
「http://192.168.71.144/rubytest/index.rb」にアクセスします。 「Ruby study hellwo world」が表示されます。
「http://192.168.71.144/rubytest/index.rb」にアクセスします。
「Ruby study hellwo world」が表示されます。

 

Debian 11

Posted by arkgame