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
Related
I am trying to get into python cgi programming and installed Apache HTTP server on my Windows machine. I did the following changes to the httpd.conf file
Options Indexes FollowSymLinks ExecCGI
and uncommented below line and added .py at the end
AddHandler cgi-script .cgi .py
I placed the hello.py in htdocs folder. Here are the instructions I followed https://editrocket.com/articles/python_apache_windows.html
But the get this error
AH02811: script not found or unable to stat: C:/Apache24/htdocs/hello.py
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 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?
I'm using Windows XP and want to know how can I create multiple django versions on a single apache service through virtual host(of course).
I'm trying to do that with one instance of python too. Should i create 1 instance of python for each django version or django needs only its eggs to work, so I can have several eggs in just one python version?
You can do something like this in your httpd.conf
NameVirtualHost 0.0.0.0:80
<VirtualHost 0.0.0.0:80>
ServerName myserver.com
ServerAdmin myemail#gmail.com
DocumentRoot "/path/to/html/root"
ErrorLog "/path/to/apache-error.log"
CustomLog "/path/to/apache-access.log" common
Options ExecCGI FollowSymLinks MultiViews
AddHandler wsgi-script .wsgi
WSGIDaemonProcess djangoapp1
WSGIProcessGroup djangoapp1
WSGIScriptAlias / /path/to/djangoapp1.wsgi
Alias /static /path/to/static/files
DirectoryIndex index.html index.cgi
AddHandler cgi-script .cgi .pl
</VirtualHost>
NameVirtualHost 0.0.0.0:81
<VirtualHost 0.0.0.0:81>
ServerName myserver.com
ServerAdmin myemail#gmail.com
DocumentRoot "/path/to/html/root"
ErrorLog "/path/to/apache-error.log"
CustomLog "/path/to/apache-access.log" common
Options ExecCGI FollowSymLinks MultiViews
AddHandler wsgi-script .wsgi
WSGIDaemonProcess djangoapp2
WSGIProcessGroup djangoapp2
WSGIScriptAlias / /path/to/djangoapp2.wsgi
Alias /static /path/to/static/files
DirectoryIndex index.html index.cgi
AddHandler cgi-script .cgi .pl
</VirtualHost>
And then, in your djangoapp1.wsgi/djangoapp2.wsgi script you can define the different django versions and applications:
#!/usr/bin/python
import os
import sys
sys.path.append('')
sys.path.append('/path/to/python2.7/site-packages')
sys.path.append('/path/to/python2.7/dist-packages/Django-1.3-py2.7.egg ')
... etc ...
sys.path.append('/path/to/djangoapp1/src')
os.environ['DJANGO_SETTINGS_MODULE'] = 'djangoapp1.settings'
os.environ['PYTHON_EGG_CACHE'] = '/tmp'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
Method 1:
put django source anywhere you want and manually specify path to django source in your manage.py and wsgi.py:
import os
os.path.insert(0, 'path-to-django-source');
You can also use virtualenv. Virtualenv fixes paths for console apps automatically, however for wsgi.py you still have to write down path's manually.
Method 2:
Use zc.buildout and djangorecipe, it will do all the stuff for you including:
donwloads django
download other modules
creates wsgi.py at project-dir\bin\wsgi
creates manage.py at project-dir\bin\django.exe
All this is done with a single config file buildout.cfg- here you list your modules and other settings, and then you run a command: buildout -N.
However buildout might not be a good solution if you have tight deadlines because there will be things you'll need learn about it but if you are planning to do more python apps I definitely recommend trying it.
Here are some examples for django+buildout setup:
http://www.google.lt/search?q=django+buildout+template+OR+skeleton
An update to your comment
You cannot install two django versions system wide.
What you can do though is either:
Do not install django, just drop the django-base/django folder into your project path. You will have to compile the internationalization files manually (if you use i18n):
cd django\conf
python ..\..\manage.py compilemessages
Or, install django with python setup.py install, but use extra arguments to change installation destination. Python documentation covers few different methods.