serve multiple gunicorn django instances under nginx ubuntu - python

I need to serve up two webs apps (Django) on gunicorn under different domain names using nginx on ubuntu. I started using this tutorial:
https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-16-04
It worked just fine for one using that. Then I tried to do the same thing for the second but it gives me a 502 gateway for the second domain. So I tried following this tutorial:
http://michal.karzynski.pl/blog/2013/10/29/serving-multiple-django-applications-with-nginx-gunicorn-supervisor/
I'm at the part where you run the gunicorn start script:
sudo bin/gunicorn_start
I get back the response:
Starting webuildblack as root
Traceback (most recent call last):
File "/home/devin/webuildblack/bin/gunicorn", line 7, in <module>
from gunicorn.app.wsgiapp import run
ImportError: No module named 'gunicorn.app'; 'gunicorn' is not a package

You have named your program gunicorn.py like the gunicorn-package. Rename your this file and remove any gunicorn.pyc file.

Related

'socket' object has no attribute 'sendfile' while sending a file in flask + gunicorn + nginx + supervisor setup

Using flask, I'm trying to send a file to the user on clicking a button in UI using send_from_directory function. It used to work fine. I wanted to change the repo and since changing it, I'm no more able to download the file. On looking at the supervisor log, I see this:
[9617] [ERROR] Error handling request
Traceback (most recent call last):
File "path_to_file/venv/lib/python3.4/site-packages/gunicorn/workers/sync.py", line 182, in handle_request
resp.write_file(respiter)
File "path_to_file/venv/lib/python3.4/site-packages/gunicorn/http/wsgi.py", line 385, in write_file
if not self.sendfile(respiter):
File "path_to_file/venv/lib/python3.4/site-packages/gunicorn/http/wsgi.py", line 375, in sendfile
self.sock.sendfile(respiter.filelike, count=nbytes)
AttributeError: 'socket' object has no attribute 'sendfile'
In the same repo, this works fine locally. But when trying in remote server using the gunicorn + supervisor + nginx setup, I get the above error message. I do get 200 Ok response in the application log file. Spent a lot of time trying to fix but without success.
Also, the notable difference between the working app between the previous repo and the non-working current repo is the python version. Previous: python2.7, Current: python3.4
For me, usually when a script works locally but not when hosted its one (or more) of these possibilities:
Location / path to the files is different
older version of python
older version of the library
Pointing the virtual env to Python 3.6 and upgrading all the relevant libraries including Flask resolved the issue.

Why won't the runserver command in the terminal run? I am (a complete beginner) working on building a web app (learning log) with Django

So I've already created and activated a virtual environment, created a project, and an app (but the app shouldn't really matter at this point). I'm trying to view my project and ran the server command but it won't run. To clarify, I had already made more progress with the web app: created a model and migrated it so i could use it in the app, viewed the project as an admin to see the model working but when i created the second model, i had trouble migrating it. so i took a few steps back to the point where I could just view the empty project and that also didn't work so i'm confused. I could just delete and start over but I'd like to know what's wrong at this point. Below is what the terminal shows when I tried to run 'runserver' command so I could view the project on my local server.
C:\Users\acer>cd Desktop/programming/learning_log
C:\Users\acer\Desktop\programming\learning_log>ll_env\Scripts\activate
(ll_env) C:\Users\acer\Desktop\programming\learning_log>python manage.py runserver
Traceback (most recent call last):
File "manage.py", line 10, in main
from django.core.management import execute_from_command_line
ModuleNotFoundError: No module named 'django'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 21, in <module>
main()
File "manage.py", line 12, in main
raise ImportError(
ImportError: Couldn't import Django. Are you sure it's installed and available on your PYTHONPATH environment variable? Did you forget to activate a virtual environment?
(ll_env) C:\Users\acer\Desktop\programming\learning_log>
Again, I had already managed to view the project through the admin site and even enter a topic (since I created a model for Topic) so obviously django is installed or at least was, and I did not delete any files or folders.

Won't import local file as module

I'm setting up a Flask application on Digitalocean and have Python 3.7 installed and the latest version of Flask. When running the app inside a virtualenv and trying to run the application using python3.7 application.py I get the following error message:
Traceback (most recent call last):
File "application.py", line 11, in <module>
from config import *
ModuleNotFoundError: No module named 'config'
What puzzles me is that config.py is located in the same folder as application.py, and not in a subfolder. I have duplicated the setup on my local machine, also running Python 3.7 and inside a virtualenv, and the importing (and the app) works flawlessly.
I've tried importing "config.py" instead of just "config" but didn't make a difference. I also tried specifying exactly what it should import (instead of using '*') but that didn't make a difference either.
Your thoughts on why it can't find config?
What seems to have been the solution to my problem above was to run the Python shell and add the path to the directory in which config.py is located (even though it's in the same folder as application.py...) by using the following command:
sys.path.append("/path/to/config/")

Why running flask application using python instead of Flask-CLI produces ImportError?

When I run my application using flask-cli, the app starts normally.
I set HOST and PORT env variables and run it like:
flask run --host ${HOST} --port ${PORT} --no-reload --no-debugger
PROBLEM:
When I run it like python3.6 main.py, I get some import errors (which I don't get using flask-cli):
Traceback (most recent call last):
File "main.py", line 21, in <module>:
from runn.models import User, Town
File "/home/dinko/my-app/runn/models.py", line 9, in <module>
from main import db
File "/home/dinko/my-app/runn/main.py", line 21, in <module>
from runn.models import User, Town
ImportError: cannot import name `User`
I know it maybe due to the circular imports, but I'm not sure how it works using flask-cli. Is there any solution to run it like with flask-cli, but using python3.6 main.py instead ?
I managed to find a way how to run my flask app by using python -m flask run.

How can I permanently add a module to my flask app?

yes I know my question sounds like a duplicate but I have tried everything I have found.
I am trying to add a module to my python flask app. This module is located at ../../clients/api relative to my flask app. I haven't had any problems adding a module before, but for some reason this just doesn't want to work.
So far, I have tried:
sys.path.append("mypath") (worked temporarily but as soon as the shell was closed it disappeared)
adding a .pth file in lib/site-packages
adding it directly to my environment variables
adding in in my IDE (Wing 101)
Aside from 1., none of them had any effect whatsoever.
$ flask run
* Serving Flask app "main.py"
* Environment: production
WARNING: Do not use the development server in a production environment.
Use a production WSGI server instead.
* Debug mode: off
Usage: flask run [OPTIONS]
Error: While importing "main", an ImportError was raised:
Traceback (most recent call last):
File "c:\python37-32\lib\site-packages\flask\cli.py", line 235, in locate_app
__import__(module_name)
File "my_flask_app_path", line 20, in <module>
from clients.api import Client as client
ModuleNotFoundError: No module named 'api'
Running this on python v-3.7.1, Windows 10, both git bash and cmd. Please try not to be blunt :)

Categories