One of the few times I have had access to an actual machine which had to server django I have never been failed by the DUNG stack. Django-Unix-Nginx-Gunicorn.
Of course I have had to replace Unix with Linux. There is nothing like a bit of Mint fresh air. This tutorial is carried out on Linux Mint but should work fine for Ubuntu and the likes.
This tutorial assumes that you have
pip and virtualenv set up and working. At the time of writing Django1.8 was the latest and Gunicorn 19.0
First things first. We have to set things up. Open up your beloved terminal and start typing.
- We have to create a directory for our tutorial. Let us call it test_django
- mkdir test_django
- cd test_django
- Now we must create a virtualenv to work in and activate it. If you do not know what that is head over here.
- virtualenv -p python3 env
- source env/bin/activate
- Now we must install django and gunicorn.
- pip install django
- pip install gunicorn
- After waiting for the above commands to finish we create an empty django project. Do not forget the dot at the end of the command.
- django-admin startproject dung_test .
- python manage.py makemigrations
- python manage.py migrate
- Our directory now looks like this now.
- .
|-- dung_test
| |-- __init__.py
| |-- settings.py
| |-- urls.py
| `-- wsgi.py
`-- manage.py
- That done we now want to make sure that our dummy website is working.
- python manage.py runserver
- Open up a browser and check localhost:8000/admin
- It should be the standard Django Administration login screen.
- To begin deploying the website with nginx and gunicorn we first have to install nginx.Issue the second command in case Nginx fails to start.
- sudo apt-get install nginx
- sudo service nginx start
- Once nginx is installed it will start automatically. You can check by navigating to "127.0.0.1" on your browser and it should say welcome to nginx.
- We now have to configure nginx to serve our website.
- We navigate to /etc/nginx/sites-available and create a new file there. Let us call it dung_website.
- cd /etc/nginx/sites-available
- Open in sudo mode. sudo gedit dung_website
- Now insert the following text into the file.
- upstream app_server_djangoapp {
server unix:/home/ghost/dev/temp/gunicorn_socket fail_timeout=0;
}
server {
listen 0.0.0.0:80;
server_name .example.com;
keepalive_timeout 5;
# path for static files
root /path/to/your/app/media;#make sure this exists
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
if (!-f $request_filename) {
proxy_pass http://app_server_djangoapp;
break;
}
}
} - Remember to replace /home/ghost/dev/temp/gunicorn_socket with your own path. The temp folder is that one which contains the test_django folder.
- Save and exit. now navigate one step up and to sites-enabled and create a link to the file just created.
- cd ..
- cd sites-enabled
- ln ../sites-available/dung_website
- Now reload nginx with sudo service nginx reload
- To configure Gunicorn nothing much is needed. Navigate to where your project was. In my case cd ~/dev/temp
- Now add "gunicorn" to the list of installed apps in the dung_test/settings.py folder.
- INSTALLED_APPS = (
'django.contrib.auth',
...
'gunicorn',
...
)
- We were earlier testing the website by running python manage.py runserver
- This will be replaced by gunicorn dung_test.wsgi:application --bind=unix:/home/ghost/dev/temp/gunicorn_socket --log-file=-
- The address given to bind is the same as written in the nginx server. The unix sockets are faster than using loopback. We could replace the unix:/path/to/socket with localhost:8000 in the nginx file and gunicorn command
- With that done we can open our browser and navigate to localhost/admin to see the admin login page without the CSS. That is solved py running python manage.py collectstatic from the folder where manage.py is kept after setting up static files in the settings.py
- Your website can be accessed from other computers by typing in your IP address. To find your ip address run ifconfig in a terminal while connected to the network