Keeping Your Django Server Alive on AWS Even After You Log Out

Keeping Your Django Server Alive on AWS Even After You Log Out

A Practical Guide to nohup, screen, and Gunicorn Deployment

In this blog, I walk you through why your Django server dies when you close the AWS terminal and how to keep it alive using screen.

7/26/2025
7
1
130

When you're developing or deploying Django apps on AWS, it's common to see your server stop working as soon as you disconnect from the SSH terminal. This guide walks through three reliable ways to prevent that:\n\n1. Use nohup to run your server in the background.\n2. Use screen/tmux to detach and reattach sessions.\n3. Use Gunicorn and systemd for production deployments.\n\nThis approach ensures your server stays alive and your development or deployment experience is stable and reliable.

Why My Django Server Stopped After Closing AWS Terminal

Understanding Why Your Server Dies When You Disconnect

When you run a Django (or any) server in an AWS EC2 terminal using:

python manage.py runserver

…it runs in the foreground. So when you close your terminal or lose your SSH connection, the process stops.

Solution: Run your server in the background or use a process manager.

🔹 Option 1: Use nohup (Quick & Easy)

nohup python manage.py runserver 0.0.0.0:8000 > server.log 2>&1 &

  • Prevents hangup with nohup
  • Redirects logs to server.log
  • Runs in the background with &

Check logs with:

tail -f server.log

🔹 Option 2: Use screen or tmux (Recommended for dev)

Install and start screen:

sudo apt install screen
screen

Then run:

python manage.py runserver 0.0.0.0:8000

Detach session with Ctrl+A then D. Reattach anytime with:

screen -r

🔹 Option 3: Use Gunicorn + systemd for production

Install Gunicorn:

pip install gunicorn

Run:

gunicorn your_project_name.wsgi:application --bind 0.0.0.0:8000

To make it persistent, use a systemd service:

[Unit] Description=Django App After=network.target [Service] User=ubuntu Group=www-data WorkingDirectory=/home/ubuntu/your_project/ ExecStart=/home/ubuntu/your_env/bin/gunicorn your_project.wsgi:application --bind 0.0.0.0:8000 Restart=always [Install] WantedBy=multi-user.target

Enable with:

sudo systemctl daemon-reexec
sudo systemctl enable django
sudo systemctl start django

Managing Multiple Django Screens on AWS EC2

Using screen to Keep Your Django Server Running Persistently

🔹 To run multiple screen sessions (e.g., Django server, Celery worker):

Start a named screen session:

screen -S django-server

Inside screen, run your Django server:

python manage.py runserver 0.0.0.0:8000

Detach the session:

Ctrl + A, then D

List active sessions:

screen -ls
11291.django-server (Detached)
11305.celery-worker (Detached)

Reattach a session:

screen -r django-server

Kill a session:

screen -S django-server -X quit

❗ Problem: If screen says it's already attached:

screen -r django-server
There is a screen on:
11291.django-server (Attached)
There is no screen to be resumed matching django-server.

✅ Fix it by forcing reattach:

screen -D -r django-server

Alternatively, reattach using the ID:

screen -D -r 11291