Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Deployment on DigitalOcean

Introduction

Deploying a Django application on DigitalOcean involves several steps, from creating a droplet to setting up your server and deploying your code. This tutorial will guide you through the entire process, ensuring your application runs smoothly on a DigitalOcean server.

Creating a DigitalOcean Account

Before you can deploy your application, you need to create a DigitalOcean account.

  1. Go to DigitalOcean.
  2. Sign up for an account or log in if you already have one.
  3. Once logged in, you can create your first droplet.

Creating a Droplet

A droplet is a virtual private server that can host your Django application.

  1. Click on the "Create" button and select "Droplets".
  2. Choose an image. For this tutorial, we'll use Ubuntu 20.04.
  3. Select a plan. The standard plan is sufficient for most small to medium-sized applications.
  4. Choose a datacenter region close to your target audience.
  5. Add SSH keys for secure access to your server.
  6. Click on "Create Droplet" to finalize the creation process.

Accessing Your Droplet

Once your droplet is created, you can access it using SSH. To do this, you need the IP address of your droplet, which can be found on your DigitalOcean dashboard.

Open your terminal and run the following command:

ssh root@your_droplet_ip

Replace your_droplet_ip with the actual IP address of your droplet.

Setting Up the Server

After accessing your droplet, you need to set up the server for your Django application.

  1. Update the package list and upgrade the installed packages:
  2. sudo apt update

    sudo apt upgrade

  3. Install necessary packages:
  4. sudo apt install python3-pip python3-dev libpq-dev postgresql postgresql-contrib nginx curl

Setting Up PostgreSQL

Django works well with PostgreSQL. Let's set it up.

  1. Log in to the PostgreSQL database server:
  2. sudo -u postgres psql
  3. Create a database and a user:
  4. CREATE DATABASE mydb;

    CREATE USER myuser WITH PASSWORD 'password';

    ALTER ROLE myuser SET client_encoding TO 'utf8';

    ALTER ROLE myuser SET default_transaction_isolation TO 'read committed';

    ALTER ROLE myuser SET timezone TO 'UTC';

    GRANT ALL PRIVILEGES ON DATABASE mydb TO myuser;

    \q

  5. Install and configure the virtual environment:
  6. sudo apt install python3-venv

    python3 -m venv myenv

    source myenv/bin/activate

  7. Install Django and Gunicorn:
  8. pip install django gunicorn psycopg2-binary

Deploying Your Django Application

Let's deploy your Django application on the server.

  1. Clone your Django project from a version control system like Git:
  2. git clone https://github.com/yourusername/your-repo.git
  3. Navigate to your project directory:
  4. cd your-repo
  5. Install the project dependencies:
  6. pip install -r requirements.txt
  7. Apply the migrations:
  8. python manage.py migrate
  9. Collect static files:
  10. python manage.py collectstatic
  11. Test your application:
  12. python manage.py runserver 0.0.0.0:8000

Configuring Gunicorn

Gunicorn is a Python WSGI HTTP Server for UNIX. It will serve your Django application.

  1. Create a systemd service file for Gunicorn:
  2. sudo nano /etc/systemd/system/gunicorn.service

  3. Add the following content to the file:
  4. [Unit]
    Description=gunicorn daemon
    After=network.target
    
    [Service]
    User=root
    Group=www-data
    WorkingDirectory=/path/to/your/project
    ExecStart=/path/to/your/venv/bin/gunicorn --workers 3 --bind unix:/path/to/your/project.sock your_project.wsgi:application
    
    [Install]
    WantedBy=multi-user.target
  5. Replace the placeholders with your actual paths and project names.
  6. Start and enable the Gunicorn service:
  7. sudo systemctl start gunicorn

    sudo systemctl enable gunicorn

Configuring Nginx

Nginx will act as a reverse proxy for Gunicorn.

  1. Create a new server block configuration file:
  2. sudo nano /etc/nginx/sites-available/your_project

  3. Add the following content to the file:
  4. server {
        listen 80;
        server_name your_domain_or_IP;
    
        location = /favicon.ico { access_log off; log_not_found off; }
        location /static/ {
            root /path/to/your/project;
        }
    
        location / {
            include proxy_params;
            proxy_pass http://unix:/path/to/your/project.sock;
        }
    }
  5. Replace the placeholders with your actual paths and project names.
  6. Enable the file by creating a symbolic link:
  7. sudo ln -s /etc/nginx/sites-available/your_project /etc/nginx/sites-enabled

  8. Test the configuration and restart Nginx:
  9. sudo nginx -t

    sudo systemctl restart nginx

Conclusion

Congratulations! You have successfully deployed your Django application on DigitalOcean. Your application should now be running and accessible via your domain or IP address.