Setting Nginx Django App with Postgres and Gunicorn on Ubuntu and Windows

Setting up a Nginx Django web application with a powerful stack like Postgres, and Gunicorn can greatly enhance its performance and scalability. In this guide, we will walk you through the process of configuring Nginx Django with Postgres as the database, Nginx as the web server, and Gunicorn as the application server on both Ubuntu and Windows operating systems.

Nginx and Django Integration

Nginx and Django are two powerful tools commonly used in web development. Nginx is a high-performance web server and reverse proxy, while Django is a popular Python web framework. Integrating Nginx with Django can greatly enhance the performance, scalability, and security of your web application.

Nginx acts as a middle layer between the client and Django, handling various tasks such as load balancing, caching, serving static files, and proxying requests to the Django application server. This integration allows Nginx to efficiently handle incoming web traffic, offload resource-intensive tasks from Django, and provide additional functionalities to optimize the overall performance of your application.

Prerequisites:

  • A working Django project
  • A running Postgres database
  • Ubuntu or Windows operating system

Nginx Django :Installing and Configuring

Before we get into installing and configuring Nginx Django app you must understand the Role of Nginx in Django Deployment.

In a typical Django deployment, Nginx plays a crucial role in managing the flow of incoming requests. When a client makes a request to your application, it first goes through Nginx, which acts as a reverse proxy. Nginx can then perform various tasks before forwarding the request to the Django application server.

One of the primary functions of Nginx is load balancing. With Nginx, you can distribute incoming requests across multiple instances of your Django application, ensuring optimal utilization of server resources and improving the overall responsiveness of your application.

Nginx also excels at serving static files efficiently. By configuring Nginx to handle static files directly, you can reduce the load on Django, allowing it to focus on processing dynamic requests. This approach significantly improves the performance of your application, especially when dealing with large volumes of static files. Let’s get started!

Install Postgres On Ubuntu

  • Open the terminal and run the following commands:
sudo apt update
sudo apt install postgresql postgresql-contrib
  • Configure Postgres: Create a new database and user for your Django project. Replace your_database_name, your_username, and your_password with your desired values:
sudo -u postgres psql
CREATE DATABASE your_database_name;
CREATE USER your_username WITH PASSWORD 'your_password';
GRANT ALL PRIVILEGES ON DATABASE your_database_name TO your_username;
\q

Download and Install Postgres On Windows

  1. Visit the official PostgreSQL website and download the installer suitable for your Windows version. Follow the installation wizard to complete the setup.
  2. Launch the pgAdmin application and create a new database and user for your Django project. Take note of the database name, username, and password for later use.

Install and Configure Django

  • Open the terminal or command prompt and run the following command:
pip install django
  • In your Django project’s settings.py file, update the database settings with the Postgres details:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'your_database_name',
'USER': 'your_username',
'PASSWORD': 'your_password',
'HOST': 'localhost',
'PORT': '',
}
}

Installing and Configuring Nginx on Ubuntu

  • Open the terminal and run the following command:
sudo apt install nginx
  • Create a new server block configuration file:
sudo nano /etc/nginx/sites-available/your_project_name
  • Inside the file, add the following configuration:
server {
listen 80;
server_name your_domain_or_ip;

location / {
proxy_pass http://localhost:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
  • Save the file and exit the editor. Then, enable the new server block and restart Nginx:
sudo ln -s /etc/nginx/sites-available/your_project_name /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx

Please note above steps are just a basic steps to configure Nginx Django, For complete and deep Nginx config please read our article on Install Nginx and Nginx Config.

Installing and Configuring Nginx on Windows

  • Download and Install Nginx: Visit the official Nginx website and download the Windows version. Extract the downloaded package and place it in a desired location.
  • Configure Nginx: Open the nginx.conf file located in the extracted Nginx directory. Inside the http block, add the following configuration:
server {
listen 80;
server_name your_domain_or_ip;

location / {
proxy_pass http://localhost:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}

Please replace your_domain_or_ip with your actual domain name or IP address.

  • Save the changes to the nginx.conf file and start Nginx by running the following command in the command prompt:
nginx.exe

Installing and Configuring Gunicorn

  • Open the terminal or command prompt and run the following command:
pip install gunicorn
  • Navigate to the root directory of your Django project where the “manage.py” file is located. Run the following command to start Gunicorn:
gunicorn your_project_name.wsgi:application --bind localhost:8000

Testing Your App

 

  • Open your web browser and visit http://your_domain_or_ip. You should see your Django application running successfully.

Congratulations! You have successfully set up Django with Postgres, Nginx, and Gunicorn on both Ubuntu and Windows operating systems. This powerful stack will provide a robust foundation for your web application, ensuring efficient handling of requests and reliable database management. Happy coding!

Nginx Django Benefits

  1. Nginx’s efficient handling of requests, load balancing capabilities, and optimized serving of static files contribute to improved performance and reduced response times for your Django application. It allows you to handle higher traffic loads and delivers a smoother user experience.
  2. Nginx’s load balancing feature enables easy scalability by distributing requests across multiple Django application instances. This horizontal scaling approach ensures that your application can handle increased traffic and provides high availability by preventing any single point of failure.
  3. Nginx acts as a reverse proxy and can implement security measures such as request filtering, rate limiting, and SSL/TLS encryption. These features protect your Django application from various types of attacks, ensuring the security and integrity of user data.
  4. By offloading the serving of static files to Nginx, you reduce the workload on Django, enabling it to focus on dynamic content generation. Nginx’s efficient file-serving capabilities enhance the overall performance and responsiveness of your application.
  5. Nginx provides a flexible and powerful configuration system that allows you to customize various aspects of your Django application’s deployment. You can fine-tune Nginx settings to optimize performance, caching, and security based on your application’s specific requirements.
Scroll to Top