When you're ready to take your Django project from your laptop to the real world, deployment becomes the next big step. It’s the bridge between “development” and “live website.” In this guide, we’ll walk through how to deploy a Django app on Linode using PostgreSQL. This tutorial is meant to be practical, detailed, and accessible—perfect for developers who know Django but may be new to cloud hosting or server management.
Why Linode and PostgreSQL?
Before we dive in, let’s quickly look at why these tools are worth using.
Linode is a developer-friendly cloud hosting provider that offers powerful Linux-based virtual servers (called Linodes) at affordable prices. Unlike shared hosting, Linode gives you full control over your server environment, which is critical for real-world Django apps.
PostgreSQL, on the other hand, is one of the most robust, scalable, and reliable relational databases out there. Django has excellent support for it, and it's used by companies like Instagram, Reddit, and Spotify. So if you’re serious about growing your app, this combination is solid.
1. Prerequisites: What You’ll Need
Let’s make sure you have all the tools and knowledge you need before we get started:
- A Django app ready to be deployed (ideally using virtualenv and environment variables).
- A Linode account (you can sign up at linode.com).
- Basic understanding of Linux terminal commands.
- Familiarity with Git, Python, and your Django project structure.
- Your domain name (optional, but recommended for production).
- A local PostgreSQL database (we’ll migrate this to the server).
2. Setting Up Your Linode Server
Once you’ve logged into Linode, follow these steps:
Step 1: Create a Linode Instance
- Choose a Linux distribution (we recommend Ubuntu 22.04 LTS).
- Select a plan (a Nanode or Shared CPU 1GB is good for small apps).
- Set root password and create your Linode.
Within a minute or two, your virtual server will be up and running.
Step 2: Access Your Server via SSH
On your local machine, open a terminal and connect:
ssh root@your-linode-ip-address
If it’s your first time connecting, type "yes" when prompted. Now you have root access.
3. Secure the Server and Create a New User
Using the root
account isn’t safe for daily operations, so let’s fix that:
Step 1: Add a New User
adduser django_user
Then give this user sudo (admin) privileges:
usermod -aG sudo django_user
Step 2: Set Up SSH for the New User
Log out of root and back in as django_user
using SSH. You can set up SSH keys for secure access later (highly recommended for production).
4. Install Required Software
Now, let’s install all the tools Django needs to run.
Update Packages
sudo apt update && sudo apt upgrade -y
Install Python, pip, and virtualenv
sudo apt install python3-pip python3-dev libpq-dev python3-venv -y
Install PostgreSQL
sudo apt install postgresql postgresql-contrib -y
5. Configure PostgreSQL for Django
Let’s set up a PostgreSQL database and user that Django can access.
Create the Database and User
Log into PostgreSQL:
sudo -u postgres psql
Then run:
CREATE DATABASE myprojectdb;
CREATE USER myprojectuser WITH PASSWORD 'your-secure-password';
ALTER ROLE myprojectuser SET client_encoding TO 'utf8';
ALTER ROLE myprojectuser SET default_transaction_isolation TO 'read committed';
ALTER ROLE myprojectuser SET timezone TO 'UTC';
GRANT ALL PRIVILEGES ON DATABASE myprojectdb TO myprojectuser;
\q
Now, you have a fresh PostgreSQL database ready for Django.
6. Upload Your Django App
Use Git to clone your project, or use scp to transfer files.
Clone via Git
git clone https://github.com/yourusername/yourproject.git
cd yourproject
Set Up Virtual Environment
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt
Make sure psycopg2
or psycopg2-binary
is included in requirements.txt
.
7. Configure Django Settings for Production
Open your settings.py
and make some essential changes:
Database Settings
Update DATABASES
:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'myprojectdb',
'USER': 'myprojectuser',
'PASSWORD': 'your-secure-password',
'HOST': 'localhost',
'PORT': '',
}
}
Security and Static Files
- Set
DEBUG = False
- Add your IP or domain to
ALLOWED_HOSTS
- Run:
python manage.py collectstatic
8. Set Up Gunicorn and Supervisor
Install Gunicorn
pip install gunicorn
Then test it locally:
gunicorn --bind 0.0.0.0:8000 myproject.wsgi
Install Supervisor to Keep Gunicorn Running
sudo apt install supervisor
Create a config file:
sudo nano /etc/supervisor/conf.d/myproject.conf
Paste:
[program:myproject]
directory=/home/django_user/yourproject
command=/home/django_user/yourproject/venv/bin/gunicorn myproject.wsgi:application --bind 127.0.0.1:8000
autostart=true
autorestart=true
stderr_logfile=/var/log/myproject.err.log
stdout_logfile=/var/log/myproject.out.log
user=django_user
Then run:
sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl start myproject
9. Set Up Nginx as a Reverse Proxy
Now, let’s make the app accessible via your domain or public IP.
Install Nginx
sudo apt install nginx
Create a new config file:
sudo nano /etc/nginx/sites-available/myproject
Paste:
server {
listen 80;
server_name yourdomain.com;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/django_user/yourproject;
}
location / {
include proxy_params;
proxy_pass http://127.0.0.1:8000;
}
}
Enable it:
sudo ln -s /etc/nginx/sites-available/myproject /etc/nginx/sites-enabled
sudo nginx -t
sudo systemctl restart nginx
10. Set Up a Firewall (Optional but Recommended)
sudo ufw allow OpenSSH
sudo ufw allow 'Nginx Full'
sudo ufw enable
11. Run Migrations and Create Superuser
Finally, finish setting up the database:
python manage.py makemigrations
python manage.py migrate
python manage.py createsuperuser
Now, you should be able to go to your IP or domain and see your live Django app!
Final Thoughts and Best Practices
Deploying a Django app on Linode with PostgreSQL is a major milestone. While there are many steps, they all serve a purpose—from securing the server to optimizing performance. Once you’ve done it once, it gets easier and more intuitive.
Here are some pro tips:
- Use HTTPS with Let’s Encrypt (look up Certbot).
- Set up automatic backups via Linode dashboard.
- Monitor your logs using
journalctl
, Supervisor logs, or Nginx logs. - Use environment variables (with
python-decouple
ordotenv
) to keep secrets safe.
Conclusion
Deploying your Django app to Linode with PostgreSQL gives you full control, strong performance, and real-world experience with production environments. While it takes time to learn, it pays off by making your skills sharper and your apps more scalable.
If you’re looking to grow as a developer, this type of hands-on deployment is the perfect next step. So go ahead—spin up that Linode and show the world what you’ve built.
Post a Comment