2017-03-15 9 views
0

私はPythonスクリプトを使用してCGIを実装しようとしています。 127.0.0.1/helloを開くと、ファイルは実行されずにダウンロードされます。内容は次のとおりPython CGI executable

#!/home/musigma/anaconda3/bin python3 

私のpythonファイルにシェバング行として、次のように私は、次を追加しました私は

/home/musigma/anaconda3/bin 

にインストールのpythonを持ってsites-available

<VirtualHost *:80> 
    ServerAdmin [email protected] 
    DocumentRoot /var/www 
    <Directory /> 
     Options FollowSymLinks 
     AllowOverride None 
     AddHandler cgi-script .py 
    </Directory> 

    <Directory /var/www/> 
     Options ExecCGI Indexes FollowSymLinks MultiViews 
     AllowOverride None 
     Order allow,deny 
     allow from all 
     AddHandler cgi-script .py 
    </Directory> 

    ScriptAlias /cgi-bin/ /usr-lib/cgi-bin/ 
    <Directory "/usr-lib/cgi-bin/"> 
     AllowOverride None 
     Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch 
     Order allow,deny 
     Allow from all 
    </Directory> 

    ErrorLog ${APACHE_LOG_DIR}/error.log 
    CustomLog ${APACHE_LOG_DIR}/access.log combined 
</VirtualHost> 

の私000-default.confファイルです私のpythonファイルの

#!/home/musigma/anaconda3/bin python3 
print("Content-type:text/html\r\n\r\n") 
print("<html>") 
print("<head><title>cgi script </title></head>") 
print("<body>") 
print('<p>I think it worked</p>') 
for i in range(5): 
    print("<h1>hello world</h1>") 
print("</body>") 
print("</head>") 

ファイルを/var/wwwに置きました。私はまた、

sudo chmod 755 hello.py 

私のpythonファイルを実行可能にするために以下の行を走っしかし、それはまだ私のPythonスクリプト実行可能にしていません。実行する代わりに、ファイルがダウンロードされます。私は物事を今働かせる方法がわかりません。どんな方向にも大きな助けになるでしょう。私はこれらのリンクを試みたが、私の目的を果たさない。私はすでに私が次に何をすべきかわから最後の四から五時間ではないので、これを理解しようとしているこれらの事の

Python CGI executable script downloads/shows script code

を世話をしてきました。どんな方向性も素晴らしいでしょう。

PS:私は、あなたが/コピーした場合hello.cgihello.pyにファイルをシンボリックリンクは何のUbuntuに16.04

apacheのバージョン

Server version: Apache/2.4.18 (Ubuntu) 
Server built: 2016-07-14T12:32:26 

答えて

0

を使用していますか? 127.0.0.1/hello.cgi127.0.0.1/hello.pyは、ソースコードまたは実行結果を生成しますか?

#!/home/musigma/anaconda3/bin python3 

へ:

からスクリプトの最初の行を変更してみてください、私が思っただろう、また

#!/home/musigma/anaconda3/bin/python3 

スクリプトへのパスは127.0.0.1/hello.pyました(拡張子を含む)をデフォルトで使用します。

+0

あなたは(素人用語で、おそらくより多くのように)もう少し説明することができますか? –

1

ディレクトリ/usr-lib/cgi-bin/のみが実行可能CGIスクリプトを許可するように設定されています。それは次のとおりです。

Options +ExecCGI 

が示すとおりです。

すでに#!行が指摘されているように修正してから、スクリプトをディレクトリ/usr-lib/cgi-bin/に移動してください。

最後に、/cgi-bin/hello.pyのURLパスを使用します。実行可能なCGIスクリプトのためのApacheの設定の詳細については

、でApacheのマニュアルを参照してください。