Redirect stdout and stderr to file in Django with wsgi - django

I'm trying to make django work with wsgi in a shared hosting, so I cannot access server logs,
I'm trying to redirect the output to a file in my django.wsgi script, like this:
saveout = sys.stdout
log_out = open('out.log', 'w')
sys.stdout = log_out
but this is the error that I have (in the only error log that I can access)
[Thu Oct 07 19:18:08 2010] [error] [client <ip>] File "/home/myuser/public_html/django.wsgi", line 9, in <module>
[Thu Oct 07 19:18:08 2010] [error] [client <ip>] mod_wsgi (pid=30677): Exception occurred processing WSGI script '/home/myuser/public_html/django.wsgi'.
and that is all the error I can get
Line 9 is the one that tries to open file. I've tried creating previously the file, giving writing access to everyone, without the file previously created.. and nothing.
In the app, I only get a 500 server error.
Is there a proper way to get this output without access to apache logs?
Edited:
Using absolute paths, I can open files and write to them, but I get empty files unless I close the files in the end, is it necessary? Could I leave the files open?..
Regarding to the initial problem, even though I used exception handling, write a "finish ok" to file in the end of my script, then close the file, but still get the error:
[Thu Oct 07 13:33:07 2010] [error] [client <ip>] mod_wsgi (pid=32541): Exception occurred processing WSGI script '/home/myuser/public_html/django.wsgi'.
If I intentionally raise an error, it is logged in the file, so as i get "ok" I think the script worked fine but the result is still that message error... no clues

You can't use relative pathnames like out.log in a WSGI application. WSGI doesn't specify where the working directory will be pointing and mod_wsgi doesn't set it unless specifically asked to in daemon mode.
Use an absolute pathname based on your application's root. You can use os.path.dirname(__file__) to get the directory containing the current script/module if needed.

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.

Can pySerial be used on a website to open and send data to serial port on local computer?

I assumed this would not be possible, but after reading this post thought that perhaps it might be:
How can I send data from a web page to a serial port?
So I added my code for serial as below and just hoped some sort of magic would occur:
import serial
ser = serial.Serial('/dev/ttyACM0', 9600)
As expected though, I got an error:
[Mon Aug 01 07:01:44.513340 2016] [wsgi:error] [pid 16:tid 140515737831488] raise SerialException(msg.errno, "could not open port %s: %s" % (self._port, msg))
[Mon Aug 01 07:01:44.513367 2016] [wsgi:error] [pid 16:tid 140515737831488] SerialException: [Errno 2] could not open port /dev/ttyACM0: [Errno 2] No such file or directory: '/dev/ttyACM0'
The code is working fine locally and the online environment mimics the modules available locally through requirements.txt.
Not directly what you want to hear. But the answer is no. You cannot use pySerial remotely unless you your webservice running locally on target computer.
But anyway you can still achieve your goal by using one of following approaches (in order of complexity for client):
Java applet or Java Web Start application (for example there is a good cross-platform library RXTX)
Browser plugins such as jUART, browser-serialport or serial for google chrome or even ActiveX component
Serial port to network proxy (like ser2net).
The only problem is that your clients will still need to do additional steps in order to your site could properly work (install java/native library/allow applet/browser plugin or additional software). The reason for that is because browsers are very restrictive and by default all filesystem operations are sandboxed by default for security reasons.

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

Apache mod_python with django issue

While running a django application on top of apache2 mod_python, I am getting this error message in my apache error log.
[Tue Dec 14 14:26:45 2010] [error] [client SOME_IP] IOError: Write failed, client closed connection., referer: http://example.com/
Traceback (most recent call last):
File "/usr/lib/python2.6/dist-packages/mod_python/importer.py", line 1931, in ReportError
req.write(text)
IOError: Write failed, client closed connection.
[Tue Dec 14 14:26:45 2010] [error] [client SOME_IP] python_handler: Dispatch() returned non-integer., referer: http://example.com/
Can anyone please suggest some solution on this?
The better long-term solution is to not use mod_python, since mod_python is no longer in development, and will not be supported in future versions of Django. Consider using mod_wsgi instead.
The Django documentation has this to say about mod_python:
Support for mod_python has been deprecated, and will be removed in Django 1.5. If you are configuring a new deployment, you are strongly encouraged to consider using mod_wsgi or any of the other supported backends.
Indicates that the user HTTP client connection was dropped before the complete response could be written back. Nothing one can do about it. Your application should handle it gracefully.