Deploy Django using Nginx with gunicorn

Posted by

Deploy Django desde Nginx con gunicorn

Deploy Django desde Nginx con gunicorn

Deploying a Django application using Nginx with gunicorn can help you to create a high performance, robust web server setup. In this article, we will walk through the steps to deploy Django with Nginx and gunicorn.

Step 1: Install and configure gunicorn

First, you need to install gunicorn by running the following command:


pip install gunicorn

Then, create a gunicorn configuration file for your Django project. You can create a file named gunicorn_config.py with the following content:


bind = '127.0.0.1:8000'
workers = 3

Step 2: Start gunicorn server

Run the following command to start the gunicorn server:


gunicorn your_project.wsgi:application -c gunicorn_config.py

Step 3: Install and configure Nginx

Install Nginx by running the following command:


sudo apt-get install nginx

Edit the Nginx configuration file /etc/nginx/nginx.conf and add the following configuration inside the http block:


server {
listen 80;
server_name your_domain.com;

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

Then, restart Nginx by running:


sudo service nginx restart

Step 4: Test the deployment

Access your Django application in a web browser using your domain name. If everything is correctly configured, you should see your Django application running smoothly with Nginx and gunicorn.

Congratulations! You have successfully deployed your Django application using Nginx with gunicorn.