Forem

Sospeter Mong'are
Sospeter Mong'are

Posted on

1

Step-by-step guide to host your Django backend API on a Vps Server (Contabo)

Here's a step-by-step guide to host your Django backend API on a subdomain (api-subdomain.mydomain.co.ke) using Nginx, Gunicorn, and Contabo as your server.


1. Point the Subdomain to Your Contabo Server (DNS Settings)

On your domain registrar (where you bought mydomain.co.ke):

  • Go to the DNS settings or Manage DNS section.
  • Create an A Record for api-subdomain:

    • Type: A
    • Name: api-subdomain
    • Value: Your Contabo public IP
    • TTL: Default or 300 seconds

πŸ“ It may take up to 10–30 minutes for DNS to propagate.

To confirm that the subdomain has propagated by ping. i.e. ping api-subdomain.domain.com

2. Install Required Packages on Contabo (Ubuntu)

sudo apt update
sudo apt install python3-pip python3-venv nginx git
pip install gunicorn
Enter fullscreen mode Exit fullscreen mode

3. Clone Your Django App to the Server

cd /var/www/
sudo git clone https://github.com/your-username/your-django-repo.git subdomain_be
cd subdomain_be
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt
Enter fullscreen mode Exit fullscreen mode

Run Django migrations:

python manage.py migrate
Enter fullscreen mode Exit fullscreen mode

Collect static files:

python manage.py collectstatic
Enter fullscreen mode Exit fullscreen mode

4. Test Gunicorn

Run Gunicorn to make sure your app runs:

gunicorn --bind 127.0.0.1:8000 django_project_backend.wsgi:application
Enter fullscreen mode Exit fullscreen mode

Replace django_project_backend with your Django project name (same folder with settings.py).


5. Set Up a Systemd Service for Gunicorn

Create the file:

sudo nano /etc/systemd/system/subdomain_be.service
Enter fullscreen mode Exit fullscreen mode

Paste this:

[Unit]
Description=Gunicorn daemon for api-subdomain
After=network.target

[Service]
User=www-data
Group=www-data
WorkingDirectory=/var/www/subdomain_be
ExecStart=/var/www/subdomain_be/venv/bin/gunicorn --access-logfile - --workers 3 --bind unix:/var/www/subdomain_be/subdomain_be.sock django_project_backend.wsgi:application

[Install]
WantedBy=multi-user.target
Enter fullscreen mode Exit fullscreen mode

Start and enable it:

sudo systemctl daemon-reexec
sudo systemctl daemon-reload
sudo systemctl start subdomain_be
sudo systemctl enable subdomain_be
Enter fullscreen mode Exit fullscreen mode

Check status:

sudo systemctl status subdomain_be
Enter fullscreen mode Exit fullscreen mode

6. Configure Nginx for the Subdomain

sudo nano /etc/nginx/sites-available/api-subdomain
Enter fullscreen mode Exit fullscreen mode

Paste:

server {
    listen 80;
    server_name api-subdomain.mydomain.co.ke;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /var/www/subdomain_be;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/var/www/subdomain_be/subdomain_be.sock;
    }
}
Enter fullscreen mode Exit fullscreen mode

Enable the config:

sudo ln -s /etc/nginx/sites-available/api-subdomain /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx
Enter fullscreen mode Exit fullscreen mode

7. (Optional but Recommended) Set Up HTTPS with Let’s Encrypt

Install Certbot:

sudo apt install certbot python3-certbot-nginx
Enter fullscreen mode Exit fullscreen mode

Run it:

sudo certbot --nginx -d api-subdomain.mydomain.co.ke
Enter fullscreen mode Exit fullscreen mode

Let Certbot update your Nginx config automatically.


8. Verify

Visit:
http://api-subdomain.mydomain.co.ke
or
https://api-subdomain.mydomain.co.ke (if SSL is enabled)

Your Django API should now be live.

Postmark Image

The email service that speaks your language

Whether you code in Ruby, PHP, Python, C#, or Rails, Postmark's robust API libraries make integration a breeze. Plus, bootstrapping your startup? Get 20% off your first three months!

Start free

Top comments (0)

ACI image

ACI.dev: Fully Open-source AI Agent Tool-Use Infra (Composio Alternative)

100% open-source tool-use platform (backend, dev portal, integration library, SDK/MCP) that connects your AI agents to 600+ tools with multi-tenant auth, granular permissions, and access through direct function calling or a unified MCP server.

Check out our GitHub!