I'm using Django 1.4.1 with the gunicorn and nginx on my server with a setup similar to Django-Nginx-Gunicorn.
What I need is a good place to hook into Django, so that I can initialize stuff like signal listeners on startup (so it works with all management commands and server startup).
I am aware that a lot of people use urls.py or the middleware hack (Where to put Django startup code?), but those don't seem to get called when I run python manage.py shell. And at some point down the road a startup signal will be available, but I don't want to fork Django to include it, unless absolutely necessary.
I really like the startup script technique from Entry Point Hook Django Projects and it works correctly when using python manage.py runserver or any other management command. However, gunicorn starts Django without calling the management command and I can't figure out where I would modify my gunicorn startup script to run the startup.py, as shown in the WSGI.py example. Any solutions?
Related
In our Django project(Django 2.1.5), every time we try to run the project we have to give the '--noreload' command in addition to the runserver command, else the project returns an error as,
ValueError: signal only works in main thread
We are using Django signals to communicate between the apps created in Django and Web-sockets in Threading aysnc-mode to connect between the other services involved in the project. When we try to deploy the project in Jenkins, this becomes a problem and we are using Nginx as the webserver for host the application. Is there any possibility to solve the issue of '--noreload' and run the application normally?
We are not sure if its because of the same problem referred above but we have a problem when trying to migrate the changes in the Models in Django, it always returns
No changes Detected
After a quick internet search, we did the migrations by mentioning the app names and it did work, yet the terminal stays still after the migrating and waits to manually terminate the process.
Is there a possible solution to overcome this? and also we would like to know where we go wrong
I have a Django app that I am trying to deploy to AWS.
I need two command lines to run the app. One command line will be used to runserver and another one to run a background task. That is how the app runs in my localserver.
How do I start two command lines in AWS?
As i can understand your django app is currently in development mode, I've used tmux for development mode, this page will guide you better to use tmux. Once you have started a tmux session run python3 manage.py runserver and then detach to the session using ctrl-b then press d and now your app will run even after you exit the shell.
To know more about tmux go through this page.
In case you have to run your app in production mode Don't use above method, run your app with any of nginx or apache2 server. This tutorial can guide you to set up your django app with nginx and gunicorn.
Supervisor is a good way to control that. You can run a lot of processes (like server, migration, collectstatic, Celery) easily.
Additionally, you can ensure that all processes will run again when your machine reboots.
And, as everybody said, you have to install a server in production that supports WSGI in order to run Django properly.
I have introduced a new django command which i can run from cronjob. This is particulary helpful to get ORM specification.
To Run this django command, do we need the django server should be running ?
No, the django server is a separate process completly independent from your custom command.
If you are using virtualenv (if you aren't yo probably should) keep in mind you must source the virtualenv or use the python interpreter within it in order to get the managemente command properly run.
After changing the view function without runserver again, and press F5 to refresh the page, Django will not reload the new view but use the previous one. but if you change the template, Django always uses the new one.
So, Is there a way to make Django reload the view every time the user refresh the page, I think that is very convenient for develop to modify the view function frequently.
If you are running django using the dev server (./manage.py runserver) then it will always reload when it detects any code changes. This is even more efficient than reloading with every request. If you make a change, it reloads when it needs to.
If you are running a production server (nginx, apache, etc) and you want code-reload, then you need to add something to your wsgi module to detect code changes.
Code reloading with apache: http://code.google.com/p/modwsgi/wiki/ReloadingSourceCode
Code reloading with uwsgi: http://projects.unbit.it/uwsgi/wiki/TipsAndTricks
It is a known issue with PyDev. I would suggest running the server from terminal/cmd.
cd to your project directory where manage.py is present and run the server using
python manage.py runserver
You don't need to run the project from eclipse menu.
Any changes made in eclipse would be reflected as soon as they are made.
If you are running Django as a WSGI application in daemon mode you just need to touch the wsgi.py for your site and it will reload the application next time there is a request. (So no need for any special options).
I noticed it is a setting in pyDev run configurations. I wonder why but it seems --noreload is configured by default. So I edit arguments of run settings and now the server is reloading also when editing views.
Try using gunicorn or nginx as running server... They dont restart on code change
try typing
gunicorn --bind 0.0.0.0:8080 app.wsgi:application
I am quite a noob when it comes to deploying a Django project. I'd like to know what are the various methods to deploy Django project and which one is the most preferred.
The Django documentation lists Apache/mod_wsgi, Apache/mod_python and FastCGI etc.
mod_python is deprecated now, one should use mod_wsgi instead.
Django with mod_wsgi is easy to setup, but:
you can only use one python version at a time [edit: you even can only use the python version mod_wsgi was compiled for]
[edit: seems if I'm wrong on mod_wsgi not supporting virtualenv: it does]
So for multiple sites (targeting different django/python versions) on a server mod_wsgi is not
the best solution.
FastCGI can be used with virtualenv, also with different python versions, as you run it with
./manage.py runfcgi …
and then configure your webserver to use this fcgi interface.
The new, hot stuff about django deployment seems to be gunicorn. It's a webserver that implements wsgi and is typically used as backend with a "big" webserver as proxy.
Deployment with gunicorn feels a lot like fcgi: you run a process doing the django processing stuff with manage.py, and a webserver as frontend to the world.
But gunicorn deployment has some advantages over fcgi:
speed - I didn't find the sources, but benchmarks say fcgi is not as fast as the f suggests
config files, for fcgi you must do all configuration on the commandline when executing the manage.py command. This comes unhandy when running multiple django instances via an init.d (unix-like OS' system service startup). It's always the same cmdline, with just different configuration files
gunicorn can drop privileges: no need to do this in your init.d script, and it's easy to switch to one user per django instance
gunicorn behaves more like a daemon: writing pidfile and logfile, forking to the background etc. makes again using it in an init.d script easier.
Thus, I would suggest to use the gunicorn solution, unless you have a single site on a single server with low traffic, than you could use the wsgi solution. But I think in the long run you're more happy with gunicorn.
If you have a django only webserver, I would suggest to use nginx as frontendproxy, as it's the best performing (again this is based on benchmarks I read in some blogposts - don't have the url anymore).
Personally I use apache as frontendproxy, as I need it for other sites hosted on the server.
A simple setup instruction for django deployment could be found here:
http://ericholscher.com/blog/2010/aug/16/lessons-learned-dash-easy-django-deployment/
My init.d script for gunicorn is located at github:
https://gist.github.com/753053
Unfortunately I did not yet blog about it, but an experienced sysadmin should be able to do the required setup.
Use the Nginx/Apache/mod-wsgi and you can't go wrong.
If you prefer a simple alternative, just use Apache.
There is a very good deployment document: http://lethain.com/entry/2009/feb/13/the-django-and-ubuntu-intrepid-almanac/
I myself have faced a lot of problems in deploying Django Projects and automating the deployment process. Apache and mod_wsgi were like curse for Django Deployment. There are several tools like Nginx, Gunicorn, SupervisorD and Fabric which are trending for Django deployment. At first I used/configured them individually without Deployment automation which took a lot of time(I had to maintain testing as well as production servers for my client and had to update them as soon as a new feature was tested and approved.) but then I stumbled upon django-fagungis, which totally automates my Django Deployment from cloning my project from bitbucket to deploying on my remote server (it uses Nginx, Gunicorn, SupervisorD, Fabtic and virtualenv and also installs all the dependencies on the fly), all with just three commands :) You can find more about it in my blog post here. Now I even don't have to get involved in this process(which used to take a lot of my time) and one of my junior developers runs those three commands of django-fagungis mentioned here on his local machine and we get a crisp new copy of our project deployed in minutes without any hassle:)