6
It’s probably easier to start afresh. Tutorial at https://www.digitalocean.com/community/articles/how-to-install-and-configure-django-with-postgres-nginx-and-gunicorn.
I got it running on a fresh ubuntu 14.04 droplet. Install python3 and django and then simply follow the tutorial. Didn’t do the postgres or virtualenv bits though.
52
It seems that there’s a package for this called gunicorn3
(this was tested on ubuntu)
sudo apt-get install gunicorn3
then running the following command should work and run gunicorn with python3:
gunicorn3 --log-level debug --bind 0.0.0.0:30443 server:app
- [Django]-How to add multiple objects to ManyToMany relationship at once in Django ?
- [Django]-Django: using more than one database with inspectdb?
- [Django]-CSRF with Django, React+Redux using Axios
27
In case the two links break one day, here’s how I got it working.
Starting after executing these instructions.
pip uninstall gunicorn
pip3 install gunicorn
Install supervisor
, sudo apt-get install supervisor
.
Next, I needed to make gunicorn_config.py
in the root of my project directory, which contains this.
command = '/usr/local/bin/gunicorn'
pythonpath = '/home/django/django_project'
bind = '127.0.0.1:9000'
workers = 3
user = 'nobody'
Then, I created a configuration file for supervisor
. vim /etc/supervisor/conf.d/gunicorn.conf
, with these contents.
[program:gunicorn]
command=/usr/local/bin/gunicorn -c /home/django/django_project/gunicorn_config.py django_project.wsgi
user=nobody
autostart=true
autorestart=true
stderr_logfile=/var/log/gunicorn3.err.log
stdout_logfile=/var/log/gunicorn3.out.log
After that, I did a supervisorctl reread
and supervisorctl update
and then it all started working.
You can use supervisorctl status gunicorn
to check if gunicorn
is running or not. You can use supervisorctl restart gunicorn
to restart.
- [Django]-In Django Admin how do I disable the Delete link
- [Django]-Pulling data to the template from an external database with django
- [Django]-TypeError: data.forEach is not a function
10
my way:
virtualenv -p /usr/bin/python3 /home/py3env
source /home/py3env/bin/activate
pip3 install gunicorn
/home/py3env/bin/gunicorn -w4 -b0.0.0.0:8000 [projectname].wsgi
- [Django]-What is "load url from future" in Django
- [Django]-Django models ForeignKey on_delete attribute: full meaning?
- [Django]-Django: Query using contains each value in a list
6
The way I make it happen was to uninstall gunicorn from everywhere:
sudo apt-get remove gunicorn
pip uninstall gunicorn
pip3 uninstall gunicorn
And then Installing gunicorn
from source.
pip3 install git+https://github.com/benoitc/gunicorn.git
Now everything’s running with no problem.
- [Django]-Aggregate (and other annotated) fields in Django Rest Framework serializers
- [Django]-Pylint "unresolved import" error in Visual Studio Code
- [Django]-What is a Django QuerySet?
5
Wrote the following script to switch to Python 3.4 with DigitalOcean’s 14.04 Django image, as I wanted it to be a nice one-step setup… It will be maintained at https://gist.github.com/tr00st/190ab4de62f9b23bea69
The main issue with the setup for me was with gevent, switching to tornado for workers worked fine.
#!/bin/bash
# Python 3 Upgrade for Django Droplet
# Will update the "Django on 14.04" Digital Ocean image.
# Run as root.
# Grab psycopg2 and pip
apt-get install python3-pip python3-psycopg2
# Remove the Python 2.7 version of gunicorn, so we can...
pip uninstall gunicorn
# Install the Python 3 version of gunicorn, and a couple of dependencies.
pip3 install gunicorn tornado django
# Sadly, at time of writing, gevent isn't Python 3 compatible... But tornado is!
# So, switch them out with a little sed magic
sed 's/worker_class = '\''gevent'\''/worker_class='\''tornado'\''/' /etc/gunicorn.d/gunicorn.py -i.orig
# Restart gunicorn to make the changes take effect...
service gunicorn restart
# And we're good!
- [Django]-Form field description in django admin
- [Django]-Django – [Errno 111] Connection refused
- [Django]-How to access Enum types in Django templates
3
I think the best way to do this is go to /usr/local/bin/gunicorn and change first line which is shebang line to #!/usr/bin/python-version
for ex:
my gunicorn runs with python3.5 interpretor
#!/usr/bin/python3.5
# -*- coding: utf-8 -*-
import re
import sys
from gunicorn.app.wsgiapp import run
if __name__ == '__main__':
sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
sys.exit(run())
- [Django]-Django dynamic forms – on-the-fly field population?
- [Django]-How to add readonly inline on django admin
- [Django]-Django: How can I create a multiple select form?
2
If you look at the gunicorn
executable, it’s just a small python script:
$ cat gunicorn
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import re
import sys
from gunicorn.app.wsgiapp import run
if __name__ == '__main__':
sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0])
sys.exit(run())
The important bit is the from gunicorn.app.wsgiapp import run
line, which tells you the location of the module responsibe for running your app. If gunicorn is quality code (which it is), you should be able to import that module directly from the command line and run your app.
“Importing the module directly from the command line” means using the -m
command line switch.
When called with -m module-name, the given module is located on the Python module path and executed as a script.
Using the python of your choice:
$ /path/to/your/python -m gunicorn.app.wsgiapp {{ gunicorn command line args go here }} server:app
And sure enough it runs!
[2017-12-04 02:05:27 +0000] [24] [INFO] Starting gunicorn 19.7.1
[2017-12-04 02:05:27 +0000] [24] [INFO] Listening at: http://127.0.0.1:5000 (24)
[2017-12-04 02:05:27 +0000] [24] [INFO] Using worker: sync
[2017-12-04 02:05:27 +0000] [28] [INFO] Booting worker with pid: 28
[2017-12-04 02:05:27 +0000] [29] [INFO] Booting worker with pid: 29
This behavior is very useful, for example when running gunicorn from somewhere like a Dockerfile.
- [Django]-Class views in Django
- [Django]-Django's timezone.now does not show the right time
- [Django]-Django: How to get related objects of a queryset?
1
Install a python3 virtual environment in your project folder
$ pipenv --three
Then run gunicorn in the activated environment
$ gunicorn [PROJECT].wgsi
- [Django]-Django REST framework: non-model serializer
- [Django]-Why does django run everything twice?
- [Django]-Unresolved attribute reference 'objects' for class '' in PyCharm
1
You can use a virtual environment
python3 -m venv .venv
source .venv/bin/activate
pip install gunicorn
And, for start gunicorn (if you already have in global)
.venv/bin/gunicorn # instead of gunicorn
- [Django]-Is there a list of Pytz Timezones?
- [Django]-IntegrityError duplicate key value violates unique constraint – django/postgres
- [Django]-Timestamp fields in django
0
I had the same issue on Digital Ocean using the droplet “Ubuntu Django on 14.04”.
I realized that the ‘gevent’ worker type was the problem for me when using Python 3. Even though I checked with python3 -m pip freeze
that ‘gevent’ was installed, it did not work. I changed it to ‘sync’ in /etc/gunicorn.d/gunicorn.py:
...
worker_class = 'sync'
...
I restarted gunicorn:
sudo service gunicorn restart
I checked the gunicorn service was running by using service gunicorn status
, and was able to see the welcome to django page by reaching my droplet’s ip address.
I hope that works for other people.
- [Django]-Django filter JSONField list of dicts
- [Django]-How do I return a 401 Unauthorized in Django?
- [Django]-How to duplicate virtualenv
0
Here is how i made it to work for me. I have installed gunicorn both with pip2 and pip3. I need both versions. The default one is gunicorn with pip2.
What I did was I installed gunicorn with virtualenv using pip3 and looked at content of the file gunicorn under bin in the virtualenv folder, that says
#!/********/virtualenv/gunicorn3/corto/bin/python3
# -*- coding: utf-8 -*-
import re
import sys
from gunicorn.app.wsgiapp import run
if __name__ == '__main__':
sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0])
sys.exit(run())
I copied this file and put it elsewhere, then i changed
#!/********/virtualenv/gunicorn3/corto/bin/python3
to
#!/usr/bin/python3
Now you can run gunicorn easily like this after getting into the directory that you copied the file gunicorn in
python3 gunicorn -c /your_config_file.py class_app:app
Note: doing pip3 uninstall gunicorn, followed by pip3 install gunicorn will install gunicron with python3 version (overriding the python2 version) in the dir ‘usr/local/bin’ (ubuntu) and then you can get the file contents of gunicron there. This will help you avoid using virtualenv.
If this doesnt work first time, do pip3 unistall gunicorn, followed by pip3 install gunicorn.
Happy unicorning
- [Django]-Effects of changing Django's SECRET_KEY
- [Django]-How do I invalidate @cached_property in django
- [Django]-Django: show the count of related objects in admin list_display