How to deploy django to production using DigitalOcean (Part-1)

In the part 1 of this tutorial, we will be looking at how to set up a digitalocean project and run Django web app on it

Log in to your server

Once the Droplet is created successfully, you will receive a mail from DigitalOcean that looks like this:

DigitalOcean email

Note the IP Address. In this demo, the ip-address is 142.93.83.203 but it may be different for you:

Now go to your ubuntu terminal and ssh login into the droplet using the command: ssh [email protected]_ip_address In the demo, it is ssh [email protected] Use the password you received in your email for the login. You will be asked to change the password on your first login.

Create a new user account and give it sudo access

While technically you can do everything as root it is just good practice on any operating system to run your applications on a user level and leave administrative tasks to the root user, and only on a per-need basis. So, let’s create a new user account and give it sudo access. To create a new user account, type adduser username

[email protected]:~# adduser pytorials
Adding user `pytorials’ …
Adding new group `pytorials’ (1001) …
Adding new user `pytorials’ (1001) with group `pytorials’ …
Creating home directory `/home/pytorials’ …
Copying files from `/etc/skel’ …
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
Changing the user information for pytorials
Enter the new value, or press ENTER for the default
Full Name []: Pytorials
Room Number []:
Work Phone []:
Home Phone []:
Other []:
Is the information correct? [Y/n] Y

Now, add it to the sudo group using the command usermod -aG sudo username

Logout and login again as the new user you just created using the command: ssh [email protected]_ip_address In the demo, it is ssh [email protected]

Install the packages from Ubuntu Repositories

We will update the apt package index and download and install the necessary packages. To do so, run the command:

  • sudo apt-get update
  • sudo apt-get install python3-pip python3-dev libpq-dev postgresql postgresql-contrib nginx python3-django

This will install pip3, postgresql, nginx, django and python development files needed to build gunicorn later.

Testing your first django site on the DigitalOcean server

First, let’s install django using the command pip3 install django

Now, run the command django-admin startproject mysite to create your first django project. The command creates files and folders on the current directory that looks like this:

mysite/
    manage.py
    mysite/
        __init__.py
        settings.py
        urls.py
        wsgi.py

Navigate to settings.py and on the locate ALLOWED_HOSTS = [] and replace it with ALLOWED_HOSTS = ["your_ip_address"] using the text editor of your choice.
Now, navigate to mysite and run the command python3 manage.py runserver 0.0.0.0:8000 On your browser, if you navigate to your_ip_address:8000. In this demo, 142.93.83.203:8000, you will see a page that looks like.

django server

This means you have successfully installed Django on your DigitalOcean server and is it is running successfully. On your terminal, you will see a message that says:

You have 17 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run ‘python manage.py migrate’ to apply them.

You can run the command python3 manage.py migrate to silence the message. We will not be going into details on web development using Django but if you want to learn it, I highly recommend Django documentation. While our Django application is up and running in the DigitalOcean server, it is still set to the development environment. In the next part of the article, we will look at how to deploy Django to the production environment.



1 Trackbacks & Pingbacks

  1. How to deploy django to production (Part-2) | pytorials.com

Leave a Reply

Your email address will not be published.




This site uses Akismet to reduce spam. Learn how your comment data is processed.