Search code examples
pythonapacheubuntulampaddhandler

Setting up Apache on Ubuntu LAMP Server to execute Python in Virtual Host


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.


Solution

  • 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>