CentOS7

Apache httpd : Python を利用する

Python スクリプトを CGI として利用できるよう設定します。

Python をインストールします。

[root@localhost ~]# yum -y install python

CGI の実行はデフォルトで「/var/www/cgi-bin/」配下で許可されています。 よって、例えば「/var/www/cgi-bin/index.py」スクリプトを作成して配置することで、「http://(httpd サーバー)/cgi-bin/index.py」へアクセス可能となります。 なお、当該設定は「/var/www/cgi-bin/」配下のファイルを全て CGI と扱うため、CGI 以外のファイルは表示不可です。

# 以下の設定により /var/www/cgi-bin/ 配下では CGI の実行が許可されている
[root@localhost ~]# grep -n "^ *ScriptAlias" /etc/httpd/conf/httpd.conf
247: ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"

上記デフォルト以外のディレクトリで CGI の実行を許可する場合は以下のように設定します。

例として、「/var/www/html/cgi-enabled」配下で CGI の実行を許可します。なお、CGI として扱う拡張子を指定しているため、html 等も配置可能です。

[root@localhost ~]# vi /etc/httpd/conf.d/cgi-enabled.conf
# 新規作成
# 拡張子 py を CGI として扱う

<Directory "/var/www/html/cgi-enabled">
Options +ExecCGI
AddHandler cgi-script .py
</Directory>
[root@localhost ~]# systemctl restart httpd

SELinux を有効にしている場合で、デフォルト以外の場所で CGI を許可する場合は、ポリシーの許可設定が必要です。

[root@localhost ~]# chcon -R -t httpd_sys_script_exec_t /var/www/html/cgi-enabled
[root@localhost ~]# semanage fcontext -a -t httpd_sys_script_exec_t /var/www/html/cgi-enabled

CGI を許可したディレクトリ配下に CGIテストページを作成して動作確認をします。任意のクライアントで Web ブラウザを起動し、以下のように作成したテストページにアクセスできれば OK です。

[root@localhost ~]# vi /var/www/html/cgi-enabled/index.py
#!/usr/bin/env python

print "Content-type: text/html\n\n"
print "<html>\n<body>"
print "<div style=\"width: 100%; font-size: 40px; font-weight: bold; text-align: center;\">"
print "Python Script Test Page"
print "</div>\n</body>\n</html>"
[root@localhost ~]# chmod 705 /var/www/html/cgi-enabled/index.py

01.png


添付ファイル: file01.png 224件 [詳細]

トップ   編集 凍結 差分 履歴 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2018-09-03 (月) 16:14:42