unable to load configuration from app:app - flask

I am trying to run flask app server with uwsgi from supervisor and i getting error
[program:tmanalytics]
directory = /srv/www/tmapi/
command = uwsgi --socket /tmp/tmuwsgi.sock app:app --chmod-socket 777 --touch-reload /srv/www/tmapi/deployment.log
watch=/srv/www/tmapi/app/
stdout_logfile=/srv/www/tmapi/tmapi-analytics.log
autostart=true
autorestart=true
redirect_stderr=true
stopsignal=QUIT

try this
command = uwsgi -s /tmp/tmuwsgi.sock -w app:app --chmod-socket=666 --touch-reload /srv/www/tmapi/deployment.log

Related

How to run multiple manage.py using docker file

I have Django application which has docker file and using below command to bring up the application
CMD ["gunicorn", "--bind", ":8000", "--workers", "3", "--worker-class", "uvicorn.workers.UvicornWorker", "myapplication.asgi:application"]
Would like to run one more process "task_process" which is present in the below directory structure
taskApp/
__init__.py
models.py
management/
__init__.py
commands/
__init__.py
task_process.py
How to execute task_process after loading an application ?
I tried the below shell script commands in startup.sh. But no luck
#!/bin/sh
exec gunicorn --bind :8000 --workers 3 --worker-class uvicorn.workers.UvicornWorker myapplication.asgi:application &
exec python manage.py task_process
#!/bin/sh
exec gunicorn --bind :8000 --workers 3 --worker-class uvicorn.workers.UvicornWorker myapplication.asgi:application &
python manage.py task_process
#!/bin/sh
gunicorn --bind :8000 --workers 3 --worker-class uvicorn.workers.UvicornWorker myapplication.asgi:application &
python manage.py task_process
#!/bin/sh
gunicorn --bind :8000 --workers 3 --worker-class uvicorn.workers.UvicornWorker myapplication.asgi:application
python manage.py task_process
Updated existing Docker file as below
RUN chmod +x startup.sh
CMD ["./startup.sh"]

django on nginx + uwsgi / static files don't show

So I want to run django on nginx with uwsgi. I installed everything, changed the ALLOWED_HOSTS and STATIC_ROOT in the settings.py and ran "collectstatic". With the terminal I can see that the admin-static-files were put exactly where I wanted them to be put. Yet I don't see the static files when I go to domainxy.de/admin . After my the installation I did this:
sudo mkdir -p /etc/uwsgi/sites
sudo nano /etc/uwsgi/sites/mysite.ini
with
####mysite.ini####
[uwsgi]
project = mysite
uid = root
base = /%(uid)
chdir = %(base)/%(project)
module = %(project).wsgi:application
master = true
processes = 5
socket = /run/uwsgi/%(project).sock
chown-socket = %(uid):www-data
chmod-socket = 660
vacuum = true
and
sudo nano /etc/systemd/system/uwsgi.service
with
###uwsgi.service####
[Unit]
Description=uWSGI Emperor service
[Service]
ExecStartPre=/bin/bash -c 'mkdir -p /run/uwsgi; chown root:www-data /run/uwsgi'
ExecStart=/usr/local/bin/uwsgi --emperor /etc/uwsgi/sites
Restart=always
KillSignal=SIGQUIT
Type=notify
NotifyAccess=all
[Install]
WantedBy=multi-user.target
and
sudo nano /etc/nginx/sites-available/mysite.conf
with
####mysite.conf####
server {
listen 80;
server_name domainxy.de www.domainxy.de xx.xxx.xxx.xxx;
location = /favicon.ico { access_log off; log_not_found off; }
location /static {
root /root/mysite/;
}
location / {
include uwsgi_params;
uwsgi_pass unix:/run/uwsgi/mysite.sock;
}
}
and
sudo ln -s /etc/nginx/sites-available/mysite.conf /etc/nginx/sites-enabled
sudo systemctl restart nginx
sudo systemctl start uwsgi
sudo ufw allow 'Nginx Full'
Where did I make the mistake? I did change the folders in "mysite.conf" multiple times but nothing helped so far. Thanks for helping. Don't judge me that I am a newbie and also not that I used the root-user for simplifying here.
Edit: my django-project is in /root/mysite/ and the settings.py is
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "static/")

How to setup nginx and django with docker-compose?

I'm having a situation while setting up Django and all the dependencies I need with Docker (docker-toolbox, docker-compose).
I'm encountering an error while I'm trying to access to my url http://192.168.99.100:8000/ which says 502 Bad Gateway (nginx/1.13.1). For this error I don't really understand from where it comes since it's the first time I'm using Django with nginx on Docker.
docker-compose.yml :
version: '2'
services:
nginx:
image: nginx:latest
container_name: nz01
ports:
- "8000:8000"
volumes:
- ./src:/src
- ./config/nginx:/etc/nginx/conf.d
- /static:/static
depends_on:
- web
web:
...
...
Dockerfile :
FROM python:latest
ENV PYTHONUNBUFFERED 1
#ENV C_FORCE_ROOT true
ENV APP_USER myapp
ENV APP_ROOT /src
RUN mkdir /src;
RUN groupadd -r ${APP_USER} \
&& useradd -r -m \
--home-dir ${APP_ROOT} \
-s /usr/sbin/nologin \
-g ${APP_USER} ${APP_USER}
WORKDIR ${APP_ROOT}
RUN mkdir /config
ADD config/requirements.pip /config/
RUN pip install -r /config/requirements.pip
USER ${APP_USER}
ADD . ${APP_ROOT}
config/nginx/... .conf
upstream web {
ip_hash;
server web:8000;
}
server {
location /static/ {
autoindex on;
alias /static/;
}
location / {
proxy_pass http://web/;
}
listen 8000;
server_name localhost;
}
Is there something I'm doing wrong ?
The issue here is with gunicorn command line argument, missing -b flag. It needs to be launched with -b flag for binding with the address specified. In your case gunicorn binds itself to default 127.0.0.1:8000 which isn't accessible to other containers. So, just change the command for web to following:
command: bash -c 'python manage.py makemigrations && python manage.py migrate && gunicorn oqtor.wsgi -b 0.0.0.0:8000'
That should make your web app accessible via nginx.

uWSGI configuration with FlaskApp

In effort to switch to Nginx, I'm running into configuration problems and getting a 502 Gateway error. Here is error log on connection:
tail -f error.log
2016/10/08 16:09:31 [crit] 21682#21682: *29 connect() to unix:/var/www/FlaskApp/FlaskApp/runserver.sock failed (13: Permission denied) while connecting to upstream, client: 73.188.249.47, server: ceejcalvert.com, request: "GET / HTTP/1.1", upstream: "uwsgi://unix:/var/www/FlaskApp/FlaskApp/runserver.sock:", host: "website.com"
If I'm in the terminal on the server I can get the site up by manually directing the socket via:
This command will get everything working if run:
uwsgi -s /var/www/FlaskApp/FlaskApp/runserver.sock -w runserver:app --chmod-socket=666
The issue is I cannot get it working in daemon mode. My configuration is as follows:
$ cat /etc/systemd/system/runserver.service
[Unit]
Description=uWSGI instance to serve runserver
After=network.target
[Service]
User=username
Group=www-data
WorkingDirectory=/var/www/FlaskApp/FlaskApp
Environment="PATH=/var/www/FlaskApp/FlaskApp/venv/bin"
ExecStart=/var/www/FlaskApp/FlaskApp/venv/bin/runserver.sock --ini runserver.ini
[Install]
WantedBy=multi-user.target
...
cat /var/www/FlaskApp/FlaskApp/runserver.ini
[uwsgi]
module = wsgi:app
master = true
processes = 5
logto = /home/jmc856/error.log
socket = runserver.sock
chmod-socket = 666
vacuum = true
die-on-term = true
Assume site-available is linked to sites-enabled
cat /etc/nginx/sites-available/runserver
server {
listen 80;
server_name website.com;
location / {
include uwsgi_params;
uwsgi_pass unix:/var/www/FlaskApp/FlaskApp/runserver.sock;
}
}
Anything obvious I'm missing?
When I run the following, I get exit code 3.
sudo systemctl status runserver
runserver.service - uWSGI instance to serve runserver
Loaded: loaded (/etc/systemd/system/runserver.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Sat 2016-10-08 16:08:45 EDT; 20min ago
Main PID: 22365 (code=exited, status=203/EXEC)
Oct 08 16:08:45 FirstNameLastName.com systemd[1]: Stopped uWSGI instance to serve runserver.
Oct 08 16:08:45 FirstNameLastName.com systemd[1]: Started uWSGI instance to serve runserver.
Oct 08 16:08:45 FirstNameLastName.com systemd[1]: runserver.service: Main process exited, code=exited, status=203/EXEC
Oct 08 16:08:45 FirstNameLastName.com systemd[1]: runserver.service: Unit entered failed state.
Oct 08 16:08:45 FirstNameLastName.com systemd[1]: runserver.service: Failed with result 'exit-code'.
Solved my issue. For the most part my configurations were fine. Here is a checklist of things to ensure if you get 502 gateway errors.
1) I first added all absolute paths to config files. For example I changed my systemd config to:
$ cat /etc/systemd/system/runserver.service
[Unit]
Description=uWSGI instance to serve runserver
After=network.target
[Service]
User=username
Group=www-data
WorkingDirectory=/var/www/FlaskApp/FlaskApp
Environment="PATH=/var/www/FlaskApp/FlaskApp/venv/bin"
ExecStart=/var/www/FlaskApp/FlaskApp/venv/bin/runserver.sock --ini /var/www/FlaskApp/FlaskApp/runserver.ini
[Install]
WantedBy=multi-user.target
2) Changed .ini file to directly call uWSGI app:
cat /var/www/FlaskApp/FlaskApp/runserver.ini
[uwsgi]
chdir=/var/Webpage/
wsgi-file = wsgi.py
callable = app
master = true
processes = 5
logto = /home/error.log
socket = runserver.sock
chmod-socket = 666
vacuum = true
die-on-term = true
3) Ensure FlaskApp host is 0.0.0.0:
if __name__ == "__main__":
app.run(host='0.0.0.0')
4) Use these commands to try to find out where things are failing.
Make sure configs have proper syntax
$ sudo nginx -t
Make sure nginx daemon running properly
$ systemctl status nginx.service
Ensure uWSGI instance to serve {app} is running.
$ systemctl
If all is good and still finding errors, search for failure in
$ sudo journalctl
And
$ sudo tail -f /var/log/nginx/error.log
If everything is running properly, make sure you perform the following:
$ sudo systemctl restart {app}
$ sudo start {app}
$ sudo enable {app}
That last command was something I forgot and prevented me from realizing my configuration was fine for a long time. In my case {app} was 'runserver'
I've been using this Docker image for all my nginx and flask apps
https://github.com/tiangolo/uwsgi-nginx-flask-docker

502 Bad Gateway - django + nginx + gunicorn - sock failed (13: Permission denied)

I am following this tutorial: https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-14-04
I believe everything else in my setup is correct. The file /var/log/nginx/error.log has many of the following error:
2015/12/19 18:41:58 [crit] 10850#0: *23 connect() to unix:/home/root/myproject/myproject.sock failed (13: Permission denied) while connecting to upstream, client: [[my ip]], server: [[server ip]], request: "GET / HTTP/1.1", upstream: "http://unix:/home/root/myproject/myproject.sock:/", host: "[[server ip]]"
The command:
/home/root/myproject ls -l /home/root/classNote/classNote.sock
outputs:
srwxrwxrwx 1 root www-data 0 Dec 19 18:17 /home/root/myproject/myproject.sock
EDIT: Andrei's comment's response:
the command:
ps ax | grep gunicorn
outputs:
847 ? Ss 0:00 /home/root/myproject/myprojectEnv/bin/python myprojectEnv/bin/gunicorn --workers 3 --bind unix:/home/root/myproject/myproject.sock myproject.wsgi:application
921 ? S 0:00 /home/root/myproject/myprojectEnv/bin/python myprojectEnv/bin/gunicorn --workers 3 --bind unix:/home/root/myproject/myproject.sock myproject.wsgi:application
923 ? S 0:00 /home/root/myproject/myprojectEnv/bin/python myprojectEnv/bin/gunicorn --workers 3 --bind unix:/home/root/myproject/myproject.sock myproject.wsgi:application
928 ? S 0:00 /home/root/myproject/myprojectEnv/bin/python myprojectEnv/bin/gunicorn --workers 3 --bind unix:/home/root/myproject/myproject.sock myproject.wsgi:application
1136 pts/0 S+ 0:00 grep gunicorn
link to similar question
I just ran into this problem. I was able to create the gunicorn socket file, but nginx complained about permission denied. The issue was that my socket file was in a sub-folder and the root folder did not have read or execute permissions. So even though the sub-folder had the correct permissions, the root folder prevented nginx from entering the sub-folder.
The solution was to add read and execute permissions to the root folder:
chmod o+rx /example_root_folder
Seems there are permission issues start your nginx service as root because root has permission to access your my project.sock
sudo service nginx stop
Then
sudo service nginx start
But it is not a good idea to start nginx as root. You may try changing permission of your current user
I can suggest you an alternative which works fine.
Let a shell script handle all this.
Create a shell script like this (* indicates required in comments)
#!/bin/bash
NAME="" #Name of the application (*)
DJANGODIR=/path/to/django/project # Django project directory (*)
SOCKFILE=/path/to/socket/file/myproject.sock # we will communicate using this unix socket (*)
USER= # the user to run as (*)
GROUP= # the group to run as (*)
NUM_WORKERS=1 # how many worker processes should Gunicorn spawn (*)
DJANGO_SETTINGS_MODULE=yourproject.settings # which settings file should Django use (*)
DJANGO_WSGI_MODULE=yourproject.wsgi # WSGI module name (*)
echo "Starting $NAME as `whoami`"
# Activate the virtual environment
cd $DJANGODIR
source /path/to/virtualenv/bin/activate
export DJANGO_SETTINGS_MODULE=$DJANGO_SETTINGS_MODULE
export PYTHONPATH=$DJANGODIR:$PYTHONPATH
# Create the run directory if it doesn't exist
RUNDIR=$(dirname $SOCKFILE)
test -d $RUNDIR || mkdir -p $RUNDIR
# Start your Django Unicorn
# Programs meant to be run under supervisor should not daemonize themselves (do not use --daemon)
exec /path/to/virtualenv/bin/gunicorn ${DJANGO_WSGI_MODULE}:application \
--name $NAME \
--workers $NUM_WORKERS \
--user $USER \
--bind=unix:$SOCKFILE
I had a very similar problem like you before (also proceeded according to the mentioned tutorial on the digitalocean.com), by this I mean the socket was not working and also I was getting the 502 error.
The solution for me was to move from socket binding to binding a normal IP address.
Inside of a virtualenv (next to "manage.py") I have a gunicorn.py file: (NOTE: I use 3.4 version)
#!/usr/bin/python3.4
"""
Run Gunicorn (Django) on a specific IP addr
"""
import os
# Change directory to the virtualenv folder
os.chdir("/home/your_path/name_of_env/")
# Run Gunicorn
os.system("bin/gunicorn -w 3 -b 127.0.0.1:8000 your_project.wsgi:application &")
The script runs Gunicorn without activating the virtualenv (command "bin/gunicorn") and in background ("&").
NOTE: If you don't use virtualenv than remove "bin/" from the script above.
For calling that script after booting PC and login, add this line to your ".profile" file at the end. (It's inside your home directory)
python3.4 path/to/the/file/gunicorn.py
In a nginx file you should have this part of code:
location / {
proxy_pass http://127.0.0.1:8000;
proxy_set_header Host $host;
proxy_set_header X-Forwarder-For $proxy_add_x_forwarded_for;
}
Remember, the IP addr 127.0.0.1:8000 is reserved. So if you want to run Django developing server, you have 2 options:
Turn off Gunicorn by commands "ps aux | grep gunicorn" and "kill -9 [PID]" and then normally run a command "python3.4 manage.py runserver"
Or run Django developing server on a different IP and port, for example "python3.4 manage.py runserver 0.0.0.0:8001"
This solution works for me.