I am using apache2 and python2.6 on linux.
I know how to make apache run a *.py file, just add "AddHandler cgi-script .cgi .py" to httpd.conf file. But how to make apache2 run a *.pyc file?
Adding "AddHandler cgi-script .cgi .pyc" is not working.
I've heard that mod_python can enable both *.pyc and *.pyo run on apache. Could anybody tell me how does mod_python make it?
By the way in the current I don't want to learn mod_python or WSGI...
Thank you all in advance!
The problem is that httpd doesn't know how to run a .pyc file, since unlike a .py file (with its shebang line), a .pyc file contains no information on how it should be run. You will need to use binfmt_misc to instruct Linux on how to run a .pyc file. And don't forget to make the file itself executable as well.
It should work. If it doesn't, try running the .pyc file in question from the command line. If that does not work either, this superuser question might help.
Apart from that, I'm wondering what the reason is for not using the python source files?
In apache config file you add lines in section virtualhost:
ScriptAlias /cgi-bin/ /usr/bin/
Action cgi-handler /cgi-bin/python3
AddHandler cgi-handler .py .pyc .pyo
<Directory /usr/bin>
Require all granted
Options FollowSymLinks
</Directory>
This complete example of configuration file to www.domain.com.br.conf (in /etc/apache2/sites-enabled/)
DocumentRoot /home/domain.com.br/www/
ServerName domain.com.br
ServerAlias www.domain.com.br
<Directory /home/domain.com.br/www/>
Options FollowSymLinks MultiViews Includes
AllowOverride None
Order allow,deny
allow from all
Require all granted
</Directory>
ScriptAlias /cgi-bin/ /usr/bin/
Action cgi-handler /cgi-bin/python3
AddHandler cgi-handler .py .pyc .pyo
<Directory /usr/bin>
Require all granted
Options FollowSymLinks
</Directory>
Related
i'm facing a pretty crazy problem at the moment:
If I run my python cgi script in my home directory its working but if i run it in cgi-bin folder of my apache i get this error:
End of script output before headers: cgi_script.py
There is no SElinux enabled on this server.
This is my Apache conf:
LoadModule cgi_module modules/mod_cgi.so
ScriptAlias /cgi-bin/ "/path/to/cgi-bin/"
<Directory "/path/to/cgi-bin">
AllowOverride All
Options +ExecCGI
AddHandler cgi-script .py
Allow from all
Require all granted
</Directory>
Cgi-script permissions are 775 and owned by the apache user.
Thanks for your answers!
EDIT:
Just found the error... Shebang was wrong.
I created a droplet on DigitalOcean but can not run the python scripts in cgi-bin folder. I tried
sudo a2enmod cgi
chmod a+rwx cgi-bin
Also give chmod 755 to files but It did not work. When I try to acces cgi-bin folder it gives Forbidden error and says You don't have permission to access /cgi-bin/ on this server. Can any one help?
My config file
<VirtualHost *:80>
ScriptAlias /cgi-bin/ "/var/www/test/cgi-bin/"
<Directory /var/www/test/cgi-bin/>
AllowOverride All
Options ExecCGI
AddHandler cgi-script .cgi .pl .tcl .py
Order allow,deny
Allow from all
</Directory>
<Directory "/var/www/test/">
AllowOverride All
Options Indexes MultiViews FollowSymLinks ExecCGI
Require all granted
</Directory>
ServerAdmin webmaster#localhost
DocumentRoot "/var/www/test"
</VirtualHost>
Also I created a test python file in document root and when I opened it, it behaves like a text file.
You can try adding the following lines to your httpd.conf or you web-server config file
<Files ~ "\.(py|cgi)$">
SetHandler python-script
Options +ExecCGI
</Files>
See Also : mod-python
I'm trying to set up Python to run on my local apache server on a mac.
On httpd.conf, I've
<VirtualHost *:80>
DocumentRoot "/Users/ptamzz/Sites/python/sandbox.ptamzz.com"
<Directory "/Users/pritams/Sites/python/sandbox.ptamzz.com">
Options Indexes FollowSymLinks MultiViews ExecCGI
AddHandler cgi-script .py
Require all granted
</Directory>
DirectoryIndex index.py
ServerName sandbox.ptamzz.com
ErrorLog "/var/log/apache2/error-log"
CustomLog "/var/log/apache2/access-log" common
</VirtualHost>
On my document root, I've my index.py file as
#!/usr/bin/python
print "Content-type: text/html"
print "<html><body>Pritam's Page</body></html>"
But when I load the page on my browser, the python codes are returned as it is.
What all am I missing?
Executing python in Apache ( cgi )
System : OSX yosmite 10.10.3 , Default apache
uncommented in http config
LoadModule cgi_module libexec/apache2/mod_cgi.so
virtual hosts entry
<VirtualHost *:80>
# Hook virtual host domain name into physical location.
ServerName python.localhost
DocumentRoot "/Users/sj/Sites/python"
# Log errors to a custom log file.
ErrorLog "/private/var/log/apache2/pythonlocal.log"
# Add python file extensions as CGI script handlers.
AddHandler cgi-script .py
# Be sure to add ** ExecCGI ** as an option in the document
# directory so Apache has permissions to run CGI scripts.
<Directory "/Users/sj/Sites/python">
Options Indexes MultiViews FollowSymLinks ExecCGI
AddHandler cgi-script .cgi
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
index.py file
#!/usr/bin/env python
print "Content-type: text/html"
print
print "<html><body>Test Page</body></html>"
file was made executable using
chmod a+x index.py
restarted apache and output
For later Versions of Apache (2.4) the answer of Sojan V Jose is mostly still applying, except that I got an authorization failure, that can be resolved by replacing:
Order allow,deny
Allow from all
with:
Require all granted
I have a running Apache2 server on Ubuntu, with PHP installed.
My root folder is /usr/local/apache2/htdocs.
I have put a test python script in /usr/local/apache2/cgi-bin, named test.py.
test.py
#!/usr/bin/python
import cgi
import cgitb; cgitb.enable()
print "Content-Type: text/plain\r\n\r\n"
print 'hi'
I access this in the browser via http://localhost/cgi-bin/test.py. At the moment, it just displays the Python code that I just typed out, rather than executing it. I have looked extensively at online documentations and other stackoverflow questions that address this issue.
From that research I changed my httpd.conf file to include a few Directory's.
httpd.conf (stuff added to end of it)
ScriptAlias /cgi-bin/ "/usr/local/apache2/cgi-bin/"
<Directory "/usr/local/apache2/cgi-bin">
Options ExecCGI
SetHandler cgi-script
</Directory>
<Directory "/usr/local/apache2">
AddHandler cgi-script .py
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>
I have restarted/hard-started/stopped Apache and was able to run the Python file successfully through the command-line, but still no luck with actually executing the python file through the browser. I have even checked the Apache error log, but saw no indication of the problem. Can anyone offer some advice?
Thank you
You need to chmod 755 cgi-bin folder(and/or subfolder) where you script is.
Then chmod 755 your script file.
and finally..
Open script in notepad of your preference and save with encoding UTF-8 without BOM, and EOL(end of line) conversion must be set for UNIX systems. (in Notepad++ latter is in Edit menu)
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.
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>
Do you have the apache wsgi module installed and enabled?
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?