How to Set Up Nginx with Django and Gunicorn

How to Set Up Nginx with Django and Gunicorn

Setting up Nginx with Django and Gunicorn allows you to deploy a Django web application with a production-ready web server and a reverse proxy for serving HTTP requests. Gunicorn (Green Unicorn) is a WSGI HTTP server for running Python web applications, while Nginx acts as a reverse proxy to handle incoming requests. Here’s how to set up this configuration:

1. Install Required Software:

Before you start, ensure that you have Python, Django, Gunicorn, and Nginx installed on your server.

  • Install Django and Gunicorn using pip:
Bash
   pip install django gunicorn
  • Install Nginx using your package manager. On Ubuntu, you can use:
Bash
   sudo apt update
   sudo apt install nginx

2. Configure Django and Gunicorn:

a. Navigate to your Django project directory and create a Gunicorn systemd service unit file. For example:

Bash
   sudo nano /etc/systemd/system/myproject_gunicorn.service

Replace myproject with your project name. Add the following content to the file:

Bash
   [Unit]
   Description=gunicorn daemon for myproject
   After=network.target

   [Service]
   User=your_username  # Replace with your username
   Group=group_name    # Replace with your group name
   WorkingDirectory=/path/to/your/project
   ExecStart=/usr/local/bin/gunicorn --workers 3 --bind unix:/path/to/your/project/myproject.sock myproject.wsgi:application

   [Install]
   WantedBy=multi-user.target

Save the file and exit the text editor.

b. Replace your_username, group_name, /path/to/your/project, and myproject with appropriate values for your setup.

c. Start and enable the Gunicorn service:

Bash
   sudo systemctl start myproject_gunicorn
   sudo systemctl enable myproject_gunicorn

3. Configure Nginx:

a. Create an Nginx server block configuration file for your Django project. For example:

Bash
   sudo nano /etc/nginx/sites-available/myproject

Add the following content to the file:

Bash
   server {
       listen 80;
       server_name your_domain.com www.your_domain.com;

       location = /favicon.ico { access_log off; log_not_found off; }
       location /static/ {
           root /path/to/your/project;
       }

       location / {
           include proxy_params;
           proxy_pass http://unix:/path/to/your/project/myproject.sock;
       }
   }

Replace your_domain.com, /path/to/your/project, and myproject with your domain, project path, and Gunicorn socket name.

b. Create a symbolic link to enable the Nginx server block:

Bash
   sudo ln -s /etc/nginx/sites-available/myproject /etc/nginx/sites-enabled

c. Test the Nginx configuration and reload Nginx:

Bash
   sudo nginx -t
   sudo systemctl reload nginx

4. Configure Django Settings:

In your Django project’s settings, make sure you have the ALLOWED_HOSTS setting configured to include your domain name:

Bash
ALLOWED_HOSTS = ['your_domain.com', 'www.your_domain.com']

5. Configure Your Domain:

Update your DNS settings to point your domain to your server’s IP address.

6. Secure Your Application:

Consider configuring SSL/TLS to encrypt data between the client and server. You can use Let’s Encrypt to obtain free SSL certificates and configure Nginx to use them.

7. Test Your Application:

Restart Gunicorn and Nginx to apply the changes:

Bash
sudo systemctl restart myproject_gunicorn
sudo systemctl restart nginx

Visit your domain in a web browser to verify that your Django application is running through Gunicorn and Nginx.

By following these steps, you can set up Nginx with Django and Gunicorn to deploy your Django web application for production use.

Total
0
Shares

Leave a Reply

Previous Post
How to Use Variables in Nginx Configuration

How to Use Variables in Nginx Configuration

Next Post
How to Set Up Nginx with Apache as a Reverse Proxy

How to Set Up Nginx with Apache as a Reverse Proxy

Related Posts