Table of Contents
What you will read?
- 1 Update CentOS and enable repositories
- 2 Install Python 3 and build essentials
- 3 Create a project folder and virtual environment
- 4 Install Django with pip
- 5 Start a new Django project
- 6 Run the development server and open the firewall
- 7 Optional: Create a superuser
- 8 Optional: Run Django with Gunicorn and systemd
Installing Django on CentOS Stream 9 is fast when you use Python 3 and a virtual environment. The workflow: update packages, install Python and tools, isolate dependencies, create a project, open the firewall, and optionally run it under systemd with Gunicorn. Commands assume sudo privileges and a clean server.
Update CentOS and enable repositories
Refresh packages, enable CRB and EPEL for a wider software set and smoother builds.
sudo dnf -y update
sudo dnf -y install epel-release
sudo dnf config-manager --set-enabled crb
Output example:
Last metadata expiration check: 0:12:03 ago on ...
Dependencies resolved.
Complete!
Install Python 3 and build essentials
Install Python, pip, and common headers used by Python packages.
sudo dnf -y install python3 python3-pip python3-devel gcc openssl-devel bzip2-devel libffi-devel
python3 --version
pip3 --version
Output example:
Python 3.9.18
pip 23.3.1 from /usr/lib/python3.9/site-packages/pip (python 3.9)
Create a project folder and virtual environment
Keep Django isolated from system Python using venv in a dedicated directory.
sudo mkdir -p /opt/djangoapp
sudo chown -R $USER:$USER /opt/djangoapp
cd /opt/djangoapp
python3 -m venv .venv
source .venv/bin/activate
python -m pip install --upgrade pip setuptools wheel
Output example:
(.venv) [user@centos /opt/djangoapp]$
Install Django with pip
Pull the latest stable Django into the virtual environment.
pip install Django
Output example:
Successfully installed asgiref-3.8.x Django-5.x sqlparse-0.5.x
Start a new Django project
Create the project skeleton and initialize the database.
django-admin startproject mysite
cd mysite
python manage.py migrate
Output example:
Applying contenttypes.0001_initial... OK
Applying auth.0012_alter_user... OK
Run the development server and open the firewall
Allow port 8000 through firewalld and serve the app for testing.
sudo firewall-cmd --add-port=8000/tcp --permanent
sudo firewall-cmd --reload
python manage.py runserver 0.0.0.0:8000
Output example:
Starting development server at http://0.0.0.0:8000/
Open a browser to http://SERVER_IP:8000 to confirm the Django welcome page.
Optional: Create a superuser
Add an admin account for accessing the Django admin panel.
(.venv) python manage.py createsuperuser
Output example:
Username: admin
Email address: [email protected]
Password: ********
Optional: Run Django with Gunicorn and systemd
Use Gunicorn as a WSGI server and manage it with systemd for reliability.
# still inside the virtualenv and /opt/djangoapp/mysite
pip install gunicorn
deactivate
sudo useradd --system --create-home --shell /sbin/nologin django
sudo chown -R django:django /opt/djangoapp
Create the unit file:
sudo nano /etc/systemd/system/mysite.service
[Unit]
Description=Gunicorn for Django mysite
After=network.target
[Service]
User=django
Group=django
WorkingDirectory=/opt/djangoapp/mysite
Environment=PATH=/opt/djangoapp/.venv/bin
ExecStart=/opt/djangoapp/.venv/bin/gunicorn --workers 3 --bind 0.0.0.0:8000 mysite.wsgi:application
Restart=on-failure
[Install]
WantedBy=multi-user.target
Enable and start the service:
sudo systemctl daemon-reload
sudo systemctl enable --now mysite
sudo systemctl status mysite --no-pager
Output example:
Active: active (running) ... ExecStart=/opt/djangoapp/.venv/bin/gunicorn ...
For production, place Nginx in front of Gunicorn and enable HTTPS. Keep SELinux enforcing; no change is needed for port 8000. If you proxy via Nginx on 80/443, skip opening 8000 publicly. Installation is complete: Django is now running on CentOS Stream 9 with a clean virtual environment, proper firewall configuration, and an optional systemd service.
For more guides, visit dropvps.com — you can also explore DropVPS for server purchases, technical support, and more resources.
