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

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.

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 install a server at home with django&postgre

Please help me to set my home PC as a server (Windows 10 + Python + Django + Postgre SQL + Anaconda)
There are several instructions on how to set the server on the Internet, and surprisingly, many instructions differ from each other. I have absolutely no experience in setting a server. Probably, I’m making a stupid mistake somewhere that I can’t identify for 3 days. I am lost.
I believe that most useful instructions are these (they are complete and new):
https://www.codementor.io/aswinmurugesh/deploying-a-django-application-in-windows-with-apache-and-mod_wsgi-uhl2xq09e
https://ostrokach.gitlab.io/post/apache-django-anaconda/
I followed the instructions and successfully downloaded the necessary modules, installed Wamp, made the changes as displayed in the guide and started it. What I see now: the Wamp icon glows green. When I load a localhost, the page loads endlessly but does not load.
Wamp error log shows following: [Fri Oct 11 14:50:33.823752 2019]
[core:notice] [pid 1364:tid 808] AH00094: Command line:
'c:\wamp64\bin\apache\apache2.4.39\bin\httpd.exe -d
C:/wamp64/bin/apache/apache2.4.39' [Fri Oct 11 14:50:33.840681 2019]
[mpm_winnt:notice] [pid 1364:tid 808] AH00418: Parent: Created child
process 15052 [Fri Oct 11 14:50:34.981629 2019] [mpm_winnt:notice]
[pid 15052:tid 800] AH00354: Child: Starting 64 worker threads.
Project name: turiumasina
path D:/Users/PycharmProjects/turiumasina/
path to wsgi:D:/Users/PycharmProjects/turiumasina/turiumasina/wsgi_windows.py (I renamed wsgi to wsgi_windows, settings file is in turiumasina/turiumasina/)
I copied the output generated by the mod_wsgi-express command and pasted it at the end of C:\wamp64\bin\apache\apache\conf\httpd.conf
my httpd-vhosts.conf
ServerName localhost
WSGIPassAuthorization On
ErrorLog "logs/turiumasina.error.log"
CustomLog "logs/turiumasina.access.log" combined
WSGIScriptAlias / "D:/Users/PycharmProjects/turiumasina/turiumasina/wsgi_windows.py"
<Directory "D:/Users/PycharmProjects/turiumasina">
<Files wsgi_windows.py>
Require all granted
</Files>
</Directory>
Alias /static "D:/Users/PycharmProjects/turiumasina/static"
<Directory "D:/Users/PycharmProjects/turiumasina/static">
Require all granted
</Directory>
wsgi_windows.py
import os
import sys
import site
from django.core.wsgi import get_wsgi_application
site.addsitedir("C:/users/.conda/envs/turiumasina/Lib/site-packages")
sys.path.append('D:/Users/PycharmProjects/turiumasina')
sys.path.append('D:/Users/PycharmProjects/turiumasina/turiumasina')
os.environ['DJANGO_SETTINGS_MODULE'] = 'turiumasina.settings'
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "turiumasina.settings")
application = get_wsgi_application()
I generated the "static" folder, wrote a line in the settings.py:
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
If I enter python manage.py runserver via pycharm, I can see my site through a browser (http://127.0.0.1:8000/). the Wamp icon glows green. When I load a localhost, the page loads endlessly but does not load.
I would like my site to be visible not only to me, but also to Internet users by entering my external IP.
If you need, I can send you the whole project and project settings
I would get rid of WAMP altogether, I don't see why you need it if you're not using PHP or MySql.
If you just need to play/debug the Django app over the local network just run ./manage.py runserver 0.0.0.0:8000 (or any other port). Make sure your Windows firewall is allowing incoming connections to the port.
The connect to it you need the computer's actual IP not '0.0.0.0:8000'. Type 'ipconfig' at the command prompt to see what your actual IP is an then connect to Your_actual_IP:8000
If you need something more solid than the build-in Django server (to run in non-debug mode) then you can install Waitress (https://docs.pylonsproject.org/projects/waitress/en/latest/).

mod_wsgi on Apache and Windows 7

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.

Django deployment with mod_wsgi on WAMP

Context
Python 2.7 64bit
Django 1.3
WAMP 2.2 (Apache 2.2.22) 64bit
mod_wsgi 3.4 64bit
Windows 7 64bit
Detail | Apache httpd.conf
...
Listen 192.168.20.11:8080
...
LoadModule wsgi_module modules/mod_wsgi.so
...
ServerName 192.168.20.11:8080
...
DocumentRoot "c:/wamp/www/"
...
<VirtualHost *:8080>
ServerName 192.168.20.11
ServerAdmin me#mine.com
DocumentRoot "C:/wamp/www/myapp"
<Directory "C:/wamp/www/mapp">
Order allow,deny
Allow from all
</Directory>
Alias /myapp/static "C:/wamp/www/myapp/static"
WSGIScriptAlias /test "C:/wamp/www/myapp/wsgi.py"
</VirtualHost>
Detail | wsgi.py
Generated by Django's startproject. Located at default /myapp/myapp/wsgi.py
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myapp.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
Problem
Cannot deploy Django app on WAMP given the context described above.
A "Hello World" test wsgi application works fine, so I have confirmed that mod_wsgi is active.
I get the error:
Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator, me#mine.com and inform them of the time the error occurred, and anything you might have done that may have caused the error.
More information about this error may be available in the server error log.
when pointing to:
http://192.168.20.11:8080/myapp
The static folder works fine, I can access all static files within the directory by pointing to:
http://192.168.20.11:8080/myapp/static
Detail | Server error log
NB: I've removed the timestamps for legibility.
mod_wsgi (pid=5728, process='', application='192.168.20.11:8080|/myapp'): Loading WSGI script 'C:/wamp/www/myapp/myapp/wsgi.py'.
mod_wsgi (pid=5728): Exception occurred processing WSGI script
'C:/wamp/www/myapp/myapp/wsgi.py'.
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\django\core\handlers\wsgi.py", line 236, in call
self.load_middleware()
for middleware_path in settings.MIDDLEWARE_CLASSES:
File "C:\Python27\lib\site-packages\django\conf\_init_.py", line 52, in getattr
self._setup(name)
File "C:\Python27\lib\site-packages\django\conf\_init_.py", line 47, in _setup
self._wrapped = Settings(settings_module)
File "C:\Python27\lib\site-packages\django\conf\_init_.py", line 132, in init
raise ImportError("Could not import settings '%s' (Is it on sys.path?): %s" %(self.SETTINGS_MODULE, e))
ImportError: Could not import settings 'myapp.settings' (Is it on sys.path?): No module named myapp.settings
File does not exist: C:/wamp/www/myapp/favicon.ico
I don't understand the function of middleware in this case and was under the impression it wasn't necessary to use middleware.
Regarding the system path, when I add:
WSGIPythonPath "c:/wamp/www/myapp"
to my VirtualHost in the httpd.conf file, the server refuses to start up.
Objective
To deploy a working Django app (that has been developed and tested locally using the Django development server) to a Microsoft SBS network without using IIS. I tried deploying on IIS and ran into difficulties early on so switched to WAMP.
I'm new to Python and Django and have never deployed a web app on any production server (I am an R programmer that has generally used programming for math and statistics).
Any advice that would help in achieving this objective, regardless of method, will be much appreciated!
This has nothing to do with middleware, that's just where the error was encountered. The error itself is the last line: "Could not import settings 'myapp.settings'". This is almost certainly because you haven't modified the PYTHONPATH - either in your wsgi or your Apache conf - to put "myapp" on the path.
See for instance the example Apache conf in the Django docs - there's a WSGIPythonPath directive there to add the app to the Python path, which you don't have.
Placing a WSGIPythonPath directive in the httpd.conf prevented the server from starting, but the problem was indeed related to the path not being declared.
Adding:
sys.path.append("c:/wamp/www/myapp")
to the wsgi.py file did the trick.
Thanks to Daniel Roseman for putting me on the right track.

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