How to Deploy Django on DigitalOcean

Django is a powerful web framework that makes it easy to build complex, database-backed web applications. Deploying Django on DigitalOcean is a great way to get your application up and running quickly and easily. In this tutorial, we will walk you through the steps of deploying Django on DigitalOcean.

Create a DigitalOcean Account

The first step in deploying Django on DigitalOcean is to create an account. To do this, go to DigitalOcean's website and click the "Sign Up" button. You will then be asked to enter your email address and create a password.

Select the Droplet Configuration

Once you have created your account, you will be taken to the Droplet creation page. Here, you can select the configuration of your Droplet. You can choose from a variety of options, including the operating system, memory size, and disk size.

For this tutorial, we will be using Ubuntu 18.04 as our operating system and 1GB of RAM as our memory size. We will also select the "One-click Apps" option and choose "Django on 16.04" from the list of available apps.

Connect to Your Droplet

Once you have selected your Droplet configuration, click the "Create" button to create your Droplet. Once it has been created, you can connect to it via SSH using the IP address provided in the Droplet details page.

To connect via SSH, open a terminal window and type:


ssh root@[IP_ADDRESS]

You will then be prompted for your password. Enter it and press enter.

Install Python and Pip

Once you are connected to your Droplet via SSH, you can begin installing Python and Pip. To do this, type:


sudo apt-get update && sudo apt-get install python3 python3-dev python3-venv python3-pip -y

This will install Python 3 and Pip on your Droplet.


Install Django


Now that Python and Pip are installed, you can install Django by typing:



sudo pip3 install django==1.11.5

This will install Django version 1.11.5 on your Droplet.


Configure Your Project


Now that Django is installed, you can configure your project by creating a new directory for it:



mkdir myproject && cd myproject

Next, create a virtual environment for your project:



python3 -m venv venv

Activate the virtual environment:



source venv/bin/activate

Now that the virtual environment is activated, you can install Django in it:



pip install django==1.11.5

Configure Your Database


Now that Django is installed in your virtual environment, you can configure your database by creating a new database user:


CREATE USER myprojectuser WITH PASSWORD 'myprojectpassword'; CREATE DATABASE myproject; GRANT ALL PRIVILEGES ON DATABASE myproject TO myprojectuser;

You can then configure Django to use this database by editing the settings file (settings.py):



DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql_psycopg2', 'NAME': 'myproject', 'USER': 'myprojectuser', 'PASSWORD': 'myprojectpassword', 'HOST': 'localhost', 'PORT': '' } }

Migrate Your Database


Once you have configured your database in settings.py, you can migrate it by running the following command:



python manage.py migrate

This will create all of the necessary tables in your database for your project.


Conclusion


In this tutorial we have walked through how to deploy Django on DigitalOcean using Ubuntu 18.04 as our operating system and 1GB of RAM as our memory size.


We have gone through how to create an account with DigitalOcean, select our droplet configuration, connect to our droplet via SSH, install Python and Pip, install Django in our virtual environment, configure our database in settings.py and migrate our database with manage.py.
With these steps completed we now have a fully functioning Django application deployed on DigitalOcean.
Useful Links: • DigitalOcean – https://www.digitalocean.com/ • Python – https://www.python.org/ • Django – https://www.djangoprojectscom/