WSGI problem on Eleastic Beanstalk with Django - django

Some days ago I have deployed a Django on AWS elastic beanstalk(EB) and it worked fine. Today, after a new deploy, where I did minor changes in view.py, the Django APP on EB has a very big problem and it becomes not accessible anymore. Looking at the log file in AWS EB I read this errors:
Script timed out before returning headers: wsgi.py
End of script output before headers: wsgi.py, referer ...
Do you have any ideas how to solve this issues?
I would like to thank you in advance,

Two potential solutions (depending on your specific situation)
This is a similar question in addressing this specific question
End of script output before headers: wsgi.py deploying python django to AWS EB
Add this WSGIApplicationGroup %{GLOBAL} to your wsgi configuration. It directs your wsgi app- your django app, to "run within the very first Python sub interpreter created when Python is initialised" (from https://modwsgi.readthedocs.io/en/develop/user-guides/application-issues.html#python-simplified-gil-state-api)
The other solution is related to increaseing memory of the instance you are using.

Related

How to fix Django "ModuleNotFoundError: No module named 'application'" on AWS?

I am trying to redeploy a Django web application on AWS. My elastic beanstalk environment has been red a couple of times. When I ran eb logs on the cli, I am getting a "ModuleNotFoundError: No module named 'application' error". I think this has got to do with my wsgi configuration.
I have deployed this web app on AWS before. I messed up when I tried deploying a new version then decided to just start over. Here is my wsgi.py configuration:
```import os
from django.core.wsgi import get_wsgi_application
from django.contrib.staticfiles.handlers import StaticFilesHandler
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
application = StaticFilesHandler(get_wsgi_application())```
When I deploy the app, it's giving me a 502: Bad gateway error. Let me know if you would like more info on the issue. Any pointers would be greatly appreciated.
By default, Elastic Beanstalk looks for a file named application.py to start your application. Because this doesn't exist in the Django project that you've created, you need to make some adjustments to your application's environment. You also must set environment variables so that your application's modules can be loaded.
follow these instructions:
https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create-deploy-python-django.html#python-django-configure-for-eb
I got this error because in an older version the django.config looked like this:
option_settings:
aws:elasticbeanstalk:container:python:
WSGIPath: ebdjango/wsgi.py
and now it should look like this:
option_settings:
aws:elasticbeanstalk:container:python:
WSGIPath: ebdjango.wsgi:application
I faced this issue as well. In my case I was getting a 502 nginx error after deploying to AWS EB using eb deploy and my environment had a red flag.
My AWS EB was using Amazon Linux 2 AMI, everything was set correctly BUT after lot of tries and errors I realized my .ebextensions folder was named .ebtextensions.
I wish EB or django would have indicated on its logs that my folder was nanmed incorrectly.
I got this error and also spent days trying to solve it. The problem is from the django.config file.
option_settings:
aws:elasticbeanstalk:container:python:
WSGIPath: <app name>.wsgi:application
The first value (app name) in the WSGIPath option needs to be the directory that contains the wsgi.py file
Check the WSGIPath in elastic beanstalk configuration (Software Category) in the AWS console too.
It should be
<app_name>.wsgi:application

Running django-q using elastic beanstalk on aws linux 2 instances

I use Elastic Beanstalk on aws to host my webapp which needs a task runner like django q. I need to run it on my instance and am facing difficulty doing that. I found this script https://gist.github.com/codeSamuraii/0e11ce6d585b3290b15a9ad163b9aa06 which does what I need but its for the older version of ec2 instance. So far I know I must run django q post deployment, but is it possible to add the process to the procfile along with starting the wsgi server.
Any help that could point me in the right direction will be greatly appreciated.
You can create a "Procfile" at the root of your bundle with following content:
web: gunicorn --bind 127.0.0.1:8000 --workers=1 --threads=15 mysite.config.wsgi:application
qcluster: python3 manage.py qcluster
Obviously, replace "mysite.config.wsgi" with the path to your wsgi.
I ended up not finding a solution, i chose a different tech altogether to fulfill the requirements. It was a crontab making curl requests to a Django server. So on the Django admin I would create task routes linking it to modules in the file storage. And paste the route info in crontab setting and set the appropriate time interval.

Django Webfaction 'Timeout when reading response headers from daemon process'

My Django app on my production server hosted on Webfaction was working fine until I just tried to restart it after pushing a change to the settings.py file. I ran
apache2/bin/restart
as usual. Then I tried to access my app on my browser, and I got a 504 Gateway timeout. I looked into the mod_wsgi logs and saw this:
[Thu Nov 03 23:46:53.605625 2016] [wsgi:error] [pid 8027:tid 139641332168448]
[client 127.0.0.1:34570] Timeout when reading response headers from daemon
process 'myapp' : /home/<me>/webapps/<myapp>/<ProjectName>/<myapp>/wsgi.py
What does this mean and how do I fix it? The only thing I changed in the settings.py file was moving some variable names around. I can still successfully interact with the app with
python2.7 manage.py shell
But I can't get to it on the web, nor use the API.
EDIT: Here's my wsgi.py file:
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "<myapp>.settings")
application = get_wsgi_application()
Python C extension modules, like numpy, are known to cause timeouts when used under mod_wsgi. There's a clear explanation of the problem (direct from the author of mod_wsgi) available at https://serverfault.com/a/514251/109598
If that sounds like it might be the cause of your problem, then the solution is probably simple - add the following to your httpd.conf:
WSGIApplicationGroup %{GLOBAL}
Be sure to restart your Apache instance after making that change.
Try increasing Timeout directive in httpd.conf, which defaults to 60 seconds in Apache 2.4. For example:
TimeOut 600
Here is how I was able to find the root cause of my issue.
python manage.py showmigrations
My app could not reach the database server, so it would eventually time out. Running manage.py I could see see the error message on the console.
In my case (Python 3.6), the mimetypes module caused this problem. I did not further investigate this, but removing a call to mimetypes.guess_type solved the problem. The call was made in the related Django view function.
I hit the same problem because the home directory of the user under which the wsgi process was running had became unavailable at some point during the server upgrade.
This might help someone.
Thank you to lenhhoxung who in the comments to one of the other solutions mentioned upgrading server capabilities. I had been successfully running a demo site on an AWS EC2 Nano instance for a long time, but for some reason it suddenly started erroring out on one page that has some complex computations. I upgraded it to an AWS EC2 Micro instance, problem solved. I think this is worthy of its own answer here considering this took a good chunk of a day. All credit to lenhhoxung, though! Thanks!

Django deployment with mod_wsgi and .htaccess

Here is my set-up: Django 1.7; Python 3.4, mod_wsgi, virtualenv, apache2. Django and other pakages are installed after activating the virtualenv.
Need .htaccess file for redirects. I am new to deployment world, so please bear with me.
Here is what I have done so far:
In the virtualhost, I call the wsgi.py file after specifying the path to site-package path for Django. This works fine, however I have 2 questions:
1) I saw a lot of sites which were calling the file.wsgi file from the virtualhost config, and in the .wsgi file they were calling the exec to activate the env and call django app. The Django guide however has a different approach (https://docs.djangoproject.com/en/1.8/howto/deployment/wsgi/modwsgi/) and this is the one I implemented. Do I need a file.wsgi if I want to have htaccess deal with the request and then forward it to Django urls.py?
2) If the set-up that I have is ok for deployment, where do i place the .htaccess file and ensure that apache sends the request to htaccess which in turn sends it to Django app?
Thanks

Django virtualenv deployment configuration

I recently start to use virtualenvwrapper and created
mkdir ~/.virtualenvs
mkvirtualenv example.com
Virtualenvwarpper automatical create a virtualenv named example.com under ~/.virtualenv
so this is the central container for all virtualenvs.
After than I installed django and some other packages via pip
and my site is at
/srv/www/example.com/public_html/
Do I have to put my site to
~/.virtualenv/example.com
if not how could I use my example.com virtualenv with my site under /srv/www/example.com/public_html.
Could you show me an apache mod_wsgi configuration for this deployment?
Thanks
Read:
http://code.google.com/p/modwsgi/wiki/VirtualEnvironments
It may not be sufficient to use just site.addsitedir() as it doesn't deal with certain ordering issues. You are better off using the configuration directive/option provided by mod_wsgi to add them. Otherwise, if the ordering becomes an issue you will need to add code into WSGI script that reorders sys.path as necessary.
In your WSGI script:
import site
site.addsitedir('/home/username/.virtualenvs/example.com/lib/python2.5/site-packages')
(Adjust as appropriate for your Python version, etc.)