Deploy Django with Nginx and Gunicorn: An Efficient and Fast Web Server Setup

When it comes to the deployment of your web application, the Django documentation recommends utilizing Apache and modwsgi. Nevertheless, an alternative method exists that can deliver superior performance and scalability: the deployment of Django with Nginx and the Gunicorn web server. In the following discourse, we shall explore the intricate process of setting up your Django application with Nginx and Gunicorn, presenting you with a lightweight and efficient configuration for your web server.

Setting Up Your Server

To get started, ensure that your server is ready for deployment. If you’re working with a small, single server setup, consider using Rackspace. Create an Ubuntu 10.10 instance with 256MB of RAM and set up a user for your application. Additionally, add your SSH key to the authorized_keys file for secure access.

Installing Nginx

Installing Nginx is a straightforward process. Follow these steps to install the latest stable release using the provided PPA repository:

  1. Run the following command to install the required package:

$ sudo apt-get install python-software-properties -y

  1. Switch to the root user:

$ sudo -s

  1. Add the Nginx stable repository:

$ apt-add-repository ppa:nginx/stable

  1. Update the package list:

$ apt-get update

  1. Install Nginx:

$ apt-get install nginx

  1. Exit the root user:

$ exit

Project Structure

In order to maintain a clean project structure, follow these guidelines:

  • The user under which the application will run is “webapp.”
  • Checkout your application to the /home/webapp directory.
  • Create the following subdirectories within /home/webapp:
  • /home/webapp/app
  • /home/webapp/app/static
  • /home/webapp/env

Note: Virtualenv is recommended for deploying the application.

Configuring Nginx and Gunicorn

To configure Nginx and Gunicorn, you need to create the following files within your project:

  1. nginx.conf:

server {

    listen 80;

    server_name webapp.org;

    access_log /home/webapp/access.log;

    error_log /home/webapp/error.log;

    location /static {

        root /home/webapp/app;

    }

    location / {

        proxy_pass http://127.0.0.1:8888;

    }

}

  1. gunicorn.conf.py:

bind = “127.0.0.1:8888”

logfile = “/home/webapp/gunicorn.log”

workers = 3

After creating these files, symlink the nginx.conf file to the server’s sites-enabled directory using the following command:

$ sudo ln -s /home/webapp/app/nginx.conf /etc/nginx/sites-enable/webapp.org

This configuration allows Nginx to serve the application’s static files directly, while proxying all other requests to the Gunicorn server.

Install Gunicorn into your application’s environment using the following command:

$ (env) pip install gunicorn

Running the Deployment

To run the deployment, follow these steps:

  1. Collect all the static files into the static directory:

$ (env) python manage.py collectstatic

  1. Restart Nginx to apply the new configuration:

$ sudo /etc/init.d/nginx restart

  1. Start the Gunicorn server:

$ (env) cd /home/webapp/app

$ (env) gunicorn_django -D -c gunicorn.conf.py

Congratulations! Your Django application is now successfully deployed using Nginx and Gunicorn.

Important Notes

Keep the following considerations in mind:

  • Adjust the permissions on the static directory if necessary.
  • The command used above starts Gunicorn as a daemon. Consider using a monitoring service like runit or supervisord for better control and management.
  • Database-specific configurations are not covered in this article, as they are identical to an Apache deployment.

Your feedback is highly appreciated. Please feel free to reach out via email with your comments, criticisms, and suggestions. Better yet, write your own article as a response. Let’s embrace the power of blogging.