LoginSignup
1
0

More than 5 years have passed since last update.

ネットワーク関連の備忘録

Last updated at Posted at 2019-01-24

Apache Serverで簡単なCGIプログラムを書いた時の備忘録

TL;DR

初めてネットワーク関連のことで遊んだので、復習がてらまとめていこうという備忘録。今まであまり意識したことがなかったファイルのパーミッション系統を意識できるようになって良かった。あと、Pythonのeval関数がすごく便利。

よく使ったコマンドたち

  1. cd: 言わずもがな
  2. ls -al:今までパーミッションあまり気にしなかったのでls -aでよかったんだけど、ls -alの大事さがわかった。
  3. vim:ターミナル内で完結するエディタは便利。gitにvimrc上げとけばSublime text3とかJupyter notebookとか使えないときに使えるので、vimrc最低限は書いてgitに上げておこうと思った。
  4. chmod:パーミッション関連。とりあえずCGI実行したければ755っておばあちゃんが言ってた。777にするとCGI動かない罠にはまっていた。
  5. curl:お手軽にGETとかPOSTとかできるのがすごい。でもこれについては奥が深すぎていまいちわかってない。
  6. which:シバン行書くときに大事ってことがよくわかった。
  7. netstat:ポートの状況を知りたいときに非常に便利だった。

Apache Server関連

#install
yum -y install httpd

#Serverの開始
sudo service httpd start

#自動起動設定
sudo chkconfig httpd on

#再起動
sudo apachectl restart 

ファイル構成など

詳しい人たちは当然知っているんでしょうが、そもそもindex.htmlどこにあるの?ってところから詰まったので・・・

#index.htmlとか
/var/www/html/

#cgi置き場
/var/www/cgi-bin/

#error_log
/var/log/httpd/error_log/

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

BASIC認証

セキリティ的にかなり問題があるらしいが、一番簡単らしいので。

#アカウントを作成
htpasswd -c -b /etc/httpd/conf/.htpasswd <user> <password>
vim /etc/httpd/conf/httpd.conf

httpd.confの末尾に以下を追加


<Directory "/var/www/html/">
      AuthUserFile /etc/httpd/conf/.htpasswd
      AuthGroupFile /dev/null
      AuthName "Basic Auth"
      AuthType Basic
      Require valid-user
</Directory>

PythonをCGIで動かすための設定

SElinuxの無効化

getenforce
#Disabled

であることを確認。
状態は以下のように表される。

表示 状態
Disabled 無効化されている
Enforcing 有効
Permissive 有効だがアクセス制限は行わず警告

httpd.confファイルを設定する

sudo vim /etc/httpd/conf/httpd.conf

コメントアウトされてる行の有効化

#コメントアウトを外す
LoadModule cgid_module modules/mod_cgid.so

.pyがCGIとして認識されるようにする

 576 #下の三行を足す
 577 <IfModule alias_module>
 578 ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
 579 </IfModule>
 580
 581 #
 582 # "/var/www/cgi-bin" should be changed to whatever your ScriptAliased
 583 # CGI directory exists, if you have that configured.
 584 #
 585 <Directory "/var/www/cgi-bin">
 586     AllowOverride None
 587     Options ExecCGI #ExecCGIにする
 588     Order allow,deny
 589     Allow from all
 590 </Directory>
 591 AddHandler cgi-scprit .cgi .py .pyc #この行に.py .pycを足す

実行ファイルを作る(Hello World)

sudo vim /var/www/cgi-bin/test.py

とりあえずHello Worldさせてみる。シバン行は環境ごとに変える。pyenvとか使ってると色々変わるらしい。

which python3
#!/usr/bin/python3
#!/usr/bin/python3 #シバン行
# coding:utf-8
print("Content-Type: text/html\n") #おまじない、意味がわかってないので今度調べる
print("Hello World")

実行

sudo chmod 755 test.py #権限を付与
curl http://IP:port/cgi-bin/test.py
#Hello World

実行ファイルを作る(keyを受け取る)

sudo vim /var/www/cgi-bin/test1.py
#!/usr/bin/python3 #シバン行
# coding:utf-8
import cgi

print("Content-Type: text/html\n")

form = cgi.FieldStorage()
key=form.getvalue('key', 'default') #keyを受け取る。keyなしならdefaultを返す。

print(key)

実行

sudo chmod 755 test1.py
curl http://IP:port/cgi-bin/test.py?key=aaa
#aaa

あとがき

随時増やすかもしれない。Permisson denyに苦しまされた勉強でした。

1
0
2

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