A few months ago, my cousin asked me to help her launch her first web app. She had built it using Django, a web framework for Python. She had no idea how to put it online. I told her, “Don’t worry, I’ll walk you through it.” We sat down, grabbed some coffee, and by the end of the day, her app was live on the internet — running on a Linux server.
If you’re in the same place, don’t worry either. Hosting a Django app might sound hard at first, but it’s actually not too bad when you break it down. In this guide, I’ll show you how to set up your own Linux server for Django.
I’ll explain the steps in a simple way. Even if this is your first time using a Linux server, you’ll be able to follow along.
What is Django?
Let’s start with the basics.
Django (say it like “JANG-oh”) is a web framework written in Python. A web framework is a set of tools and rules that make it easier to build websites or web apps.
Django helps you build web apps fast, safely, and cleanly. Lots of popular sites like Instagram and Pinterest use it.
What You’ll Need
Before we dive in, let’s check that you have everything ready.
You’ll need:
- A Linux server (Ubuntu 20.04 or newer is great)
- SSH access (so you can connect to the server)
- A Django project (already created on your computer)
- A domain name (optional, but useful)
- Basic knowledge of Python and Linux commands
We’ll use a few tools in this tutorial. Don’t worry — I’ll explain each one as we go.
Step 1: Connect to Your Server
First, use SSH to connect to your server. On your computer, open the terminal and run:
ssh your-username@your-server-ip
Replace your-username
and your-server-ip
with your own info.
If it’s your first time connecting, your terminal might ask if you trust the server. Type “yes.”
Step 2: Update Your Server
Before installing anything, update your packages:
sudo apt update
sudo apt upgrade
This makes sure everything is up to date.
Step 3: Install Python and Pip
Django runs on Python. Most Linux servers already have Python, but it’s good to check.
Run:
python3 --version
You should see something like Python 3.8.10
. If it shows an error, install Python:
sudo apt install python3 python3-pip
Pip is Python’s package installer. You’ll need it to install Django and other tools.
Step 4: Install a Virtual Environment
A virtual environment keeps your project’s packages separate from the system. That way, different projects don’t mess each other up.
To install it:
sudo apt install python3-venv
Then, go to the folder where you want to keep your Django project:
mkdir myproject
cd myproject
Create the virtual environment:
python3 -m venv venv
Activate it:
source venv/bin/activate
You’ll now see (venv)
at the start of your terminal line.
Step 5: Install Django
With the virtual environment active, install Django:
pip install django
If you already have your Django project on your computer, you can upload it using SCP or Git. If you want to start a new one:
django-admin startproject mysite
Step 6: Test Your Project
Go into your project folder:
cd mysite
Then run the server:
python manage.py runserver 0.0.0.0:8000
This tells Django to listen on all IP addresses, on port 8000.
Open a browser and go to http://your-server-ip:8000
. If everything worked, you’ll see the Django welcome page.
Step 7: Set Up Gunicorn
Gunicorn (say “GUN-i-corn”) is a web server for Python apps. It helps your Django project run faster and more safely.
To install:
pip install gunicorn
Now try running your app with Gunicorn:
gunicorn --bind 0.0.0.0:8000 mysite.wsgi
It should work just like before — but now it’s more ready for production.
Step 8: Set Up Nginx
Nginx is a reverse proxy. It sits in front of Gunicorn and handles things like:
- Serving static files
- Redirecting traffic to HTTPS
- Keeping your site secure and fast
Install Nginx:
sudo apt install nginx
Create a config file for your site:
sudo nano /etc/nginx/sites-available/mysite
Paste in something like this:
server {
listen 80;
server_name yourdomain.com;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/youruser/myproject/mysite;
}
location / {
include proxy_params;
proxy_pass http://127.0.0.1:8000;
}
}
Save and exit (press CTRL+O
, then ENTER
, then CTRL+X
).
Link the config:
sudo ln -s /etc/nginx/sites-available/mysite /etc/nginx/sites-enabled
Test it:
sudo nginx -t
If it says “OK”, restart Nginx:
sudo systemctl restart nginx
Step 9: Set Up Static Files
Django doesn’t serve static files (like CSS or images) in production. You need to collect them:
Edit settings.py
in your Django project and add:
STATIC_ROOT = BASE_DIR / 'static/'
Then run:
python manage.py collectstatic
This puts all your static files in one folder, which Nginx can serve.
Step 10: Set Up Automatic Start with Systemd
You want Gunicorn to start when your server boots.
Create a systemd service:
sudo nano /etc/systemd/system/gunicorn.service
Add:
[Unit]
Description=gunicorn daemon
After=network.target
[Service]
User=youruser
Group=www-data
WorkingDirectory=/home/youruser/myproject/mysite
ExecStart=/home/youruser/myproject/venv/bin/gunicorn --access-logfile - --workers 3 --bind unix:/home/youruser/myproject/mysite.sock mysite.wsgi:application
[Install]
WantedBy=multi-user.target
Save, then run:
sudo systemctl start gunicorn
sudo systemctl enable gunicorn
Now Gunicorn will run even after a reboot.
Two Handy Lists
Main Tools You’ll Use:
- Django – the framework your app is built on
- Gunicorn – serves your Django app
- Nginx – acts as a front-end and static file handler
- Systemd – runs your app as a service
- Virtualenv – keeps your Python packages clean
Common Problems to Watch For:
- Nginx not restarting? Check config with
sudo nginx -t
- Static files missing? Make sure
collectstatic
was run - Gunicorn not starting? Check
journalctl -u gunicorn
- 502 Bad Gateway? Usually means Gunicorn isn’t running
Final Thoughts
Hosting Django on a Linux server can seem scary at first. But once you understand how the pieces fit — Python, Gunicorn, Nginx — it starts to make sense.
I’ve used this setup on personal projects, client websites, and student apps. It works well and gives you control. You don’t need to rely on platforms like Heroku or shared hosting.
You now have a strong foundation. Your app is live, secure, and ready for users.
What will you build next?