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.
Related
I'm trying to test out running CGI scripts on a WAMP installation, I Have wamp installed under C:/wamp64and cgi_modules for apache enabled (was enabled by default). And I have put a test.py in C:/wamp64/cgi-bin/test.py with the content of:
#!C:\Users\fayss\AppData\Local\Programs\Python\Python310\python.exe -u
import cgi
import datetime
print("Content-type: text/html")
print()
print("<html><body>")
print("<h1>Current date and time:</h1>")
print("<p>%s</p>" % datetime.datetime.now())
print("</body></html>")
I have python added to the path and installed in C:\Users\fayss\AppData\Local\Programs\Python\Python310\python.exe
but when I access the page localhost/cgi-bin/test.py I get 404 not found error
I checked permissions and added all permissions to Users for test.py file.
httpd.conf
I've added this line in the httpd.conf of apache AddHandler cgi-script .py
and also tried commenting: # AddHandler cgi-script .cgi
and added ExecCGI to Options +Indexes +FollowSymLinks +Multiviews like this
Options +Indexes +FollowSymLinks +Multiviews +ExecCGI
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 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?
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>