I am trying to setup Python with WSGI for a particular directory on Apache but I am getting the following error:
mod_wsgi (pid=3857): Target WSGI script '/var/www/test/test.py' does not contain WSGI application 'application'.
My test.py contains:
print 'Hello, World!'
And my wsgi.conf contains:
LoadModule wsgi_module modules/mod_wsgi.so
WSGIPythonHome /usr/local/bin/python2.7
Alias /test/ /var/www/test/test.py
<Directory /var/www/test>
SetHandler wsgi-script
Options ExecCGI
Order deny,allow
Allow from all
</Directory>
On top of all that, interestingly enough, the web browser returns a "404 Not Found" error but thankfully the error_log is a little more enlightening.
What am I doing wrong?
You're using WSGI as though it was CGI (strangely without headers).
What you need to do, for your immediate problem is adapt the following from http://code.google.com/p/modwsgi/wiki/QuickConfigurationGuide
def application(environ, start_response):
status = '200 OK'
output = 'Hello World!'
response_headers = [('Content-type', 'text/plain'),
('Content-Length', str(len(output)))]
start_response(status, response_headers)
return [output]
So that you have application present.
And from the referenced doc.
Note that mod_wsgi requires that the WSGI application entry point be
called 'application'. If you want to call it something else then you
would need to configure mod_wsgi explicitly to use the other name.
Thus, don't go arbitrarily changing the name of the function. If you
do, even if you set up everything else correctly the application will
not be found.
Related
Python3.8 has a new feature where you can set PYTHONPYCACHEPREFIX environment variable to a dir path so that the pycache uses a separate parallel filesystem tree, rather than the default __pycache__ subdirectories within each source directory:
https://docs.python.org/3/whatsnew/3.8.html#parallel-filesystem-cache-for-compiled-bytecode-files
I've set this environment variable:
export PYTHONPYCACHEPREFIX="$HOME/.cache/pycache/"
This works corectly with python3.8 via the command line directly, but it doesn't work for a python3.8 WSGI web application. How do you get this working with a WSGI web application? Here is an example of my WSGI web application:
# apache.conf Load python3.8 mod_wsgi (installed mod_wsgi by Python3.8 pip)
LoadModule wsgi_module /path/to/mod_wsgi-py38.cpython-38-x86_64-linux-gnu.so
# Vhost
<VirtualHost *:80>
ServerName example1.example.com
# The Python-home runs Python3.8 in venv
WSGIDaemonProcess example1 processes=2 threads=15 display-name=%{GROUP} python-home=/home/ubuntu/web-apps/example1/venv
WSGIProcessGroup example1
WSGIScriptAlias / /home/ubuntu/web-apps/example1/web/app.wsgi
<Directory /home/ubuntu/web-apps/example1/web>
Options -Indexes +FollowSymLinks
Require all granted
</Directory>
</VirtualHost>
# example1/web/app.wsgi
import os
import sys
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "../")))
from web.app import application
# example1/web/app.py
# Will return "Hello. Python version is 3.8.0"
import sys
def application(environ, start_response):
status = '200 OK'
header = [('Content-type', 'text/html; charset=utf-8')]
content = f"Hello. Python version is {sys.version}"
start_response(status, header)
content = content.encode('utf-8')
return [content]
I have a small web application that I have built using Flask and python. With the internal server that I used for developing everything runs fine. However now I want to use apache to start using it. But it doesn`t work. Keep in mind that I have never worked with apache or web based stuff before.
I used this guide as my starting point:
http://flask.pocoo.org/docs/deploying/mod_wsgi/
right now I have my application which is in the file called "/rg/server.py" and looks like this:
app=Flask(__name__)
# all app routes...
if __name__ == '__main__':
app.run(
debug=True,
host="127.0.0.1",
port=80
)
than I have a wsgi file as "/rg/wsgi/minerva.wsgi"
import sys
sys.path.insert(0, /rg)
from server import app as minerva
and finally I have an apache config file in "etc/apach2/sites-available/minerva.com":
<VirtualHost *>
ServerName minerva.test
WSGIDaemonProcess minerva threads=10
WSGIScriptAlias / /rg/wsgi/minerva.wsgi
<Directory /rg>
WSGIProcessGroup minerva
WSGIApplicationGroup %{GLOBAL}
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
Then I updated apache with a2ensite minerva.com which succeded. Then I releaded Apache and no errors. However I cannot acces minerva.test in any way...
If I type in apache2ctl -S it does list minerva.test
I have no idea what is going wrong...
system information:
OS: debian 64bit
python 2.7
The WSGI application entry point must be called 'application' for mod_wsgi. You have:
from server import app as minerva
It should be:
from server import app as application
You aren't even getting that far though, else the line:
sys.path.insert(0, /rg)
would give a syntax error.
Going back further, instead of:
<VirtualHost *>
you should have:
<VirtualHost *:80>
and finally, if 'minerva.test' isn't actually a resolvable host, you will not get anywhere.
So fill out your question with the actual URL you are using in the browser and also indicate whether 'minerva.test' is even listed in local hosts file.
The first thing I would check would be to make sure mod_wsgi is installed and loaded by apache. If that's fine, your setup looks pretty similar to mine with a few minor differences:
I had to add WSGISocketPrefix /var/run/wsgi above my VirtualHost definitoin. See here.
I included user and group values on the WSGIDaemonProcess line.
I'm trying to run Python within a sandbox using WSGI and Apache.
I created the virtual environment:
virtualenv /var/www/demo-environment --python /usr/bin/python3.3
I also created the following /var/www/demo.py file:
#!/usr/bin/env python
import sys
def application(environ, start_response):
start_response('200 OK', [('Content-Type', 'text/plain')])
return "Running " + str(sys.version_info)
Finally, I changed Apache configuration like this:
WSGIPythonPath /var/www/demo-environment/lib/python3.3/site-packages/
WSGIDaemonProcess example.com python-path=/var/www/demo-environment/lib/python3.3/site-packages/
WSGIProcessGroup example.com
WSGIScriptAlias / /var/www/demo.py
When going to the home page of the website, it shows the following contents: Running sys.version_info(major=2, minor=7, micro=5, releaselevel='final', serial=0), indicating that while Python works, it is not called within a virtual environment.
Since this is the wrong way to enable virtualenv, which is the right one?
Just use the setting WSGIPythonHome to specify the root of your env:
WSGIPythonHome /home/grapsus/work/python/env
WSGIScriptAlias /demo /home/grapsus/work/python/demo.py
I modified your script to print sys.path:
Running ['/home/grapsus/work/python/env/lib/python2.7', ...
I have the following lines in the config file for my Apache virtual host:
DocumentRoot /home/jordan/webprojects/wsgihello/web
WSGIDaemonProcess wsgihello processes=4 threads=15 display-name=%{GROUP}
WSGIProcessGroup wsgihello
WSGIScriptAlias /api /home/jordan/webprojects/wsgihello/api/front.py
front.py just returns a text document with the keys and values of the environ dict. I noticed that environ['GATEWAY_INTERFACE'] == 'CGI/1.1'.
What exactly does CGI/1.1 mean? Is Apache going though CGI to start a new interpreter for each request or does it simply identify mod_wsgi by that interface identifier?
It is an artifact of the fact that mod_wsgi calls an Apache C routine to generate the CGI key/values for the environ dictionary. Apache so happens to add that. It is not invoking a separate process.
Read:
http://code.google.com/p/modwsgi/wiki/ProcessesAndThreading
for more information about process/threading model in mod_wsgi.
I am running a ec2 instance to host some Django websites. The websites are being served by Apache with the use of mod_wsgi
Since a few days I am trying to deploy a new webplatform we are developing but I am running into a problem that seems impossible for me to solve. For security reasons we use scrypt 0.4 to secure the users personal information like passwords.
On the development server everything works like a charm but when we deploy to our live server we get a 500 Internal Server error. The apache log gives me the following message
Premature end of script headers: socialmarketingplatform.wsgi, referer:
When I uncomment the line where the scrypt module is used everything works fine. Also when I run the server delivered with Django with scrypt enabled everything works on the live server. So it is a combination of mod_wsgi, django and scrypt that generates the error.
I am using the following wsgi file:
import os
import sys
path = '/var/www/vhosts/[sub_domain]'
if path not in sys.path:
sys.path.append(path)
sys.path.append(path + '/socialmarketingplatform')
os.environ['DJANGO_SETTINGS_MODULE'] = 'socialmarketingplatform.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
And the following virtualhost config:
<VirtualHost *:80>
#Basic setup
ServerAdmin [removed email]
ServerName luxdevelopment.net
ServerAlias [sub domain]
DocumentRoot /var/www/vhosts/[sub domain]/socialmarketingplatform/
Alias /media/admin /usr/lib/python2.6/site-packages/Django-1.3-py2.6.egg/django/contrib/admin/media
Alias /media /var/www/vhosts/[sub domain]/socialmarketingplatform/media
<Location media="">
SetHandler None
</Location>
LogLevel warn
ErrorLog /var/log/httpd/smp_error.log
CustomLog /var/log/httpd/smp_access.log combined
WSGIDaemonProcess luxdevelopment.net user=apache group=apache threads=25
WSGIProcessGroup luxdevelopment.net
WSGIScriptAlias / /var/www/cgi-bin/socialmarketingplatform.wsgi
</VirtualHost>
I hope someone can help me with this problem. If there are any further question let me know.
See:
http://code.google.com/p/modwsgi/wiki/FrequentlyAskedQuestions#Apache_Process_Crashes
The message 'premature end of script headers' is usually indicative of your code crashing the daemon process. You can verify this by looking for segmentation fault or similar message in main Apache error log file. If you enable 'LogLevel info' in main Apache config and VirtualHost then mod_wsgi will log more about daemon process restarts.
A quick remedy if running only application in that daemon process group is to add:
WSGIApplicationGroup %{GLOBAL}
This will work around crashes caused by broken third party extension modules for Python which aren't written properly to work in sub interpreters.
Other than that, can be shared library version mismatches as described in the FAQ.