0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Rocky Linux にapacheを入れてcgiを動かす

Last updated at Posted at 2022-05-09

またRockey Linuxのいじってみつつ久しぶりにcgiを使ってみる

開発環境

macOS Monterey 12.2.1
VirtualBox 6.1
Vagrant 2.2.19
Rocky Linux 8.5
Apache 2.4.37

Rocky Linux 起動

Vagrantfile

Vagrant.configure("2") do |config|
  config.vm.box = "generic/rocky8"
  config.vm.network "public_network", ip: "192.168.0.17"
end

vagrant up
vagrant ssh

apache install

sudo yum install httpd
sudo systemctl enable httpd
sudo systemctl start httpd

ブラウザで閲覧できるようにfirewalldを止める

systemctl status firewalld
sudo systemctl stop firewalld
systemctl status firewalld
sudo systemctl disable firewalld

sudo reboot

日本語サポート

sudo localectl set-locale LANG=ja_JP.UTF-8

mod_cgiを読み込む

sudo vi /etc/httpd/conf/httpd.conf
# LoadModule cgi_module modules/mod_cgi.so 追加

sudo systemctl restart httpd

test cgi用意

cat > /var/www/html/form.html <<EOF
<html>
<head>
<title>フォームサンプル</title>
</head>
<body>
<form method="post" action="../cgi-bin/form.cgi">
  <p>
    メッセージ<br>
    <input type="text" name="message" size="20" value="">
  </p>
  <p><input type="submit" value="送信する"></p>
</form>
</body>
</html>
EOF
cat > /var/www/cgi-bin/form.cgi <<EOF
#!/bin/perl

#送信されたデータを受け取る
if ($ENV{'REQUEST_METHOD'} eq 'POST') {
  read(STDIN, $alldata, $ENV{'CONTENT_LENGTH'});
} else {
  $alldata = $ENV{'QUERY_STRING'};
}
foreach $data (split(/&/, $alldata)) {
  ($key, $value) = split(/=/, $data);

  $value =~ s/\+/ /g;
  $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack('C', hex($1))/eg;
  $value =~ s/\t//g;

  $in{"$key"} = $value;
}

print "Content-Type: text/html;\n\n";
print "<html>\n";
print "<head><title>フォームサンプル</title></head>\n";
print "<body>\n";

#受け取ったデータを表示する
print "<p>入力されたメッセージは$in{'message'}です。</p>\n";

print "</body>\n";
print "</html>\n";

exit;
EOF
chmod 755 /var/www/cgi-bin/form.cgi

スクリーンショット 2022-05-09 22.53.18.png

スクリーンショット 2022-05-09 22.53.54.png

ちゃんと動きました

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?