A Practical Guide to nohup, screen, and Gunicorn Deployment
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 &
nohup
server.log
&
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
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