Run Django with python as daemon

2.5k views Asked by At

We have web application which is running with django, python and PostgreSQL. We are also using virtualenv.

To start the web service, we first activate the virtualenv and then start python as service on 8080 with nohup.

But after sometime nohup process dies. Is there any way to launch service as demon like apache, or use some thing like monit?

I am new to this, please excuse my mistakes

3

There are 3 answers

0
amit singh On BEST ANSWER

I was able to do it, but forgot to update the answers.IF any one is looking for same they can follow this. Best way to run django app in production is to run with

django+gunicorn+supervisor+nginx.

I used gunicorn which is a Python WSGI HTTP Server for UNIX where you can control thread count, timeout settings and much more. gunicorn was running was on socket, it could be run on port but to reduce tcp overhead we ran on socket.

Supervisor is used to run this gunicorn script as supervisor is simple tool which is used to control your process.

and with the help of nginx reverse proxy Our Django site was life.

For more details follow below blog.

http://michal.karzynski.pl/blog/2013/06/09/django-nginx-gunicorn-virtualenv-supervisor/

0
Alex Vyushkov On

It may be a good idea to deploy your application using apache or ngnix. There is official Django documentation on how to do it with apache - https://docs.djangoproject.com/en/1.10/howto/deployment/wsgi/modwsgi/

Apache does support virtual environment - just add python-home=<path_to_your_virtual_env> to the WSGIDaemonProcess directive when using daemon mode of mod_wsgi:

 WSGIDaemonProcess django python-path=/opt/portal/src/ python-home=/opt/venv/django home=/opt/portal/ 

Best practice for how to use mod_wsgi and virtual environments is explained in:

0
Dan On

So a runserver command should only be used in testing environments. And just like @Alasdair said, Django docs already have interesting information about that topic.

I would suggest using gunicorn as a wsgi with nginx as a reverse proxy. You can find more information here

And i would suggest using supervisor to monitor and control your gunicorn workers. More information can be found here