Django WSGI daemon mode synchronization of requests - django

Running appache2 with the following /etc/httpd.conf:
<VirtualHost *:80>
WSGIDaemonProcess myapp user=pq group=pq processes=2 threads=1
WSGIProcessGroup myapp
LogLevel debug
<Directory /django/myapp/apache/>
Order allow,deny
Allow from all
</Directory>
WSGIScriptAlias / /django/myapp/apache/django.wsgi
</VirtualHost>
where this is my /django/myapp/apache/django.wsgi:
import os
import sys
sys.path.append('/django')
os.environ['PYTHONPATH'] = '/django'
os.environ['DJANGO_SETTINGS_MODULE'] = 'myapp.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
I have the following view:
def sleep(request):
print >> sys.stderr, '{', os.getpid()
time.sleep(5)
print >> sys.stderr, '}', os.getpid()
return index(request)
I make 4 concurrent requests and my error log shows:
[Wed Jan 12 12:59:56 2011] [error] {17160
[Wed Jan 12 13:00:01 2011] [error] }17160
[Wed Jan 12 13:00:01 2011] [error] {17157
[Wed Jan 12 13:00:06 2011] [error] }17157
[Wed Jan 12 13:00:06 2011] [error] {17160
[Wed Jan 12 13:00:11 2011] [error] }17160
[Wed Jan 12 13:00:11 2011] [error] {17157
[Wed Jan 12 13:00:16 2011] [error] }17157
Basically my requests were synchronized per webserver (not even per
process).
Why is this?
Edit: This is a single CPU machine and Apache2 is compiled with
prefork MMP. My client was 4 tabs in Chrome. Interesting, when I try
this with curl I get the expected:
[Wed Jan 12 18:10:18 2011] [error] {17160
[Wed Jan 12 18:10:18 2011] [error] {17157
[Wed Jan 12 18:10:23 2011] [error] }17160
[Wed Jan 12 18:10:23 2011] [error] {17160
[Wed Jan 12 18:10:23 2011] [error] }17157
[Wed Jan 12 18:10:23 2011] [error] {17157
[Wed Jan 12 18:10:28 2011] [error] }17160
[Wed Jan 12 18:10:28 2011] [error] }17157
Edit2: Looks like this is an issue with Chrome synchronizing requests. My (limited) tests showed that this only happens with Chrome and only when tabs are used. Multiple requests within a single tab are asynchronous.

Related

Truncated or oversized response headers in daemon mode

I've a weird error in a flask website.
Important : The first page (a form) is running, but when I submit, I've a 505.
I try severals things thaks to StackOverflow :), but I cannot find the solution.
Here the log :
Mon Feb 25 09:17:37.863313 2019] [wsgi:info] [pid 30874:tid 140525377849088] [remote 192.168.56.1:61092] mod_wsgi (pid=30874, process='flask_my_app', application=''): Loading WSGI script '/var/www/my_app/my_app/flask_my_app/flask_my_app.wsgi'.
[Mon Feb 25 09:17:38.191855 2019] [wsgi:error] [pid 30874:tid 140525377849088] /home/vagrant/.virtualenvs/my_app/lib/python2.7/site-packages/psycopg2/__init__.py:144: UserWarning: The psycopg2 wheel package will be renamed from release 2.8; in order to keep installing from binary please use "pip install psycopg2-binary" instead. For details see: <http://initd.org/psycopg/docs/install.html#binary-install-from-pypi>.
[Mon Feb 25 09:17:38.191899 2019] [wsgi:error] [pid 30874:tid 140525377849088] """)
[Mon Feb 25 09:17:44.995445 2019] [wsgi:error] [pid 30781:tid 140525239768832] [client 192.168.56.1:3595] Truncated or oversized response headers received from daemon process 'flask_my_app': /var/www/my_app/my_app/flask_my_app/flask_my_app.wsgi, referer: http://vm.my_app/
==> /var/log/apache2/error.log <==
[Mon Feb 25 09:17:45.562808 2019] [core:notice] [pid 30767:tid 140525508945792] AH00051: child pid 30874 exit signal Segmentation fault (11), possible coredump in /etc/apache2
==> /var/log/apache2/my_app-error.log <==
[Mon Feb 25 09:17:45.589899 2019] [wsgi:info] [pid 30956:tid 140525508945792] mod_wsgi (pid=30956): Attach interpreter ''.
[Mon Feb 25 09:17:45.591070 2019] [wsgi:info] [pid 30956:tid 140525508945792] mod_wsgi (pid=30956): Adding '/var/www/my_app/my_app' to path.
[Mon Feb 25 09:17:45.595458 2019] [wsgi:info] [pid 30956:tid 140525508945792] mod_wsgi (pid=30956): Adding '/home/vagrant/.virtualenvs/my_app/lib/python2.7/site-packages' to path.
[Mon Feb 25 09:17:45.608611 2019] [wsgi:info] [pid 30956:tid 140525377849088] [remote 192.168.56.1:61092] mod_wsgi (pid=30956, process='flask_my_app', application=''): Loading WSGI script '/var/www/my_app/my_app/flask_my_app/flask_my_app.wsgi'.
[Mon Feb 25 09:17:45.916197 2019] [wsgi:error] [pid 30956:tid 140525377849088] /home/vagrant/.virtualenvs/my_app/lib/python2.7/site-packages/psycopg2/__init__.py:144: UserWarning: The psycopg2 wheel package will be renamed from release 2.8; in order to keep installing from binary please use "pip install psycopg2-binary" instead. For details see: <http://initd.org/psycopg/docs/install.html#binary-install-from-pypi>.
[Mon Feb 25 09:17:45.916224 2019] [wsgi:error] [pid 30956:tid 140525377849088] """)
My Virtual Host in Apache 2.7 is here :
<VirtualHost *:80>
ServerName vm.my_app
ServerAlias vm.my_app
DocumentRoot /var/www/my_app/my_app
LogLevel info
WSGIDaemonProcess flask_my_app python-path=/var/www/my_app/my_app:/home/vagrant/.virtualenvs/my_app/lib/python2.7/site-packages display-name=%{GROUP}
WSGIProcessGroup flask_my_app
WSGIScriptAlias / /var/www/my_app/my_app/flask_my_app/flask_my_app.wsgi
WSGIApplicationGroup %{GLOBAL}
#Alias /favicon.ico /var/www/my_app/my_app/static/favicon.ico
Alias /site_media/ /var/www/my_app/my_app/site_media/
Alias /media/ /var/www/my_app/my_app/media/
Alias /static/ /var/www/my_app/my_app/static/
AliasMatch /([/]*\.css) /var/www/my_app/my_app/static/css/$1
<Directory /var/www/my_app/my_app>
Order allow,deny
Allow from all
Require all granted
</Directory>
<Directory /var/www/my_app/my_app/flask_my_app>
Order allow,deny
Allow from all
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/my_app-error.log
CustomLog ${APACHE_LOG_DIR}/my_app-access.log combined
</VirtualHost>
The python version is 2.7.12, and I use "virtual env wrapper".
The version of the wsgi module is old I think :
lrwxrwxrwx 1 root root 15 mai 3 2016 mod_wsgi.so -> mod_wsgi.so-2.7*
-rwxrwxrwx 1 root root 223K mai 3 2016 mod_wsgi.so-2.7*
The wsgi file is "classic", and the requirements.txt file :
$ cat ../requirements.txt
click==6.7
Flask==1.0.2
Flask-Assets==0.12
Flask-Caching==1.4.0
Flask-Login==0.4.1
Flask-SQLAlchemy==2.3.2
Flask-WTF==0.14.2
itsdangerous==0.24
Jinja2==2.10
MarkupSafe==1.0
pbr==5.1.1
psycopg2==2.7.5
six==1.12.0
SQLAlchemy==1.2.8
stevedore==1.30.0
webassets==0.12.1
Werkzeug==0.14.1
WTForms==2.2.1
Can anobody helps me ?
Thanks a lot
F.
It's maybe useful to others, that my issue was fixed by desactivating the SSL support !!!, in postgresql.conf :
80 ssl = off # (change requires restart)
the log :
==> /var/log/postgresql/postgresql-9.6-main.log <==
2019-03-07 15:28:28.573 CET [5346] [unknown]#[unknown] LOG: could not accept SSL connection: EOF detected

Django Misconfiguration

I am trying to deploy mayan-edms a django web application on a RedHat5.6 server in production mode with apache webserver.
I have setup the database and the used a virtual-env to setup the env
This is my httpd.conf
WSGIScriptAlias /mayan /var/www/mayan/mayan/wsgi/dispatch.wsgi
WSGIPythonPath /var/www/mayan/mayan:/var/www/mayan/lib/python2.6/site-packages
<Directory /var/www/mayan/mayan>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>
I am getting a 500 error if I start apache with this conf. Could anyone tell me where it is getting misconfigured?
This is the error
[Mon Jan 14 12:47:02 2013] [error] [client 130.229.146.48] mod_wsgi (pid=21715): Exception occurred processing WSGI script '/var/www/mayan/mayan/wsgi/dispatch.wsgi'.
[Mon Jan 14 12:47:02 2013] [error] [client 130.229.146.48] Traceback (most recent call last):
[Mon Jan 14 12:47:02 2013] [error] [client 130.229.146.48] File "/var/www/mayan/lib/python2.6/site-packages/django/core/handlers/wsgi.py", line 250, in __call__
[Mon Jan 14 12:47:02 2013] [error] [client 130.229.146.48] self.load_middleware()
[Mon Jan 14 12:47:02 2013] [error] [client 130.229.146.48] File "/var/www/mayan/lib/python2.6/site-packages/django/core/handlers/base.py", line 45, in load_middleware
[Mon Jan 14 12:47:02 2013] [error] [client 130.229.146.48] mod = import_module(mw_module)
[Mon Jan 14 12:47:02 2013] [error] [client 130.229.146.48] File "/var/www/mayan/lib/python2.6/site-packages/django/utils/importlib.py", line 35, in import_module
[Mon Jan 14 12:47:02 2013] [error] [client 130.229.146.48] __import__(name)
[Mon Jan 14 12:47:02 2013] [error] [client 130.229.146.48] File "/var/www/mayan/mayan/apps/common/__init__.py", line 17
[Mon Jan 14 12:47:02 2013] [error] [client 130.229.146.48] from .conf.settings import (AUTO_CREATE_ADMIN, AUTO_ADMIN_USERNAME,
[Mon Jan 14 12:47:02 2013] [error] [client 130.229.146.48] ^
[Mon Jan 14 12:47:02 2013] [error] [client 130.229.146.48] SyntaxError: invalid syntax
This is my init.py around line 17
from django.db import transaction, DatabaseError
from navigation.api import register_links, register_top_menu
from .conf.settings import (AUTO_CREATE_ADMIN, AUTO_ADMIN_USERNAME, AUTO_ADMIN_PASSWORD, TEMPORARY_DIRECTORY)
from .conf import settings as common_settings
from .utils import validate_path
from .models import AutoAdminSingleton
Sounds like a Python version issue. RHEL 5.6 appears to be quite an old release, and anyway Red Hat are known for bundling old versions of Python. Looking at this table it seems likely that that version of RHEL was not even Python 2.4.
From the other side, the minimum Python version to run the current Django release is 2.7. Although that said, the requirements.txt file for your app actually asks for Django 1.3.5 - but you will still need at least Python 2.6 for that. You will either need to compile and install that version yourself, or upgrade your version of Red Hat.
You are using python 2.6, the documentation for installation seems to use python 2.7

Apache-Django WSGI exception: ValueError: zero length field name in format

I'm setting up a django server (1.4, python 2.7) with apache2 + mod_wsgi on a Debian system. From the django side, everything works perfect, if I run manage.py runserver, the development server displays the site correctly, however, when I try to set up my apache server to run WSGI, it fails displaying a ValueError.
This is mysite.wsgi
ALLDIRS = ['/usr/local/lib/python2.7/site-packages']
# the above directory depends on the location of your python installation.
# if using virtualenv, it will need to match your projects locale.
import os
import sys
import site
os.environ['DJANGO_SETTINGS_MODULE'] = 'ggDatabase.settings'
prev_sys_path = list(sys.path)
sys.path.append("/usr/local/lib/python2.7")
path = "/home/acad/statsdb/src"
if path not in sys.path:
sys.path.append(path)
sys.path.append("/home/acad/statsdb/src/ggDatabase")
for directory in ALLDIRS:
site.addsitedir(directory)
new_sys_path = []
for item in list(sys.path):
if item not in prev_sys_path:
new_sys_path.append(item)
sys.path.remove(item)
sys.path[:0] = new_sys_path
# change this depending on your project.
site.addsitedir("/home/acad/statsdb/src/ggDatabase/")
os.environ['PYTHON_EGG_CACHE'] = '/home/acad/.python-eggs'
os.environ['SETTINGS_MODULE'] = 'settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
This is the apache VirtualHost config:
<VirtualHost 127.0.0.1:8000>
ServerName mysite.com
ServerAlias www.mysite.com
<Directory /home/acad/statsdb/src/ggDatabase>
Order deny,allow
Allow from all
</Directory>
LogLevel warn
CustomLog /var/log/apache2/mysite.com.access.log combined
ErrorLog /var/log/apache2/mysite.com.error.log
WSGIDaemonProcess mysite.com user=acad group=acad threads=25 python-path=/usr/local/lib/python2.7
WSGIProcessGroup mysite.com
WSGIScriptAlias / /home/acad/statsdb/src/ggDatabase/apache/mysite.wsgi
</VirtualHost>
This is the exact configuration I have on my test server and it works there, but the new server runs into a problem when trying to run the wsgi script:
[Fri Jun 29 09:38:14 2012] [error] [client 127.0.0.1] mod_wsgi (pid=26233): Target WSGI script '/home/acad/statsdb/src/ggDatabase/apache/mysite.wsgi' cannot be loaded as Python module.
[Fri Jun 29 09:38:14 2012] [error] [client 127.0.0.1] mod_wsgi (pid=26233): Exception occurred processing WSGI script '/home/acad/statsdb/src/ggDatabase/apache/mysite.wsgi'.
[Fri Jun 29 09:38:14 2012] [error] [client 127.0.0.1] Traceback (most recent call last):
[Fri Jun 29 09:38:14 2012] [error] [client 127.0.0.1] File "/home/acad/statsdb/src/ggDatabase/apache/mysite.wsgi", line 31, in <module>
[Fri Jun 29 09:38:14 2012] [error] [client 127.0.0.1] import django.core.handlers.wsgi
[Fri Jun 29 09:38:14 2012] [error] [client 127.0.0.1] File "/usr/local/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 8, in <module>
[Fri Jun 29 09:38:14 2012] [error] [client 127.0.0.1] from django import http
[Fri Jun 29 09:38:14 2012] [error] [client 127.0.0.1] File "/usr/local/lib/python2.7/site-packages/django/http/__init__.py", line 11, in <module>
[Fri Jun 29 09:38:14 2012] [error] [client 127.0.0.1] from urllib import urlencode, quote
[Fri Jun 29 09:38:14 2012] [error] [client 127.0.0.1] File "/usr/local/lib/python2.7/urllib.py", line 1228, in <module>
[Fri Jun 29 09:38:14 2012] [error] [client 127.0.0.1] _safe_map[c] = c if (i < 128 and c in always_safe) else '%{:02X}'.format(i)
[Fri Jun 29 09:38:14 2012] [error] [client 127.0.0.1] ValueError: zero length field name in format
The error is so generic that I really don't know what could be going wrong. Any ideas?
Thanks in advance!
Bruno
Solved it. If someone has similar errors, make sure to print sys.version, you may find out (as I did) that wsgi was configured under another installed python version.
If that's the case, reinstall mod_wsgi with the --with-python option.

Snow Leopard: Apache + mod_wsgi + django problem

i tried to set up apache + mod_wsgi + django on my snow leopard, but it kept gave me the same errors which listed below. i tried to follow every tutorial on the web that i could found, but still couldn't make it work :( . (im sure all the paths are added into sys.path, but don't know why it had the import error)
[Tue Aug 02 14:47:36 2011] [error] [client 127.0.0.1] import django.core.handlers.wsgi
[Tue Aug 02 14:47:36 2011] [error] [client 127.0.0.1] ImportError: No module named django.core.handlers.wsgi
[Tue Aug 02 14:47:36 2011] [error] [client 127.0.0.1] mod_wsgi (pid=4333): Target WSGI script '/Users/kyle/wsgi_source/django-tutorial/mysite/apache/django.wsgi' cannot be loaded as Python module.
[Tue Aug 02 14:47:36 2011] [error] [client 127.0.0.1] mod_wsgi (pid=4333): Exception occurred processing WSGI script '/Users/kyle/wsgi_source/django-tutorial/mysite/apache/django.wsgi'.
[Tue Aug 02 14:47:36 2011] [error] [client 127.0.0.1] Traceback (most recent call last):
[Tue Aug 02 14:47:36 2011] [error] [client 127.0.0.1] File "/Users/kyle/wsgi_source/django-tutorial/mysite/apache/django.wsgi", line 8, in <module>
[Tue Aug 02 14:47:36 2011] [error] [client 127.0.0.1] import django.core.handlers.wsgi
[Tue Aug 02 14:47:36 2011] [error] [client 127.0.0.1] ImportError: No module named django.core.handlers.wsgi
Here is the apache configuration file:
<VirtualHost *:80>
WSGIDaemonProcess localdjango processes=2 threads=15 display-name=%{GROUP}
WSGIProcessGroup localdjango
ServerName localdjango
WSGIScriptAlias / /Users/kyle/wsgi_source/django-tutorial/mysite/apache/django.wsgi
<Directory /Users/kyle/wsgi_source/django-tutorial/mysite/apache>
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
here is /Users/kyle/wsgi_source/django-tutorial/mysite/apache/django.wsgi
1 import sys
2 import os
3
4 sys.path.append('/Users/eookoo/wsgi_source/django-tutorial')
5
6 os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'
7
8 import django.core.handlers.wsgi
9 application = django.core.handlers.wsgi.WSGIHandler()
Thanks in advance.
It is import to know whether you are using the operating system supplied Python or not.
If you are and that is the only Python version on the system, then Django wasn't installed into it. That or if you are using a Python virtual environment, then you haven't set up your WSGI script file or mod_wsgi to use the Python virtual environment.
If you have installed a separate Python version, and Django is installed into it, then likely that mod_wsgi was compiled against the system Python version and not your version.
Alternative to the latter is that you are hitting a problem that occurs with some Python installations that caused framework linking under MacOS X to not work properly and at run time, even though mod_wsgi was compiled against your separate Python version, it is still using the system wide Python framework. In this latter case you need to rebuild mod_wsgi against your separate Python version, but this time use the '--disable-framework' option to the 'configure' script for mod_wsgi before building it.
For a general resource on MacOS X issues for mod_wsgi, ensure you read the documentation at:
http://code.google.com/p/modwsgi/wiki/InstallationOnMacOSX

Setting up Apache and Python WSGI to use VirtualEnv

I'm having trouble getting Apache/WSGI to use my VirtualEnv. I have added the following two lines (path on server is pointing to the actual location of site-packages in the target virtualenv) to my WSGI file:
import site
site.addsitedir('/sites/mysite/virtpy/lib/python2.6/site-packages')
(from http://www.foxhop.net/django-virtualenv-apache-mod_wsgi). However, when I try to load the url in the browser I get a 500. Checking the apache logs:
[Sun Jul 17 11:07:11 2011] [error] [client 94.170.105.142] app = import_module(appname)
[Sun Jul 17 11:07:11 2011] [error] [client 94.170.105.142] File "/usr/local/lib/python2.6/dist-packages/django/utils/importlib.py", line 35, in import_module
[Sun Jul 17 11:07:11 2011] [error] [client 94.170.105.142] __import__(name)
[Sun Jul 17 11:07:11 2011] [error] [client 94.170.105.142] TemplateSyntaxError: Caught ImportError while rendering: No module named tagging
[Sun Jul 17 11:07:11 2011] [debug] mod_deflate.c(615): [client 94.170.105.142] Zlib: Compressed 629 to 387 : URL /
So I guess that the VirtualEnv isn't being loaded. Anyone know how to tell Apache / WSGI the correct virtualenv to use?
UPDATE
I have updated django.wsgi following Ken's advice, but now I am getting the following error in the apache log
[Sun Jul 17 16:46:36 2011] [info] [client 94.170.105.142] mod_wsgi (pid=11260, process='', application='igniteflow-django.com:8090|'): Loading WSGI script '/sites/igniteflow/apache/django.wsgi'.
[Sun Jul 17 16:46:36 2011] [error] [client 94.170.105.142] mod_wsgi (pid=11260): Target WSGI script '/sites/igniteflow/apache/django.wsgi' cannot be loaded as Python module.
[Sun Jul 17 16:46:36 2011] [error] [client 94.170.105.142] mod_wsgi (pid=11260): Exception occurred processing WSGI script '/sites/igniteflow/apache/django.wsgi'.
[Sun Jul 17 16:46:36 2011] [error] [client 94.170.106.142] Traceback (most recent call last):
[Sun Jul 17 16:46:36 2011] [error] [client 94.170.105.142] File "/sites/igniteflow/apache/django.wsgi", line 5, in <module>
[Sun Jul 17 16:46:36 2011] [error] [client 94.170.105.142] execfile(activate_this, dict(__file__=activate_this))
[Sun Jul 17 16:46:36 2011] [error] [client 94.170.105.142] IOError: [Errno 13] Permission denied: '/root/.virtualenvs/igniteflow/bin/activate_this.py'
I assume this is because the virtualenv is in root and apache doesn't have permissions? I chowned the folder to root:www-data but it hasn't fixed the problem. Any suggestions?
In my app.wsgi file I have something like this. You will need to change it to put to where your virtual env is located, mine is under /opt/ve/ve_name/ in this example.
import os
# activate virtualenv
activate_this = os.path.expanduser("/opt/ve/ve_name/bin/activate_this.py")
execfile(activate_this, dict(__file__=activate_this))