Clock Slave
Clock Slave

Reputation: 7977

Python CGI executable

I am trying to implement CGI using python scripts. Whenever I open 127.0.0.1/hello, the file gets downloaded instead of executing. Following is my 000-default.conf file in sites-available

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    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>

I have python installed in

/home/musigma/anaconda3/bin 

so I added the following as the shebang line to my python file

#!/home/musigma/anaconda3/bin python3

Following are the contents of my python file

#!/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>")

I have placed the file in /var/www. I also ran the following line to make my python file an executable

sudo chmod 755 hello.py 

But that still doesn't make my python script an executable. Instead of executing, the file gets downloaded. I am not sure how to make things work now. Any direction would be of great help. I have tried these links but they don't serve my purpose. I have already taken care of these things

Python CGI executable script downloads / shows script code

I have been trying to figure this out since the last four-five hours and not sure what to do next. Any direction would be great.

PS: I am using ubuntu 16.04

apache version

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

Upvotes: 1

Views: 876

Answers (2)

Graham Dumpleton
Graham Dumpleton

Reputation: 58563

Only the directory /usr-lib/cgi-bin/ is setup to allow executable CGI scripts. That is what the:

Options +ExecCGI

indicates.

So fix the #! line as pointed out already, then move the script to the directory /usr-lib/cgi-bin/.

Finally, use the URL path of /cgi-bin/hello.py.

For more information on setting up Apache for executable CGI scripts, see the Apache documentation at:

Upvotes: 3

Bjartur Thorlacius
Bjartur Thorlacius

Reputation: 804

What if you copy/symlink your file to hello.cgi and hello.py? Do then 127.0.0.1/hello.cgi and 127.0.0.1/hello.py produce the source code or the results of the execution?

Try changing the first line of the script from:

#!/home/musigma/anaconda3/bin python3

to:

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

Also, I would have thought the path to your script was 127.0.0.1/hello.py (including the extension) by default.

Upvotes: 0

Related Questions