Django with Unicorn and Nginx, creating gunicorn logs - python

I Launch my Django app in a docker container, using a startup script as per the below:-
#!/bin/bash
python manage.py collectstatic --noinput
python manage.py makemigrations
python manage.py migrate
/etc/init.d/celeryd start
/etc/init.d/celerybeat start
exec gunicorn itapp.wsgi -b 0.0.0.0:8000
Unicorn and ngnix every now and then give me a 502 error, so I went to look for logs and it looks by default gunicorn does not log any? im on version 19.7.1
so I added the to the exec guncicorn command:
exec gunicorn itapp.wsgi -b 0.0.0.0:8000 --error-logfile /var/log/gunicorn/errors.log , --log-file /var/log/gunicorn/access.log
Which I goy form the gunicorn documentation. however now Gunicorn fails to launch with the following error:-
usage: gunicorn [OPTIONS] [APP_MODULE]
gunicorn: error: unrecognized arguments: ,
How can I create the gunicorn logs to debug these errors?
Thanks

Remove comma and you should be fine:
exec gunicorn itapp.wsgi -b 0.0.0.0:8000 --error-logfile /var/log/gunicorn/errors.log --log-file /var/log/gunicorn/access.log

Related

environment variable issue with procfile heroku

I am doing a Heroku tutorial and web: python manage.py runserver 0.0.0.0:$PORT creates the error when I run heroku local or heroku local -p 5000 (or one of several more variants). However, web: python manage.py runserver 0.0.0.0:5000 works fine. I suspect I am making a simple error with how to pass an environment variable into the Procfile.
The error message is: CommandError: "0.0.0.0:$PORT" is not a valid port number or address:port pair.
The problem was a difference between Windows and Linux. The solution is below for others' benefit.
on linux, $PORT references the variable called PORT
on windows, we instead need %PORT%
Hence, web: python manage.py runserver 0.0.0.0:%PORT% works for Procfile.windows. To run the Windows specific Procfile, run heroku local web -f Procfile.windows as the normal Procfile file should be left with web: python manage.py runserver 0.0.0.0:$PORT so it works when deployed (as heroku machines use linux)

How to deploy a Flask application with Gunicorn and Nginx

Ok, I'm new to Python/Flask deployment and was following this tutorial.
This is my system file:
[Unit]
Description=Gunicorn instance to serve myapp
After=network.target
[Service]
User=deployer
Group=www-data
WorkingDirectory=/home/deployer/myapp
Environment="PATH=/home/deployer/myapp/myapp_env/bin"
ExecStart=/home/deployer/myapp/myapp_env/bin/gunicorn --workers 3 --bind unix:myapp.sock -m 007 appserver:gunicorn_app
[Install]
WantedBy=multi-user.target
But it doesn't work. I get Main process exited, code=exited, status=203/EXEC or /root/myapp/myapp_env/bin/python3: bad interpreter: Permission denied
If I cd into my myapp directory and issue the gunicorn command like so:
gunicorn --workers 3 --bind unix:smarrttrader_api.sock -m 007 appserver:gunicorn_app
Everything works fine. If I do a which gunicorn from my app directory I get /usr/local/bin/gunicorn and try to run ()from elsewhere in the server like so:
/usr/local/bin/gunicorn --workers 3 --bind unix:smarrttrader_api.sock -m 007 appserver:gunicorn_app
It doesn't work and I get the following error: ImportError: No module named 'appserver', so how can I get it to work?

how to use gunicorn with swagger_server on flask

I'm trying to start the swagger server using gunicorn on ec2 instance by using the following code:
I tried :
gunicorn -w 4 -b 0.0.0.0:8080 -p pidfile -D swagger_server:app
and this:
gunicorn -w 4 -b 0.0.0.0:8080 -p pidfile -D "python3 -m swagger_server":app
and even this :
gunicorn -w 4 -b 0.0.0.0:8080 -p pidfile -D __main__:app
How can I get it to work?
RAW python code which works : python3 -m swagger_server
What you are trying to do is equivalent to:
from swagger_server.__main__ import main
For this to work with gunicorn, try:
gunicorn "swagger_server.__main__:main" -w 4 -b 0.0.0.0:8080`
In case you have the error:
ImportError: No module named swagger_server
add the PYTHONPATH to gunicorn command:
gunicorn "swagger_server.__main__:main" -w 4 -b 0.0.0.0:8080 --pythonpath path_to_swagger_server
gunicorn -b 0.0.0.0:8080 main:app --reload
This should be the correct syntax, obviously make sure you're in the correct directory and source your virtualenv.
isn't your application looking for a configuration file with a section like [app:main]?
This one worked for me:
gunicorn "swagger_server.__main__:app" -w 4 -b 0.0.0.0:8080

Understanding Gunicorn and Flask on Docker/Docker-Compose

I'm having trouble getting Flask and Gunicorn to work properly on Docker using Docker-compose
Dockerfile:
FROM ubuntu:latest
MAINTAINER Kyle Calica "Kyle Calica"
RUN apt-get update -y
RUN apt-get install -y python3-dev build-essential python-pip gunicorn
RUN pip install --upgrade setuptools
RUN pip install ez_setup
COPY . /app
WORKDIR /app
RUN pip install -r ./app/requirements.txt
CMD [ "gunicorn", "-b", ":8000", "run" ]
Docker-Compose.yml:
version: '2'
services:
web:
build: .
volumes:
- ./:/var/www/crypto
ports:
- "5000:5000"
run.py:
from app import app
app.run()
From my understanding the Gunicorn master will run at port 8000 on all interfaces in the container
And then it'll spawn a node to run at port 5000 in the container at 127.0.0.1/localhost.
From there I link port 5000 in the container to my hostport 8000
I expected to see my application from my host computer at http://127.0.0.1:8000
instead nothing happened and nothing seemed to be connecting.
I have done this before but can't remember what I did differently.
(env) paper-street:CoinSlack kyle$ gunicorn -b :8000 run
[2017-09-16 17:43:59 -0700] [15402] [INFO] Starting gunicorn 19.7.1
[2017-09-16 17:43:59 -0700] [15402] [INFO] Listening at: http://0.0.0.0:8000 (15402)
[2017-09-16 17:43:59 -0700] [15402] [INFO] Using worker: sync
[2017-09-16 17:43:59 -0700] [15405] [INFO] Booting worker with pid: 15405
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
^the reason why is because it seems like it spawned a worker and is running it at port 5000, i can't access my app through port 8000
app.run() and gunicorn are two ways to run a webserver. The first is the Flask development server, and it's useful for development but shouldn't be deployed in production. You shouldn't run both at the same time.
gunicorn should be pointed to the app object so that it can import it and use it to run the webserver itself. That's all it needs.
Instead of CMD [ "gunicorn", "-b", ":8000", "run" ]
Do CMD ["gunicorn", "app:app", "-b", "0.0.0.0:8000"]
You can see that instead of the telling the gunicorn process to run you instead tell the process where to look. The application that you want gunicorn to serve is app. You can also add more options to the gunicorn command such as reload, the number of workers, timeout, log-levels, etc...
To expand on Alex Hall's answer, you don't want to run a Flask server on production, because the ability to scale is very limited. According to the Flask docs, the mention that:
Flask’s built-in server is not suitable for production as it doesn’t
scale well and by default serves only one request at a time

manage.py celeryd -B. How to run it in background?

I use django celery
python manage.py celeryd -B
[2013-05-01 23:42:58,583: WARNING/MainProcess] celery#aaa ready.
How to run it in background?
python manage.py celeryd -B --detach
You can refer to: http://docs.celeryproject.org/en/latest/reference/celery.bin.celeryev.html?highlight=detach#cmdoption-celery-events--detach

Categories