How to persist UWSGI server on AWS after console closed - python

I'm a newcomer to web applications and AWS, so please forgive me if this is the answer is bit trivial!
I'm hosting a python web application on a AWS EC2 server using nginx + uWSGI. This is all working perfectly, except when I terminate my connection (using putty), my uWSGI application stops running, producing a "502 Bad Gateway" error from nginx.
I'm aware of adding the "&" to the uwsgi start up command (below), but that does not work after I close out my connection.
uwsgi --socket 127.0.0.1:8000 -master -s /tmp/uwsgi.sock --chmod-socket=666 -w wsgi2 &
How do I persist my uWSGI application to continue hosting my web app after I log out/terminate my connection?
Thanks in advance!

You'll typically want to start uwsgi from an init script. There are several ways to do this and the exact procedure depends on the Linux distribution you're using:
SystemV init script
upstart script (Ubuntu)
supervisor (Python-based process manager)

For the purposes of my use case, I will not have to reboot my machine in the near future, so Linux's nohup (no hangup) command works perfectly for this. It's a quick and dirty hack that's super powerful.

Related

How different nohup & or daemon (on uwsgi for restful server)?

I am using uwsgi for restful server.
Type this in shell nohup /home/ubuntu/anaconda3/envs/py37/bin/uwsgi --ini uwsgi.ini &
OK, now it works when shell closed.
however ,,,,, is it ok for long time use???
Normally i use apache + uwsgi for web server as daemon, but what I need now is just internal restful server.
It looks satisfying my purpose,but is there any problem to continue using??
I want to know the difference of daemon and shell command.

How to correctly run datasette, an uncommonly used python library, on port 80 of a remote server?

I installed datasette, which provides a GUI for accessing SQLite databases, on a remote server and ran the following code in the folder with the SQLite databases.
nohup datasette serve -h 0.0.0.0 *.db --cors --port 8000 --metadata metadata.json > output.log 2>&1 &.
When I go to http://my-remote-server.com:8000, the site loads. But I know this is not a good long-term solution to running datasette on this server.
What is the "correct" way to have this site run, preferably on server port 80?
Were actually discussing this issue on the Datasette issue tracker at the moment - I would love to put together an official tutorial with suggestions here.
https://github.com/simonw/datasette/issues/514
The short version is that we recommend nginx on port 80 producing to Datasette on port 8000, and using systemd to keep Datasette running.
I've not actually used systemd like this myself yet, but I plan to figure it out soon so I can turn it into documentation.

invalid request block size 21573

I was reading the tutorial provided in
http://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html
This very tutorial is a great tutorial. I have been able to configure django server on my raspberry pi raspbian system, on my Ubuntu Desktop too.
Now I am trying to do the same on a Virtual Machine, Ubuntu 16.04, nginx server.
On the line,
uwsgi --socket :8001 --wsgi-file test.py
I get an error saying
invalid request block size 21573 on the terminal
I went through the uwsgi tutorial that said not to use --http for --socket;
Either way I have not been able to get my webserver running. Please help.
Nginx is currently serving a wordpress site on start.
With --socket option you have to use uwsgi module in nginx instead of proxy.

How to kill / reload / autoreload a django server having persistent connections like SSE ?

django-admin.py runserver is running while developing. I have an open webpage connected to my sse endpoint.
Seems like using django-sse breaks to server autoreload feature, cf. this issue.
What's worse is that if I manually restart the server (Ctr+C & django-admin.py runserver), it fails with a 'port already in use error' and I need to ps grep runserver kill whatever_id first, a true PITA.
So:
How comes using persistent connections breaks my dev workflow ?
Is there an easy workaround not involving patching django ?
In production I'm using a Procfile with foreman to launch gunicorn gevent workers. Here a manual restart goes fine (open connections are closed) but there's not autoreload feature nor any log printed in the terminal.

fabric don't start twisted application as a daemon

I have written a simple automation script for deploying and restarting my twisted application on remote Debian host. But I have an issue with starting using twistd.
I have a run.tac file and start my application as follows inside fabric task:
#task
def start():
run("twistd -y run.tac")
And then just fab -H host_name start. It works great on localhost but when I want to start application on remote host I get nothing. I can see in log file that application is actually launched, but factory is not started. I've also checked netstat -l - nothing is listening my port.
I've tried to run in non-daemon mode, like so twistd -ny run.tac, and, voila, factory started and I can see it in netstat -l on remote host. But that is not the way I want it to work cause it. Any help is appreciated.
There was an issue reported sometime back which is similar to this.
Init scripts frequently fail to start their daemons
init-scripts-dont-work
It also suggested that it seems to succeed with the option pty=False. Can you try and check that?
run("twistd -y run.tac", pty=False)
Some more pointers from the FaQ:
why-can-t-i-run-programs-in-the-background-with-it-makes-fabric-hang

Categories

Resources