Deploying Django 3, Nginx & Postgre to Ubuntu 18.04 VPS
Installing Server Dependencies
In this article we’ll deploying Django 3 with Python 3 to Ubuntu server. This article will only focus on deploying a basic Django application & avoid unnecessary complexities. In this article I also assume you already have installed a fresh Ubuntu server and access as root to the server. First let’s update server first:
$ apt update
install python3 (if your server doesn’t have it)
$ apt install python3
install python3 pip
$ apt install python3-pip
Install other dependencies to support Python & Postgre:
$ apt install build-essential
followed by:
$ apt install libpq-dev python3-dev
Postgre & Nginx Installation
Now we’re going to install database engine which is Postgre:
$ apt install postgresql postgresql-contrib
We need to switch user to postgre in order to create a new database & a new user attached to the database:
$ su - postgres
From postgres user let’s create a new database named project (or you may using another name if you wish to do so, but make sure you also adjusted the name in Django database connection setting later):
$ createdb project
then added a new user named django (or another name) & attached the user to the newly created database:
$ createuser --interactive --pwprompt
- At the Enter name of role to add: prompt, type the user’s name in this case is django.
- At the Enter password for new role: prompt, type a password for the user.
- At the Enter it again: prompt, retype the password.
- At the Shall the new role be a superuser? prompt, type y if you want to grant superuser access. Otherwise, type n.
- At the Shall the new role be allowed to create databases? prompt, type y if you want to allow the user to create new databases. Otherwise, type n.
- At the Shall the new role be allowed to create more new roles? prompt, type y if you want to allow the user to create new users. Otherwise, type n.
Grant access for user django to access database project:
$ psql
Followed by:
GRANT ALL PRIVILEGES ON DATABASE project TO django;
Great we’ve setup the database, let’s get back from user postgres to root by typing from psql console:
\q
followed by:
$ exit
then install Nginx:
$ apt install nginx
now we’re going to create a configuration file for our Nginx:
$ nano /etc/nginx/sites-enabled/django_app
inside the editor add code as follow:
server {
listen 80;
server_name 192.0.2.0; location /static/ {
alias /root/myproject/static/; # django app static file
} location / {
proxy_pass http://127.0.0.1:8000;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
Disable Nginx default configuration since we’re going to use our own configuration:
$ unlink /etc/nginx/sites-enabled/default
Reload Nginx server using command:
nginx -s reload
Installing Django 3 & Deployment
After our preparation now we’re ready to install a fresh Django project & deploying it. First of all, install latest Django version:
$ pip3 install django
we need to install psychopg2 in order to make django able to connect to Postgre database:
$ pip3 install psycopg2
now we can create a simple django project for testing deployment purpose:
$ django-admin.py startproject myproject
a new directory named my project is created, let’s edit settings.py inside myproject directory
nano myproject/myproject/settings.py
replace the database part as follow:
at ALLOWED_HOSTS section, add your domain and IP Address, for example my server IP address is 206.189.200.41 therefore I modified:
ALLOWED_HOSTS = []
become
ALLOWED_HOSTS = ['206.189.200.41']
In deployment you also need to adjust the static files please add these two lines at below settings.py:
STATIC_URL = '/assets/'
STATIC_ROOT = '/root/myproject/static'
last, in your project urls.py (in my case myproject/myproject/urls.py)add static files to the URL as follow :
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
# ... the rest of your URLconf goes here ...
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
You can visit django documentation at managing static file static section here for the detail
Let’s test the database by doing default migration from django by go into root project directory (in my case /myproject):
$ python3 manage.py migrate
You’ll see something like this:
we also need to collect static files so the app can load static files such as images, js & css files correctly:
$ python3 manage.py collectstatic
Which means that we’re successfully migrate to our Postgre database and next we can install Gunicorn:
$ apt install gunicorn3
Followed by (inside your root project directory):
$ gunicorn3 myproject.wsgi:application
Please take a note that the project we created before is myproject therefore we use myproject.wsgi if you use another name then you need to alter the name with yours. Now you can access /admin page since that’s the only one we have in our basic Django project.
Conclusion
Deploying Django 3 to Ubuntu server isn’t difficult but the thing is please ensure you follow this step correctly. If you miss at least one step, it may lead to error. That’s the common problem you may encounter when learning how to deploy Django app. Another tricky problem is your server can miss certain dependencies & you need to install them. Google & Stackoverflow may comes in handy for this problem.