0

I'm trying to run it on localhost. A short introduction. Some links I've read before post: How to run Python CGI script How can I run Python CGI scripts on my web server? most precise: Need Help to Configure apache Server to run CGI Script written in Python And also official http://httpd.apache.org/docs/1.3/misc/FAQ.html#CGIoutsideScriptAlias http://www.editrocket.com/articles/python_apache_windows.html and other...


All steps they suggest to configure Apache for .cgi and .py scripts: 1)

install libapache2-mod-wsgi

[done] 2) check the script is executable and available for apache ~$ ls -lah /var/www/cgi-bin/cgi101.py

-rwxrwxr-x 1 user user 318 2012-11-27 03:03 /var/www/cgi-bin/cgi101.py

3) edit /etc/apache2/sites-available/default[updated to actual]:

<VirtualHost *:80>
    ServerAdmin webmaster@localhost

    DocumentRoot /var/www
    <Directory />
        Options FollowSymLinks
        AllowOverride None
    </Directory>
#   <Directory /var/www>
    <Directory /var/www/cgi-bin>
        Options Indexes FollowSymLinks MultiViews ExecCGI
        AllowOverride None
        Order allow,deny
        allow from all
        AddHandler cgi-script .cgi .py
#       AddHandler wsgi-script .wsgi
    </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

    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn

    CustomLog ${APACHE_LOG_DIR}/access.log combined

    Alias /doc/ "/usr/share/doc/"
    <Directory "/usr/share/doc/">
        Options Indexes MultiViews FollowSymLinks
        AllowOverride None
        Order deny,allow
        Deny from all
        Allow from 127.0.0.0/255.0.0.0 ::1/128
    </Directory>

</VirtualHost>

or with

<Directory /var/www/cgi-bin>

then restart Apache

sudo /etc/init.d/apache2 restart

and finally, try the script. Something like[updated to actual]:

#!/usr/bin/python3

import cgi
form = cgi.FieldStorage()
# parse form data
print('Content-type: text/html\n')
# hdr plus blank line
print('<title>Reply Page</title>')
# html reply page
if not 'user' in form:
  print('<h1>Who are you?</h1>')
else:
  print('<h1>Hello <i>%s</i>!</h1>' % cgi.escape(form['user'].value))

On the output i get

404 Not found

error. WTH? I've tried both and /var/www. And my python path to python3:

~$ ls -lah /usr/bin/python*
...
lrwxrwxrwx 1 root root    9 2011-10-05 23:53 /usr/bin/python3 -> python3.2
lrwxrwxrwx 1 root root   11 2012-10-20 06:17 /usr/bin/python3.2 -> python3.2mu
-rwxr-xr-x 1 root root 2.8M 2012-10-20 06:17 /usr/bin/python3.2mu
...

that's why i use

#!/usr/bin/python3

Thanks in advance!


[updated] /var/log/apache2/error.log

[Tue Nov 27 13:47:56 2012] [error] [client 127.0.0.1] script not found or unable to stat: /usr/lib/cgi-bin/script.py, referer: http://localhost/cgi1$

Why it looks for script.py in /usr/lib/ ??


[updated] I closed my eyes while reading the lines

 ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">

It must be path to executable .cgi or .py in /var/www/cgi-bin. Thanks Evert and everyone!

Community
  • 1
  • 1
boldnik
  • 2,547
  • 2
  • 27
  • 32
  • 404: what URL are you accessing? And perhaps other parts of your config are incomplete? Assuming there's a default `index.html` , do you get to see that one at least when visiting http://127.0.0.1/ ? –  Nov 27 '12 at 12:48
  • i open /localhost/cgi-bin/script.py - the actual path where the script is and where apache has access. There is no index.html/index.php/index.else – boldnik Nov 27 '12 at 13:02
  • Surely you mean `http://localhost/cgi-bin/script.py`? My apache configuration is a bit rusty, but possibly, you can leave off the .py extension there as well. But for your (and our) sanity, it'd be good to put an (empty) index.html file in `/var/www/` and see if you can at least find that. –  Nov 27 '12 at 13:24
  • Yes! localhost/index.html and localhost/index.php can be found! Even testpage works fine. But it is about php, not python... – boldnik Nov 27 '12 at 13:32
  • No, it's about an executable, whether that be shell, python, perl or php (the php you're seeing goes through modphp, not modcgi). You could try a perl script instead, but that will probably also fail. But that's beside the point. –  Nov 27 '12 at 13:50
  • `script.py` or `cgi101.py`? Just be careful with renaming things, that gets confusing. –  Nov 27 '12 at 13:57
  • You don't need to be installing 'install libapache2-mod-wsgi'. Has got nothing to do with CGI scripts. The mod_wsgi package would only have come up in as much as people would have been suggesting you not use CGI scripts, but instead use a WSGI server/adapter. – Graham Dumpleton Nov 27 '12 at 19:21

2 Answers2

2

At a guess, you have your script in /var/www/cgi-bin/cgi101.py.

But your Apache configuration has this:

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>

and add the AddHandler directive in there as well:

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

(and perhaps remove the section above this one, for the /var/www directory.)

I would then put your script at /usr/lib/cgi-bin/cgi101.py, and try again.

The Apache tutorial seems actually pretty clear on this.

  • Just noticed the update, which tells *exactly* what I say above ;-). –  Nov 27 '12 at 13:56
  • Thank you !!! Works perfectly. Probably I have eyes in ... somewhere else than where they should be. – boldnik Nov 27 '12 at 14:21
0

At first you must be sure that your script can be run by Python/OS. In your case you try to use Python3 and I see that you use old form of print. For Python3 you must use print() as function. Your script must run when you try to execute it from command line. Make sure it has execute attribute.

When you script will run from command line then you can try to run it via CGI. When in trouble check http server logs. They will probably be in /var/log/http and there should be error.log. Check it.

Also make sure that your file use unix type EndOfLine, i.e. LF only, not CRLF from DOS/Windows. If you use CRLF then first line (shebang) is corrupted.

Also you must separate HTTP headers from content using empty line. So instead of

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

use:

print("Content-Type: text/html\n")
Michał Niklas
  • 53,067
  • 18
  • 70
  • 114
  • 1
    If the script won't run, the status returned to the browser will be 500, not 404. – Wooble Nov 27 '12 at 12:14
  • You are right. Then maybe it is incorrect URL. But at first I would make it runnable from command line, then I would check http server logs. – Michał Niklas Nov 27 '12 at 12:24
  • I've done this before posting, the script runs successfully in shell (~$ python3 /var/www/cgi-bin/script.py). And the problem is really in apache configuration. i'm sure. – boldnik Nov 27 '12 at 13:09
  • And sorry for misunderstanding, all above was correct, i actually use print() for py3. – boldnik Nov 27 '12 at 13:10
  • Also try to execute your script from shell as: `~$ /var/www/cgi-bin/script.py` i.e. not by python3, but via shebang (first line in your script). – Michał Niklas Nov 27 '12 at 13:14
  • $ /var/www/cgi-bin/cgi101.py 'Content-type: text/html Reply Page

    Who are you?

    ' -- runs exactly how it should
    – boldnik Nov 27 '12 at 13:20
  • I've tried with **#!/usr/bin/python print "Content-type:text/html\r\n\r\n" print '' print '' print 'Hello Word - First CGI Program' print '' print '' print '

    Hello Word! This is my first CGI program

    ' print '' print ''** but it gives the same 404. Running in terminal gives the same good result.
    – boldnik Nov 27 '12 at 13:34
  • So you must look at Apache logs. Especially `access.log` and `error.log` – Michał Niklas Nov 27 '12 at 13:48