Nev's picture

I'm using the LAMP Amazon Machine Image (AMI) to learn python3, and I'm at the point where I need Apache configured to execute python3 scripts.  At present python scripts in /var/www/cgi-bin just get displayed, but I want them to actually run.  My book says to configure Apache to execute python - help!

Forum: 
Jeremy Davis's picture

But obviously not... I've never even tried though so this will be a little blind leading the blind...

With a quick bit of googling and it seems that when PHP is used as CGI it uses either mod_php or FastCGI whereas python will need:

a2enmod cgi
service apache2 restart

Also you may need to edit/add something like this below to your site to make Apache run them:

<Directory /var/www/cgi-bin>
        Options +ExecCGI
        AddHandler cgi-script .py
</Directory>

And finally, AFAIK the script will need to be executable (although perhaps I'm worng...?)

During my research it seems to me that ideally this isn't a great way to run Python (slow and resource intensive). It seems that mod_wsgi is actually better for running Python under Apache, or better still if you want to use python for web stuff, a python web framework such as web2py or Django is a better bet...

Nev's picture

to access files I had to omit /var/www

from url, apache must truncate in some config file
 
http://svr/index.php                 !worked
http://srv/var/www/index.php  !error: File does not exist: /var/www/var
 
fixed execute attribute:
chmod 755 response2.py
root@lamp www/cgi-bin# ls -l                                                    
-rwxr-xr-x 1 root root 175 Sep 17 13:04 response2.py
 
 
then
 
 [error] [client IP] Options ExecCGI is off in this directory: /var/www/response.py
 
Used  instructions  to modify /etc/apache2/sites-enabled/000-default as follows:
 
root@lamp apache2/sites-enabled# more 000-default                               
NameVirtualHost *:80
NameVirtualHost *:443
 
<VirtualHost *:80>
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/
</VirtualHost>
 
<VirtualHost *:443>
        SSLEngine on
        SSLCertificateFile /etc/ssl/certs/cert.pem
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/
</VirtualHost>
 
ScriptAlias /cgi-bin/ /var/www/cgi-bin/
 
<Directory /var/www/>
        AllowOverride None
        Options Indexes FollowSymLinks MultiViews +ExecCGI
        Order allow,deny
        allow from all
</Directory>
 
 
Restarted Apache2
 
root@lamp apache2/sites-enabled# service apache2 restart                        
[....] Restarting web server: apache2apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 for ServerName
 ... waiting apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 for ServerName
. ok 
 
Added #!/usr/bin/env python as first line in the python script &
moved python files to /var/www
 
I used the following command to help troubleshoot the problem.
tail -f /var/log/apache2/access.log &
tail -f /var/log/apache2/error.log &
 
Voila!
 
Only problem is that I don't think I'm using python3...but close enough for now
Jeremy Davis's picture

And thanks for posting back with such an extensive explanation. I'm sure that will help someone else! :)

Add new comment