Deploying Django on DigitalOcean

beginner
25 min

Deploying Django on DigitalOcean

Welcome to this comprehensive guide on deploying your Django project on DigitalOcean! By the end of this tutorial, you'll have a clear understanding of how to deploy a Django project, and you'll even get to practice with two complete working examples. Let's get started!

Prerequisites πŸ“

Before we dive in, ensure you have the following prerequisites ready:

  1. A Django project set up and running locally.
  2. A DigitalOcean account (sign up here).
  3. SSH keys set up for your DigitalOcean account.

Setting Up a Droplet 🎯

A DigitalOcean Droplet is a virtual private server (VPS) that you'll use to host your Django project. Let's create one:

  1. Log in to your DigitalOcean account.
  2. Click on Create in the top menu, then Droplets.
  3. Choose the Operating System (OS), for Django we recommend Ubuntu 20.04 x64.
  4. Select the size based on your project's requirements.
  5. Under Add Backups, toggle the switch on if needed.
  6. Click on Add SSH Key to attach your SSH key.
  7. Give your Droplet a name and click on Create.

Once the Droplet is created, you'll find its public IP address.

Connecting to Your Droplet πŸ’‘

Now, let's connect to your Droplet using SSH:

  1. Open your terminal and type ssh root@[Droplet's Public IP].
  2. Enter the password or passphrase for your SSH key when prompted.

Installing Django on the Droplet πŸ“

Let's install Django on your Droplet:

bash
sudo apt-get update sudo apt-get install python3 python3-pip python3-venv pip3 install django

Transferring Your Local Django Project 🎯

You'll need to transfer your local Django project to the Droplet. I recommend using SFTP or scp. For this tutorial, we'll use scp:

  1. In your terminal, type scp -r path/to/your/django/project root@[Droplet's Public IP]:/path/to/your/project.

Replace path/to/your/django/project with the absolute path to your Django project folder. Replace /path/to/your/project with the destination path on your Droplet.

Setting Up the Django Project πŸ’‘

Now that your project is on the Droplet, let's set it up:

  1. On the Droplet, navigate to your project directory: cd /path/to/your/project.
  2. Create a new virtual environment: python3 -m venv myenv.
  3. Activate the virtual environment: source myenv/bin/activate.
  4. Install the project's dependencies: pip install -r requirements.txt.

Running Your Django Project 🎯

Finally, let's run your Django project:

bash
django-admin runserver

Your Django project should now be running on the Droplet.

Securing Your Django Project πŸ’‘

It's essential to secure your Django project. In a real-world scenario, you'd want to use HTTPS, configure Django's built-in security features, and install a WAF (Web Application Firewall) like Cloudflare.

Quiz πŸ“

Quick Quiz
Question 1 of 1

What is the command to install Django on the Droplet?

That's it! You now have a deployed Django project on DigitalOcean. Keep practicing, and happy coding! πŸŽ‰

Note: This tutorial is for demonstration purposes only. In a real-world scenario, you'd want to consider best practices for production deployment, such as using environment variables, running the project under a non-root user, and setting up a reverse proxy like Nginx or Apache.

πŸ’‘ Pro Tip: Check out CodeYourCraft's upcoming tutorials on setting up Nginx, configuring environment variables, and deploying Django with a reverse proxy.