Django Gunicorn Setup
Gunicorn is a “Python WSGI HTTP Server for UNIX”. It is absolutely stupid simple to setup and get running.
I will be using the following for this guide:
- Django 1.8
- Debian 8 (jessie)
pip3 install gunicorn
Create a blank WSGI file inside your project folder (where
manage.pyresides). This is where you can configure Gunicorn options instead of passing arguments to it.
gunicorn --threads=COUNT PROJECT.wsgi:application
That’s it. You now have a Gunicorn WSGI server running on port
The Gunicorn docs recommend setting the thread count to 2x to 3x the core count of the server.
Running Gunicorn aaS (systemd)
After install Gunicorn and testing it out, you’ll probably want to set it up as a service so it can be started automatically on boot.
Create a new user.
Give it an extremely long and random password. You won’t be logging into this user.
Move your Django project into
/home/django/. This is now where your project will reside.
Change the ownership of your project files.
chown -R django:django /home/django/
Update the static files path in nginx if you have one set.
Create the Gunicorn systemd service file.
``` [Unit] Description=gunicorn daemon Requires=gunicorn.socket After=network.target
[Service] Environment=PYTHONHASHSEED=random PIDFile=/run/gunicorn/pid User=django Group=django WorkingDirectory=/home/django/PROJECT ExecStart=/usr/local/bin/gunicorn –pid /run/gunicorn/pid django.wsgi:application ExecReload=/bin/kill -s HUP $MAINPID ExecStop=/bin/kill -s TERM $MAINPID PrivateTmp=true
* [PYTHONHASHSEED](https://docs.djangoproject.com/en/1.8/howto/deployment/checklist/) 4. Create the Gunicorn systemd socket file. `nano /etc/systemd/system/gunicorn.socket`
[Unit] Description=gunicorn socket
[Socket] ListenStream=/run/gunicorn/socket ListenStream=0.0.0.0:9000 ListenStream=[::]:8000
5. Change the permissions so the pid file can be created. `chown -R django:django /run/gunicorn` 6. Start the services.
systemctl start gunicorn.socket systemctl start gunicorn.service
7. Enable the services to be started on boot if there were no problems.
systemctl enable gunicorn.socket systemctl enable gunicorn.service ```
- Update any cronjobs for Django to the new user.
- Change file permissions of the static files if you are not running nginx as the
I had one small issue with Redis where the user had no permission to access
/var/run/redis.sock. This was easily solved by using TCP sockets instead of Unix sockets.