1

I am trying to setup a LAMP server in my lab, and I'm having trouble getting Apache to execute the .py files. It instead just downloads them. At first I thought my header might be wrong, but when I changed it, it unfortunately I'm still not executing the .py. On the plus side I can load the site, run the PHP, and CRUD the MySQL. I think the problem might be in how I'm setting up my Virtual Host. Here is the Apache2.conf:

<VirtualHost *:80>
    Alias "/SiteIwant" "/var/www/SiteIwant"
    ServerName localhost
    DocumentRoot /var/www/SiteIwant
    CustomLog /var/www/SiteIwant/my_access.log combined
    ErrorLog /var/www/SiteIwant/my_error.log
        AddType application/x-httpd-php .php
        SetEnv LD_LIBRARY_PATH /etc/init.d/mysql
    <Directory /var/www/SiteIwant>
        Options None ExecCGI
        AddHandler cgi-script .cgi .pl .py
        #AddHandler mod_python .py
        DirectoryIndex index.php
        AllowOverride AuthConfig
        Order deny,allow
        Deny from All
            Allow from 999.999.999.0/24 #anonymized for posting here, but working
    </Directory>
#   <Directory /var/www/SiteIwant/cgi/>
    #   AllowOverride All
    #   Options +ExecCGI +SymLinksIfOwnerMatch
    #   Order allow,deny
    #   Allow from all
    #</Directory>
</VirtualHost>

I've tried it with and without the specification on the cgi folder, and I've chkmod +rwx *.py in /var/www/SiteIwant/cgi. Just for kicks (after that didn't help), I also changed the mode of the python interpreter in /usr/bin and /usr/local/bin to +rwx.

Everything else in the apache2.conf file is as it comes out from current Ubuntu Server-LAMP option install.

I feel very stuck and like I'm missing something stupid/small.

Edit: Should this really be asked on Server Fault?

If I put an AddHandler cgi-script .cgi .pl .py outside the the Virtual Host, I get a 403 permission error, despite chmod 777 the folder.

Community
  • 1
  • 1
Atl LED
  • 656
  • 2
  • 8
  • 31

3 Answers3

1

late answer, I got through there too and got this working by adding ExecCGI in directory option, or for more security, like this:

<Directory /path/to/www/yourfile.py>
Options +ExecCGI
</Directory>
Albirew
  • 300
  • 1
  • 5
  • Thanks a lot for digging this up. That worked like a charm. It's on a closed network, so I'm not too concerned about the security, but why would that be more secure? – Atl LED Jan 06 '14 at 18:30
  • 1
    In my example, i allowed only _/path/to/www/yourfile.py_ to be executed as CGI so even if an harming file is put in the same directory, it will not be executed by the web server. – Albirew Jan 08 '14 at 00:32
0

Do you have the apache wsgi module installed and enabled?

Adam
  • 772
  • 3
  • 10
  • I don't know is the short answer, I'm trying to check now. I certainly didn't do it manually if it wasn't thrown in on the LAMP install. – Atl LED Jun 26 '13 at 21:02
  • I think you might be talking about the mod_wsgi in which case, no I don't think that it is. Should I install it? Just looking here: https://help.ubuntu.com/community/HelpOnInstalling/ApacheWithModWSGI I wouldn't think that it's needed? – Atl LED Jun 26 '13 at 21:35
0

This statement in the vhost and/or global config should be enough (and you have it in your config):

AddHandler cgi-script .py

Did you reload Apache after changing the config? If this doesn't work, you should check your webserver's errorlog, it should give some hints on the cause of the problem.

Also, you didn't describe what happens when you visit the URL hosting the python script, do you get an error message?

Teun Vink
  • 101
  • 4
  • When you visit the python script, it just downloads the python and displays in plain text. No error seems to be generated (in the log as well). And it would seem I have the handler added in the vhost config. – Atl LED Jun 26 '13 at 21:00
  • 1
    A quick test tells me that this should be enough. Check permissions of the file (readable and executable by the uid the webserver is using) and if your statement is in the right vhost. – Teun Vink Jun 26 '13 at 21:09
  • As far as reloading goes, I ran sudo apache2ctl restart, is that the same? – Atl LED Jun 26 '13 at 21:09
  • 1
    Yes, restart does even more than just reloading the config. – Teun Vink Jun 26 '13 at 21:11
  • I feel like the permissions might be the problem. How would I check change them for the uid the webserver is using? I've done "sudo chmod +rwx *.py" in the folder where they are located. I haven't tried to do anything with: `User ${APACHE_RUN_USER}` or `Group ${APACHE_RUN_GROUP}` – Atl LED Jun 26 '13 at 21:27
  • so this might have been a bit of a hammer, but I went exicuted "sudo chmod -R 777 /var/www/SiteIwant" and I still have the problem. Does that tell us it's not a permissions problem? – Atl LED Jun 26 '13 at 21:55