Skip to content

Instantly share code, notes, and snippets.

@Amar-Chaudhari
Last active December 4, 2020 17:27
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Amar-Chaudhari/0de5bf8d209614c59200043356f5cfa9 to your computer and use it in GitHub Desktop.
Save Amar-Chaudhari/0de5bf8d209614c59200043356f5cfa9 to your computer and use it in GitHub Desktop.
Nginx + Gunicorn + Django Centos 7

Nginx + Gunicorn + Django

Gunicorn configuration

Filename : /etc/systemd/system/gunicorn.service

[Unit]
Description=gunicorn daemon
After=network.target

[Service]
# Update all paths below
# Setting provided are for reference only !
User=webapp
Group=www
WorkingDirectory=/var/webapps/sbprod/django_project_dir
ExecStart=/var/webapps/virtual_sample_env/bin/gunicorn --workers 2 --bind unix:/var/webapps/run/SOMENAME.sock Django_project_name.wsgi:application

[Install]
WantedBy=multi-user.target

Nginx configuration

Domain : xyz.com

server {
     listen  80;
     server_name xyz.com;

     access_log /var/webapps/xyz.com/logs/access.log;
     error_log /var/webapps/xyz.com/logs/error.log;

     root /var/webapps/xyz.com/public_html/;
     index index.php index.html index.htm;

    location = /favicon.ico { access_log off; log_not_found off; }

    # serve static content
    location /static/ {
           # Update correct static folder path
            root /var/webapps/xyz/main/;
    }

    location / {
      proxy_set_header Host $http_host;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header X-Forwarded-Proto $scheme;

      # use the sock created in above gunicorn file
      proxy_pass http://unix:/var/webapps/run/SOMENAME.sock;
    }
}
@xcarlx
Copy link

xcarlx commented Nov 24, 2017

whats is configuration for two project django in gunicorn ?

@softmixt
Copy link

awesome works

@rmahmood19
Copy link

Do we have to create the sock file manually?

@Amar-Chaudhari
Copy link
Author

@rmahmood19 It will be created automatically when you start the service.

@rvipandey
Copy link

nginx service automatically getting closed several times in a day, could you please help me on that...i am using centos7 on vps.

@nadyrbek97
Copy link

is there any difference on CENTOS 8 ? my gunicorn.service file failed (((((((((

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment