How do I host a Django site?
Hosting a Django Application on Linux
Hosting a Django site requires a Linux server environment with Python 3 installed, a WSGI-compatible server, and a reverse proxy. At HostingDuty, we provide VPS and Cloud hosting plans that give you root access to configure these components. You cannot host Django on standard shared hosting because it requires background processes and specific Python dependencies.
Prerequisites and Server Setup
Before deploying, ensure your server meets the following requirements:
- Operating System: Ubuntu 20.04/22.04 LTS or Debian 11/12
- Python Version: Python 3.8 or higher
- WSGI Server: Gunicorn or uWSGI
- Web Server: Nginx or Apache
- Process Manager: systemd or Supervisor
sudo apt update && sudo apt install python3-pip python3-venv nginx. For detailed setup instructions, refer to our tutorial on setting up a Python environment. If you need help with server configuration, check our knowledge base entry on Linux server basics.Configuring the WSGI Application
Django applications require a WSGI (Web Server Gateway Interface) file to communicate with the web server. This file is typically named wsgi.py and is generated automatically when you create a new Django project. The WSGI file tells the server how to load your application and handle requests.
You must configure the WSGI file to point to your Django project's settings module. For example, if your project is named myproject, the WSGI file should include os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings'). This configuration ensures that Django loads the correct settings for production.
Virtual Environment and Dependencies
Always deploy your Django app inside a Python virtual environment. This isolates your project dependencies from the system Python and prevents conflicts. Create a virtual environment using python3 -m venv venv, then activate it with source venv/bin/activate. Install your project dependencies using pip install -r requirements.txt.
We recommend using a requirements.txt file generated from your development environment. To export your dependencies, run pip freeze > requirements.txt. For more information on managing Python packages, see our glossary entry on virtual environments.
Process Management and Systemd
To keep your Django application running continuously, you need a process manager. systemd is the standard on modern Linux distributions. Create a service file at /etc/systemd/system/django-app.service with the following configuration:
``ini
[Unit]
Description=Django Application
After=network.target
[Service] User=www-data WorkingDirectory=/var/www/myproject ExecStart=/var/www/myproject/venv/bin/gunicorn --access-logfile - --workers 3 --bind 127.0.0.1:8000 myproject.wsgi:application
[Install]
WantedBy=multi-user.target
`
Enable and start the service using sudo systemctl enable django-app && sudo systemctl start django-app. For alternative process managers, check our QA entry on Supervisor vs systemd.
Reverse Proxy with Nginx
Nginx acts as a reverse proxy to handle static files, SSL termination, and load balancing. Configure Nginx to forward requests to your Gunicorn instance on port 8000. A typical Nginx configuration includes location / { proxy_pass http://127.0.0.1:8000; } and static file handling for /static/ and /media/ directories.
For production deployments, we strongly recommend using HTTPS. You can obtain free SSL certificates from Let's Encrypt using Certbot. For SSL configuration guidance, see our SSL setup guide.
Managed Hosting Options
If you prefer not to manage the server yourself, HostingDuty offers managed Django hosting on our Cloud platform. This includes automated deployments, backups, and monitoring. For a comparison between VPS and Cloud hosting, see our VPS vs Cloud hosting comparison.
Security Considerations
Always set DEBUG = False in your Django settings for production. Configure ALLOWED_HOSTS` to restrict access to your domain. Use environment variables for sensitive data like database credentials and secret keys. For security best practices, refer to our Django security checklist.
Monitoring and Maintenance
Set up logging and monitoring to track application performance. Django provides built-in logging, but you can also use tools like Sentry for error tracking. For deployment automation, consider using CI/CD pipelines with GitHub Actions or GitLab CI. For more on deployment strategies, see our deployment best practices guide.
By following these steps, you can host a secure and performant Django application on HostingDuty infrastructure. For additional support, contact our technical team through our support portal.
People also ask
- What is the best hosting for Django applications?
- How do I configure Gunicorn for Django?
- What are Django security best practices?
- How do I deploy Django on a VPS?
- What is the difference between WSGI and ASGI?
- How do I set up SSL for a Django site?