mod_wsgi on Apache and Windows 7 - django

I installed Apache and mod_wsgi on Win 7 and copied config text output of mod_wsgi into httpd.conf.
I also check and see server working before mod_wsgi.
Now when I try to connect on local host I get a connection refused. After some research I found out I should add mod_wsgi.server to my app settings. I did it and ran runmodwsgi which failed because it was running a script in Windows which used os.getuid. Not valid in Windows. Is it mandatory to add mod_wsgi to isntalled apps in django project? Not mentioned in here.
If so what can I do for os.getuid in Windows?
Beside these why I get connection refused and no log if only my handler config is incorrect.

I ran httpd from cmd and found the error:
[Fri Dec 21 10:11:17.538864 2018] [wsgi:info] [pid 64084:tid 180]
mod_wsgi (pid=64084): Initializing Python. Fatal Python error:
Py_Initialize: unable to load the file system codec
ModuleNotFoundError: No module named 'encodings'
Don't know the reason yet.
[UPDATE]
Changed PYTHONHOME to python base path and it is working now. There most be an in compatibility between venv and setuptools.

Related

Django project crashes server when admin backend is accessed

Problem
I am running apache2 on my local ubuntu server. I set up a Django project using django-admin startproject site and set up my virtual host to use a WSGI Daemon process to run the Django project. This worked and the site is accessible through its IP 192.168.1.3.
When I go to /admin, it allows me to log in and see the initial backend but loads for 5 minutes then goes to a 500 error when I click anything or reload, even when trying to access the non-admin index page. This persists until I run systemctl restart apache2 and completely restart apache or wait ~10-15 minutes until it fixes itself (only to break again immediately if I access /admin pages).
Versions
Django version 4.1.5
mod-wsgi version 4.9.4
My Attempts
If I run the project with python manage.py runserver, I can access it on 192.168.1.3:8000 and fully use the /admin backend, even creating new users, etc. I then thought it was the WSGI Daemon process somehow messing it up, so I followed the linked section of this page: https://pypi.org/project/mod-wsgi#using-mod-wsgi-express-with-django and ran the site with python manage.py runmodwsgi. The site completely works on 192.168.1.3:8000 along with the /admin, and I can create new users etc., which points to it having nothing to do with the WSGI setup (as well as the non-admin pages working fine on the virtual host WSGI).
I have fully deleted and restarted the Django project, as well as setting up new virtual host .conf files with the proper WSGI information, etc.
I read about an /admin loading issue back in Django 3.0, but I am on 4.1 so that is unrelated.
I am at a complete loss at what the issue could be at this point. Why won't my /admin section work on the virtual host??
EDIT:
The issue is actually definitely the WSGI somehow. I am getting this is my apache error log:
[Sun Jan 29 09:27:12.388714 2023] [wsgi:error] [pid 67623:tid 140212873184832] (70007)The timeout specified has expired: [client 192.168.1.10:63055] mod_wsgi (pid=67623): Failed to proxy response from daemon., referer: http://192.168.1.3/admin/
[Sun Jan 29 09:27:14.427470 2023] [wsgi:error] [pid 67624:tid 140211782657600] [client 192.168.1.10:63053] Timeout when reading response headers from daemon process 'Portfolio': /var/www/Portfolio/Portfolio/wsgi.py, referer: http://192.168.1.3/admin/auth/
[Sun Jan 29 09:27:14.949762 2023] [wsgi:error] [pid 67624:tid 140211279357504] [client 192.168.1.10:63056] Timeout when reading response headers from daemon process 'Portfolio': /var/www/Portfolio/Portfolio/wsgi.py, referer: http://192.168.1.3/admin/auth/
[Sun Jan 29 09:27:16.385674 2023] [wsgi:error] [pid 67623:tid 140212856399424] [client 192.168.1.10:63100] Truncated or oversized response headers received from daemon process 'Portfolio': /var/www/Portfolio/Portfolio/wsgi.py
I still do not know what the solution is, though.
https://forum.djangoproject.com/t/django-apache-deployment-not-working-as-intended/15800
I found the solution. Somehow during the whole setup, adding WSGIApplicationGroup %{GLOBAL} to the /etc/apache2/apache2.conf file was never recommended, at least not that I noticed, but it worked and all the admin functionality works on the virtual host through WSGI Daemon.
Hope this can help someone else.

how to configure WSGISocketPrefix

I am getting a permissions issue when running django in daemon mode. Reading here https://code.google.com/p/modwsgi/wiki/ConfigurationIssues#Location_Of_UNIX_Sockets I think the solution is to configure the WSGISocketPrefix
The problem is that /var/run/wsgi is no where to be found on my centos server.
The closes thing I can find is: /etc/httpd/run/httpd.pid
How can I find where wsgi is installed?
Or what other value can I set the WSGISocketPrefix equal to?
With Ubuntu 14.0.4 LTS, Django11.1, Python 3.6.1, and mod_wsgi-4.5.15 it works for me as follows:
Apache was showing the following error:
503 Service Unavailable
Apache error log showed the following:
[wsgi:error] [pid 5411] (2)No such file or directory: [client 1.2.3.4:12345] mod_wsgi (pid=5411): Unable to connect to WSGI daemon process 'mywsgi' on '/var/run/apache2/wsgi.5403.0.1.sock' as user with uid=123.
Note that I'm running WSGI as a different user than the default www-data. This is changed with 'user' from WSGIDaemonProcess.
Solution:
At the bottom of /etc/apache2/apache2.conf, add:
WSGISocketPrefix /var/run/apache2/wsgi
After restarting Apache, I can ls -la and see the lock files under /var/run/apache2:
apache2.pid
wsgi.5017.0.1.sock
Sources:
http://modwsgi.readthedocs.io/en/develop/configuration-directives/WSGISocketPrefix.html?highlight=WSGISocketPrefix%20
https://code.google.com/archive/p/modwsgi/wikis/ConfigurationIssues.wiki#Location_Of_UNIX_Sockets

Django + mod_wsgi + apache: ImportError: No module named <module name>

Setting up my first app. I started on the django development server, and am now moving things to an apache setup so that I can code in my target prod environment. I've gotten mod_wsgi set up successfully and got a successful "Hello World!". Now I'm having issues actually getting my app to respond in apache the way it did in the django dev server.
Here's my directory structure:
/var/www/www.example.com/ [site directory]
Example [my app]
wsgi.py
settings.py
users [another django module i wrote]
trips [yet another django module i wrote]
Now, in my virtual host file, I have the following line:
WSGIScriptAlias / /var/www/www.example.com/Example/wsgi.py
And in my wsgi.py file, I have the following lines:
sys.path.append('/var/www/www.example.com/Example')
sys.path.append('/var/www/www.example.com/trips')
sys.path.append('/var/www/www.example.com/users')
Yet, here's the error I'm getting:
[Sun Jan 06 21:37:46 2013] [error] [client 127.0.0.1] ImportError: No module named trips
What do I need to do to get django and mod_wsgi to recognize my trips and users apps?
Thanks!
don't add every module to sys.path.
Instead you should add /var/www/www.example.com to sys.path.

Django + Apache, browser always loading and none data received

I am running Django site in Apache with mod_wsgi.
The site works before. Today when I am try to install a tokuwiki on the same server, I found the site become failed that when I open the django site, the chrome always loading and after a long long time it say "no data received".
The django site works on my development machine and another virtual machine.
On the amazon ec2:
When I use wget on static files (.css, .img) served by apache, it received normally.
But when I use wget on dynamic page from django, it stoped at awaiting response... , which means the problem is on the django + apache part.
I check the apache log that each time I refresh the browser, it will loading the django setting again, but nothing else. The log shows it did not enter the view function (I am not sure whether it entered the url routine or not):
[Sat Nov 19 02:15:54 2011] [error] Absolute_dir: /home/www/jdlab-browser/trunk/jdlab_browser
[Sat Nov 19 02:15:54 2011] [error] Using Amazon Server Settings
[Sat Nov 19 02:15:54 2011] [error] Absolute_dir: /home/www/jdlab-browser/trunk/jdlab_browser
[Sat Nov 19 02:15:54 2011] [error] Using Amazon Server Settings
I tried to uninstall tokuwiki, reinstall apache, mod_wsgi, django. Rewrite the apache configure file.
Other information:
I opened the Debug mode of Django, and when I visit a not exist page, Django's page not found error page showed on the browser.
When I open the django's admin console mysitename/admin/ , browser also did not received any data.
Any clu where to check next?
Finally, found the problem is caused by scipy package.
And then I found a similar problem asked by other people.
using-scipy-stats-stats-in-django-after-deployment

django-mssql not working in Apache w/ mod-wsgi, but works fine in dev server

I have a Django application using django-mssql to communicate w/ SQL Server.
This works just fine in the dev server (runserver) but under Apache/mod-wsgi, I get a fail related to it trying to find a .dll which does exist:
[Thu May 19 15:35:09 2011] [error] [client 127.0.0.1] File "C:\\Python27\\lib\\site-packages\\win32\\lib\\pywintypes.py", line 98, in
__import_pywin32_system_module__ [Thu May 19 15:35:09 2011] [error] [client
127.0.0.1] raise ImportError("No system module '%s' (%s)" % (modname, filename)) [Thu May 19 15:35:09 2011] [error] [client 127.0.0.1] ImportError: No system module 'pywintypes' (pywintypes27.dll)
pywintypes27.dll exists in C:\Windows\SysWOW64 (64 bit windows, but I installed 32 bit py and Apache)
Is there something in either httpd.conf or WSGIScriptAlias I need to do/specify to help it find this dll? Something else I should do? I've done a fair bit of Googling/Binging/Searching to no avail.
Some further info:
python27 (not Active State)
pywin32 is installed
Win Server 2008 (64 bit)
Apache 2.2
Django 1.3
Thanks
You mentioned that you have Apache 32bits and the official mod_wsgi site seems to only have win32 mod_wsgi binaries available, which in turn also require a 32 bits version of Python. From your description it is likely that you are using the pywin32 library for Windows 64 bits and you configuration expects the 32 bits one.