Django project crashes server when admin backend is accessed - django

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.

Related

How to display my Wt application in my local apache server?

I am very new to Wt and cgi stuff. What I am trying to do is write a web application in C++. This is possible thanks to the Wt library:
https://www.webtoolkit.eu/wt
Apparently there are two modes: http and fastcgi. The former is straightforward to use: I compile my code and run the bin. This starts some kind of web server (my vocabulary is probably not exact, as I am a big noob in this domain) and outputs the server's address and port. Typing all that in my favorite browser results in displaying the web page that I coded in C++.
I have a big problem with the latter and this is very annoying because this is the only way to deploy the software on a hosted remote server.
To deploy a test web server, I took the hello example found in the Wt library and linked it against wtfcgi by defining the cmake variable EXAMPLES_CONNECTOR=wtfcgi.
After that, I installed apache2 and set it up. I did the following:
I installed the following Ubuntu packages:
libapache2-mod-fastcgi
apache2
apache2-bin
apache2-data
apache2-utils
Then,
enable mod_fastcgi for Apache with a2enmod
modify the configuration file fastcgi.conf like this:
<IfModule mod_fastcgi.c>;
AddHandler fastcgi-script .wt
FastCgiIpcDir /var/lib/apache2/fastcgi
FastCgiConfig -idle-timeout 100 -maxClassProcesses 1 -initial-env WT_APP_ROOT=/tmp
FastCgiServer /var/www/wt/hello.wt
</IfModule>
create a new site configuration file for wt applications, /etc/apache2/sites-available/wt.conf:
<Directory /var/www/wt/>
Allow from all
Options +ExecCGI
</Directory>
copy my Wt example code into the folder /var/www/wt/hello.wt
enable the site and reload apache
a2ensite wt
service apache2 reload
Then, after that, the /var/log/apache2/error.log gave me
[Fri Jun 16 22:52:30.176846 2017] [core:notice] [pid 1676:tid 139808785006080] AH00094: Command line: '/usr/sbin/apache2'
[Fri Jun 16 22:52:30.177041 2017] [:warn] [pid 1678:tid 139808785006080] FastCGI: server "/var/www/wt/hello.wt" started (pid 1682)
[2017-Jun-16 22:52:30.328742] 1682 - [info] "WServer/wtfcgi: initializing relay server"
[2017-Jun-16 22:52:30.332011] 1682 - [info] "config: reading Wt config file: /etc/wt/wt_config.xml (location = '/var/www/wt/hello.wt')"
[2017-Jun-16 22:52:30.334308] 1682 - [info] "wtfcgi: spawned session process: pid = 1737"
[2017-Jun-16 22:52:30.334383] 1682 - [error] "wtfcgi: fatal: cannot create run directory '/var/run/wt'"
[Fri Jun 16 22:52:30.337635 2017] [:warn] [pid 1678:tid 139808785006080] FastCGI: server "/var/www/wt/hello.wt" (pid 1682) terminated by calling exit with status '1'
[2017-Jun-16 22:52:30.375392] 1737 - [info] "config: reading Wt config file: /etc/wt/wt_config.xml (location = '/var/www/wt/hello.wt')"
[2017-Jun-16 22:52:30.375519] 1737 - [info] "WServer/wtfcgi: initializing shared wtfcgi session process"
[2017-Jun-16 22:52:30.375708] 1737 - [error] "wtfcgi: fatal: bind(): No such file or directory"
After that, I followed the advice given in this web page:
http://redmine.webtoolkit.eu/projects/wt/wiki/Fastcgi_on_apache
For instance, I created the /var/run/wt myself and changed its owner to www-data. Then I got this log:
AH00112: Warning: DocumentRoot [/var/www/html] does not exist
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerName' directive globally to suppress this message
[Fri Jun 16 23:15:53.355484 2017] [:notice] [pid 1994:tid 139719627398656] FastCGI: process manager initialized (pid 1994)
[Fri Jun 16 23:15:53.355544 2017] [mpm_event:notice] [pid 1656:tid 139719627398656] AH00489: Apache/2.4.18 (Ubuntu) mod_fastcgi/mod_fastcgi-SNAP-0910052141 configured -- resuming normal operations
[Fri Jun 16 23:15:53.355554 2017] [core:notice] [pid 1656:tid 139719627398656] AH00094: Command line: '/usr/sbin/apache2'
[Fri Jun 16 23:15:53.355676 2017] [:warn] [pid 1994:tid 139719627398656] FastCGI: server "/var/www/wt/hello.wt" started (pid 1995)
[2017-Jun-16 23:15:53.365485] 1995 - [info] "WServer/wtfcgi: initializing relay server"
[2017-Jun-16 23:15:53.365675] 1995 - [info] "config: reading Wt config file: /etc/wt/wt_config.xml (location = '/var/www/wt/hello.wt')"
[2017-Jun-16 23:15:53.365878] 1995 - [info] "wtfcgi: spawned session process: pid = 2052"
[2017-Jun-16 23:15:53.365942] 1995 - [info] "wtfcgi: reading FastCGI stream from stdin"
[2017-Jun-16 23:15:53.373121] 2052 - [info] "config: reading Wt config file: /etc/wt/wt_config.xml (location = '/var/www/wt/hello.wt')"
[2017-Jun-16 23:15:53.373243] 2052 - [info] "WServer/wtfcgi: initializing shared wtfcgi session process"
which then looks fine. However, when I now display the address 127.0.1.1 on my firefox browser, I still see the default apache2 webpage. Why don't I see the hello example? How do I fix this? Do I have to write a special html page that calls the hello.wt? How does that work?
It seems like something is started but doesn't keep running. Where can I find logs of that?
Thanks in advance!
I managed to make it work by doing the following:
make sure the folder /var/www/html exists and copy my wt application there
modify the configuration file fastcgi.conf like this:
<IfModule mod_fastcgi.c>
AddHandler fastcgi-script .wt
FastCgiIpcDir /var/lib/apache2/fastcgi
FastCgiConfig -idle-timeout 100 -maxClassProcesses 1 -initial-env WT_APP_ROOT=/tmp
FastCgiServer /var/www/html/hello.wt
</IfModule>
get rid of the site configuration file for wt applications:
/etc/apache2/sites-enabled/wt.conf
because it has no effect on the process
reload apache
service apache2 reload
Now, when I type the address
localhost/hello.wt
in my browser, I can see the example displayed, assuming
/var/run/wt
could be created by the www-data user.

Video Straming on raspberrypi using flask apche2and wsgi server

I have used flask app for straming video via raspberrypi camera. The code i used for flask app is here:
https://blog.miguelgrinberg.com/post/video-streaming-with-flask
In local server it is doing video stream but not on my website.
I am using apache2 server having wsgi file below:
flaskapp2.wsgi
#!/usr/bin/python
import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/var/www/FlaskApp2/FlaskApp2")
from ashish import app as application
application.secret_key ='1233883'
The structure of file system is :
/var/www/FlaskApp2/
flskapp2.wsgi
FlaskApp2/
ashish.py
camera_pi.py
templates/index.html
The ashish.py is the flask app which is doing stream.
The error log file in apache is:
[Wed Nov 23 15:17:17.458803 2016] [mpm_prefork:notice] [pid 783] AH00163: Apache/2.4.10 (Raspbian) mod_wsgi/4.3.0 Python/2.7.9 configured -- resuming normal operations
[Wed Nov 23 15:17:17.459043 2016] [core:notice] [pid 783] AH00094: Command line: '/usr/sbin/apache2'
[Wed Nov 23 15:17:14.278353 2016] [wsgi:warn] [pid 662] mod_wsgi: Compiled for Python/2.7.8.
[Wed Nov 23 15:17:14.279359 2016] [wsgi:warn] [pid 662] mod_wsgi: Runtime using Python/2.7.9.
[Wed Nov 23 15:17:14.305871 2016] [mpm_prefork:notice] [pid 662] AH00163: Apache/2.4.10 (Raspbian) mod_wsgi/4.3.0 Python/2.7.9 configured -- resuming normal operations
[Wed Nov 23 15:17:14.306169 2016] [core:notice] [pid 662] AH00094: Command line: '/usr/sbin/apache2'
[Wed Nov 23 15:17:16.502484 2016] [wsgi:warn] [pid 670] mod_wsgi: Compiled for Python/2.7.8.
[Wed Nov 23 15:17:16.504897 2016] [wsgi:warn] [pid 670] mod_wsgi: Runtime using Python/2.7.9.
[Wed Nov 23 15:17:16.531217 2016] [mpm_prefork:notice] [pid 670] AH00163: Apache/2.4.10 (Raspbian) mod_wsgi/4.3.0 Python/2.7.9 configured -- resuming normal operations
[Wed Nov 23 15:17:16.531502 2016] [core:notice] [pid 670] AH00094: Command line: '/usr/sbin/apache2'
On localhost it is working but not on my website is there any mistake in wsgi file or i need to add something.
You have a few possible issues when getting a 404 Not Found.
If that VirtualHost is not the only one in the Apache configuration file, then it will never be used. This is because you have set ServerName incorrectly. The ServerName directory should be set to a host name. Not a URL, not an IP address. Thus using:
ServerName https://7a78657b7a.dataplicity.io
is incorrect. It should be:
ServerName 7a78657b7a.dataplicity.io
It has to be a host name as Apache relies on it for name base virtual host matching against the Host header in the request. If it is wrong, then Apache will not know which is the correct VirtualHost to use. When this occurs Apache will fall back to sending the requests to whatever was the first VirtualHost definition it found when reading the Apache configuration files. Thus if this is not the first VirtualHost definition, it will never be used. If there is no similar URL handler set up in the first VirtualHost, you will get a 404.
The second is your WSGI application entry point in the WSGI script file pointed at by the WSGIScriptAlias directive, is not called application you will get a 404. You do appear to have it being called application, so you should be fine on this point and it shouldn't be the issue. There would have been a distinctive error message in the Apache error logs of this was the issue anyway.
The third is that the URL path you are using doesn't map to a route in the Flask application. There are actually two part to this. Because you are mounting at a sub URL in Apache, the URL path must at least start with /flask2. With that value it means you need to have a route in your Flask application which matches the root of the site. Normally in Flask that would mean you have a route for #app.route('/') but am not sure whether that still works when you have mounted your Flask application at a sub URL in Apache. You don't show your route code, so can't see what you have. You might at least try instead using /flask2/ in the URL. If your video feed isn't at the root of the Flask application but a sub URL such as set up by #app.route('/video_feed') as given in the post you link, then you should be using /flask2/video_feed as URL path. If you don't use the correct URL you will get a 404.
My whole configuration of files were right but as i was running flask applications under apache server ,apache was not able to acess pi camera because it was not having root access to pi.so,on local server it was working but not on my website when it was running on apache.

Django with mod_wsgi-express and Apache displays Apache test page instead of site home page

I'm trying to setup a Django based website on a CentOS 6.7 Linux PaaS server VM.
I've installed Python 2.7.8 in a virtualenv.
I'm using Apache 2.2.15 (installed using yum), Django 1.7 (installed using pip2.7) and mod_wsgi-express version 4.4.21 (that I compiled from source).
I've followed the instructions at https://pypi.python.org/pypi/mod_wsgi
and am at a point where this command, issued at the Django project root directory, works for me:
(vishwaas_env)$sudo python manage.py runmodwsgi --host=0.0.0.0 --port=80 --user apache --group apache --server-root=/home/syt_admin/projects/vishwaas/www
I'm able to point my browser to the public ip address of the VM and connect to my site. The error_log located in my server-root directory shows normal operation:
[Thu Dec 03 10:48:22 2015] [notice] Apache/2.2.15 (Unix) mod_wsgi/4.4.21 Python/2.7.8 configured -- resuming normal operations
However, since I would like to start this as a background task, I followed Graham's answer to this question:
how can run django on centos using wsgi
I've setup the server configs:
(vishwaas_env)$ python manage.py runmodwsgi --setup-only --host=0.0.0.0 --port=80 --user apache --group apache --server-root=/home/syt_admin/projects/vishwaas/www
and started the apachectl script in the server-root directory
(vishwaas_env)$ sudo apachectl start
But this time, pointing the browser to the same ip address displays the Apache 2 Test page. Also, this time, the error_log that contains messages is the one at
/var/log/httpd/error_log
[Thu Dec 03 10:50:52 2015] [notice] suEXEC mechanism enabled (wrapper: /usr/sbin/suexec)
[Thu Dec 03 10:50:52 2015] [notice] Digest: generating secret for digest authentication ...
[Thu Dec 03 10:50:52 2015] [notice] Digest: done
[Thu Dec 03 10:50:52 2015] [notice] Apache/2.2.15 (Unix) DAV/2 configured -- resuming normal operations
[Thu Dec 03 10:51:03 2015] [error] [client <ip address>] Directory index forbidden by Options directive: /var/www/html/
What am I missing?
Just realized my mistake.
$sudo apachectl start
was executing the script at /usr/sbin/apachectl
and not the one in my local directory.
$sudo ./apachectl start
invoked the one with the correct configs. I need to figure out what is missing in my path definition.

Apache on Amazon EC2 Not Connecting To My Django Site

I have an Amazon EC2 instance running Ubuntu and I'm trying to get it to display my Django site. The dev server works (i.e. if I run the dev server on the instance and go to http://ec2-XX-XXX-XX-XX.us-west-2.compute.amazonaws.com/:8000, my site works fine) but I can't get it to run properly via apache.
I've read about a thousand tutorials and so far none have been successful. Here is my most recent attempt - my Django site is stored in /home/ubuntu/Amazon/repo/my_site/.
I've tried creating a virtual host file in /etc/apache2/sites-available/ec2.conf with the following:
<VirtualHost *:80>
ServerName ec2-XX-XXX-XX-XX.us-west-2.compute.amazonaws.com
ServerAlias *.compute.amazonaws.com
WSGIScriptAlias / /home/ubuntu/Amazon/repo/my_site/my_site/apache/apache.wsgi
<Directory />
Require all granted
</Directory>
</VirtualHost>
After that, I ran sudo a2ensite ec2 to add the conf file to sites-enabled but going to ec2-XX-XXX-XX-XX.us-west-2.compute.amazonaws.com in my browser still returns to the default page.
Any guidance would be great, I'm sure I'm missing several steps, but I'm completely lost at the moment. There are a lot of things I don't understand at the moment such as:
Do I need a /var/www/html/ directory somewhere? Or does Django handle that?
Do I need to use virtual hosts at all? Or is that only for subdomains?
Do I need to register a domain name prior to getting it working? All the examples I see use something like "example.com" where I have "ec2-XX-XXX-XX-XX.us-west-2.compute.amazonaws.com". I have the domain name registered but I haven't pointed it at my amazon instance yet - I was hoping to first get it working.
Edit: Apache error.log output:
[Tue Jul 08 16:07:47.983174 2014] [mpm_event:notice] [pid 5054:tid 140062636054400] AH00491: caught SIGTERM, shutting down
[Tue Jul 08 16:07:49.046666 2014] [mpm_event:notice] [pid 5211:tid 139882510350208] AH00489: Apache/2.4.7 (Ubuntu) mod_wsgi/3.4 Python/2.7.6 configured -- resuming normal operations
[Tue Jul 08 16:07:49.046735 2014] [core:notice] [pid 5211:tid 139882510350208] AH00094: Command line: '/usr/sbin/apache2'

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