- Регистрация
- 1 Мар 2015
- Сообщения
- 1,481
- Баллы
- 155
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):
2. Install Required Packages on Contabo (Ubuntu)
sudo apt update
sudo apt install python3-pip python3-venv nginx git
pip install gunicorn
3. Clone Your Django App to the Server
cd /var/www/
sudo git clone subdomain_be
cd subdomain_be
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt
Run Django migrations:
python manage.py migrate
Collect static files:
python manage.py collectstatic
4. Test Gunicorn
Run Gunicorn to make sure your app runs:
gunicorn --bind 127.0.0.1:8000 django_project_backend.wsgi:application
Create the file:
sudo nano /etc/systemd/system/subdomain_be.service
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
Start and enable it:
sudo systemctl daemon-reexec
sudo systemctl daemon-reload
sudo systemctl start subdomain_be
sudo systemctl enable subdomain_be
Check status:
sudo systemctl status subdomain_be
6. Configure Nginx for the Subdomain
sudo nano /etc/nginx/sites-available/api-subdomain
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;
}
}
Enable the config:
sudo ln -s /etc/nginx/sites-available/api-subdomain /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx
7. (Optional but Recommended) Set Up HTTPS with Let’s Encrypt
Install Certbot:
sudo apt install certbot python3-certbot-nginx
Run it:
sudo certbot --nginx -d api-subdomain.mydomain.co.ke
Let Certbot update your Nginx config automatically.
8. Verify
Visit:
or
(if SSL is enabled)
Your Django API should now be live.
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
To confirm that the subdomain has propagated by ping. i.e. ping api-subdomain.domain.com? It may take up to 10–30 minutes for DNS to propagate.
2. Install Required Packages on Contabo (Ubuntu)
sudo apt update
sudo apt install python3-pip python3-venv nginx git
pip install gunicorn
3. Clone Your Django App to the Server
cd /var/www/
sudo git clone subdomain_be
cd subdomain_be
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt
Run Django migrations:
python manage.py migrate
Collect static files:
python manage.py collectstatic
4. Test Gunicorn
Run Gunicorn to make sure your app runs:
gunicorn --bind 127.0.0.1:8000 django_project_backend.wsgi:application
5. Set Up a Systemd Service for GunicornReplace django_project_backend with your Django project name (same folder with settings.py).
Create the file:
sudo nano /etc/systemd/system/subdomain_be.service
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
Start and enable it:
sudo systemctl daemon-reexec
sudo systemctl daemon-reload
sudo systemctl start subdomain_be
sudo systemctl enable subdomain_be
Check status:
sudo systemctl status subdomain_be
6. Configure Nginx for the Subdomain
sudo nano /etc/nginx/sites-available/api-subdomain
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;
}
}
Enable the config:
sudo ln -s /etc/nginx/sites-available/api-subdomain /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx
7. (Optional but Recommended) Set Up HTTPS with Let’s Encrypt
Install Certbot:
sudo apt install certbot python3-certbot-nginx
Run it:
sudo certbot --nginx -d api-subdomain.mydomain.co.ke
Let Certbot update your Nginx config automatically.
8. Verify
Visit:
or
(if SSL is enabled)
Your Django API should now be live.