Gunicorn does't serve the flask app - flask

I have an issue to serve my flask app with gunicorn on ubuntu 16 (google computer engine instance).
I used the application factory pattern to create the app so, from my virtualenv, I've started gunicorn with the following instruction
gunicorn -w 3 "app:create_app()" --bind 0.0.0.0:8080
It seems that guniconr run correctly but I'm not able to reach the website.
I create the istance allowing HTTP trafic but I don't know if I have to define a specific firewall rule or if the problem is somewhere else.
Thanks in advance for your help.

Related

Do Server work while i dont connect? If yes how can i do?

I deployed my django app in ubuntu server. I want to give to API for mobile application. So i followed some source and i deployed. For I want to deploye django, I use gunicorn and ngnix.
Server is working with this command:
gunicorn --bind 0.0.0.0:8000 myapp.wsgi
I can give API with this way. Everything is okey.
But when i close cmd which connected to server, server stop. Can server work while i dont connect by my computer? Must not close cmd and my computer? Or can i do other way?
I fixed it! We just need to add "--daemon" in our code. For example:
gunicorn --bind 0.0.0.0:8000 myapp.wsgi --daemon

How to divide Daphne requests on mutiple processors?

I use a Daphne server for my ASGI Django application. When I run htop on my Ubuntu server it shows all the load on only one processor and the app gets slow, so I'd like to know what is the easiest way to speed up my server. App is inside a Docker container.
I run Daphne with command: daphne WBT.asgi:application --bind "0.0.0.0" --port "8000".
I use Nginx as a proxy.
I tried using uvicorn server with multiple workers but then I get problems with layout. Ie. let's say that I have 4 workers when I visit my web app, it loads it on only one worker correctly, so in 25% cases I get no layout.

How to run Daphne and Gunicorn At The Same Time?

I'm using django-channels therefore I need to use daphne but for the static files and other things I want to use gunicorn. I can start daphne alongside gunicorn but I can not start both of them at the same time.
My question is should I start both of them at the same time or is there any better option?
If should I how can I do that?
Here is my runing server command:
gunicorn app.wsgi:application --bind 0.0.0.0:8000 --reload && daphne -b 0.0.0.0 -p 8089 app.asgi:application
PS:
I splited location of / and /ws/ for gunicorn and daphne in nginx.conf.
Your problem is you're calling both processes in the same context/line and one never gets called because the first one never "ends".
this process: gunicorn app.wsgi:application --bind 0.0.0.0:8000 --reload
won't terminate at any point so the && command never gets ran unless you kill that command manually, and at that point I'm not sure that wouldn't kill the whole process chain entirely.
if you want to run both you can background both processes with & e.g.
(can't test but this should work)
gunicorn app.wsgi:application --bind 0.0.0.0:8000 --reload & daphne -b 0.0.0.0 -p 8089 app.asgi:application &
The teach a man to fish info on this type of issue is here
I'm fairly certain you will lose the normal console logging you would normally have by running these in the background, as such i'd suggest looking into nohup instead of & or sending the logs somewhere with a logging utility so you aren't flying blind.
As for other options if you plan to scale up to a large number of users, probably 100+ I would just run two servers, one for wsgi django http requests and one for asgi daphne ws requests. Have nginx proxy between the two for whatever you need and you're done. That's also what channels recommends for larger applications.
It is good practice to use a common path prefix like /ws/ to distinguish WebSocket connections from ordinary HTTP connections because it will make deploying Channels to a production environment in certain configurations easier.
In particular for large sites it will be possible to configure a production-grade HTTP server like nginx to route requests based on path to either (1) a production-grade WSGI server like Gunicorn+Django for ordinary HTTP requests or (2) a production-grade ASGI server like Daphne+Channels for WebSocket requests.
Note that for smaller sites you can use a simpler deployment strategy where Daphne serves all requests - HTTP and WebSocket - rather than having a separate WSGI server. In this deployment configuration no common path prefix like /ws/ is necessary.
it is not needed to run both.Daphne is a HTTP, HTTP2 and WebSocket protocol server.
take a look at README at this link:
https://github.com/django/daphne

How to serve Flask app on different port than main site running on port 80?

Read through the other threads, but none seemed to address my need.
I have a website running fine on default port 80/443 using Apache on CentOS 7.
I have now installed gunicorn and copied my files to a /api subdirectory on the server. I am able to start gunicorn successfully using "gunicorn -b 0.0.0.0:8000 api:app"; however, I don't seem to be able to browse to https://example.com:8000 (site can't be reached).
Do I need to use a VirtualHost's file specifically for the Flask /api directory? If so, what would that look like without disrupting the main site?
EDIT: Note this is a Flask-RESTful API, no static files or anything on the Flask side. Just endpoints/routes to serve.

Visit webpage hosted on ubuntu server in a local network

I have a ubuntu server hosting a web page driven by Python Django, I can access that page by using the following command:elinks http:// 127.0.0.1:8000.
Now if I want to access that same web page on a macbook sharing the same home router with my ubuntu server(local ip: 10.0.0.9), how would I do it? Typing in elinks http:// 10.0.0.9:8000 wouldn't work.
Thanks a lot,
ZZ
Are you running the development server using manage.py?
If so, you should start the server using:
python manage.py runserver 0.0.0.0:8000
This will allow the development server to be visited by ips on all interfaces instead of just localhost.
You need to serve it. There are a number of ways to do this, but my preferred method is to use Nginx as a reverse proxy server for gunicorn. This is a good tutorial for that.