How to set up Gunicorn and Nginx for Django? IntroductionDjango is a high-level Python web framework that allows rapid development of secure and scalable websites. It is an excellent choice when you need more control and dedicated resources, which is why it works especially well on VPS Hosting.In this guide, you will learn how to deploy a Django application using Gunicorn and Nginx on a VPS hosting environment. This method is ideal for production deployments that prioritize performance, security, and flexibility.You will set up a virtual environment, install Django and Gunicorn, and configure Nginx to serve your Django application efficiently on your VPS. This approach gives you full server-level control and is recommended for serious or growing Django projects.Note: You’ll need root or sudo access to your VPS to follow these steps. Prerequisites Before you begin:A VPS with Ubuntu 22.04 or later.Root or sudo user access.A domain name (optional but recommended).Basic familiarity with Linux commands. Step 1 – Installing Python and Nginx First, update your server packages:Then install Python, Pip, and Nginx:Tip: Make sure Nginx is running: # sudo apt update # sudo apt install python3-pip python3-dev nginx #sudo systemctl status nginx Step 2 – Creating a Python Virtual EnvironmentInstall the virtual environment package:Create your project directory:Create and activate a virtual environment:Note: Once activated, your prompt will change to (env), confirming the environment is active. # sudo pip3 install virtualenv # mkdir ~/projectdir # cd ~/projectdir # virtualenv env # source env/bin/activate Step 3 – Installing Django and GunicornInstall Django and Gunicorn in your virtual environment:You can verify the installation: # pip install django gunicorn # django-admin --version # gunicorn --version Step 4 – Setting Up Your Django ProjectYou can copy your existing Django project into ~/projectdir/ — or create a new one:Open the project’s settings file:Update the ALLOWED_HOSTS variable:Run database migrations:Open port 8000 in the firewall:Start Django’s development server to test your setup: # django-admin startproject myproject ~/projectdir # nano ~/projectdir/myproject/settings.py ALLOWED_HOSTS = ['your_server_ip', 'your_domain.com'] # python manage.py makemigrations # python manage.py migrate # sudo ufw allow 8000 # python manage.py runserver 0.0.0.0:8000 Now visit:http://<your-server-ip>:8000If you see the Django welcome page, your project is working. Step 5 – Configuring Gunicorn Test Gunicorn’s ability to serve the application:You can now access your Django app at:http://<your-server-ip>:8000Once verified, stop Gunicorn with CTRL + C. # gunicorn –bind 0.0.0.0:8000 myproject.wsgi:application Step 6 – Creating a systemd Service for Gunicorn To manage Gunicorn as a background service, create a systemd unit file: # sudo nano /etc/systemd/system/gunicorn.service Add the following configuration: [Unit] Description=Gunicorn daemon for Django project After=network.target [Service] User=root Group=www-data WorkingDirectory=/home/username/projectdir ExecStart=/home/username/projectdir/env/bin/gunicorn –workers 3 –bind unix:/home/username/projectdir/myproject.sock myproject.wsgi:application [Install] WantedBy=multi-user.target Save and close the file, then start and enable the service: # sudo systemctl start gunicorn # sudo systemctl enable gunicorn # sudo systemctl status gunicorn Note: If you make changes, reload systemd # sudo systemctl daemon-reload Step 7 – Configuring Nginx as a Reverse Proxy Create an Nginx configuration file:Add this configuration: # sudo nano /etc/nginx/sites-available/myproject server { listen 80; server_name your_domain.com your_server_ip; location = /favicon.ico { access_log off; log_not_found off; } location /static/ { root /home/username/projectdir; } location / { include proxy_params; proxy_pass http://unix:/home/username/projectdir/myproject.sock; } } Add this configuration:Enable the configuration and restart Nginx:You can now access your Django app using your domain or IP. # sudo ln -s /etc/nginx/sites-available/myproject /etc/nginx/sites-enabled # sudo nginx -t # sudo systemctl restart nginx Step 8 – Enabling Firewall and Testing SetupAllow Nginx through the firewall:Reload UFW: # sudo ufw allow ‘Nginx Full’ # sudo ufw delete allow 8000 # sudo ufw reload Now open your browser and visit:http://your_domain.comYour Django app should be running behind Nginx and Gunicorn. Common Issues and FixesIssueCauseSolution502 Bad GatewayGunicorn service not runningRun sudo systemctl restart gunicornPermission DeniedIncorrect file or folder permissionsEnsure /home/username/projectdir is owned by www-data or rootStatic files not loadingMissing static configurationRun python manage.py collectstaticGunicorn not foundvenv path incorrect in service fileUpdate ExecStart path in gunicorn.service Conclusion Django applications can be deployed on both shared hosting and VPS environments using standard, well-supported setups. On shared hosting, cPanel with CloudLinux Passenger provides a managed approach that handles virtual environments and application startup with minimal configuration. This option is suitable for smaller projects and straightforward deployments.On a VPS, using Gunicorn with Nginx offers full control over the server and application behavior. This setup is better suited for applications that require higher performance, persistent processes, or custom server configuration. In both cases, using virtual environments, matching Python and Django versions, managing static files correctly, and restarting services after changes are essential for a stable deployment. Need Help? If you require assistance at any point while using this guide, our Support Team is here to help: Email: support@bigcloudy.com Submit a support ticket Confirm your purchase by clicking “Continue” button. FAQ Can I use this setup for multiple Django apps?Yes, create separate Gunicorn service and Nginx config files for each app. How can I enable HTTPS?Install and configure Certbot for Nginx: # sudo apt install certbot python3-certbot-nginx # sudo certbot –nginx -d your_domain.com What’s the advantage of using Gunicorn with Nginx?Gunicorn serves Django efficiently, while Nginx handles static files, caching, and request load balancing. How to restart the Gunicorn service?# sudo systemctl restart gunicorn How can I check Gunicorn logs?# sudo journalctl -u gunicorn