ryanam1
By:
ryanam1

How do I setup multiple Django sites using the One-Click install option for Ubuntu 16.04

August 2, 2017 420 views
Django Python Python Frameworks Nginx Ubuntu 16.04

I have successfully created the one-click Django droplet and the website works but I am unsure how to add other Django websites.

This is the process I have taken so far:

  1. I copied the default django_project folder in /home/django and renamed it.

  2. I copied the file in etc/nginx/sites-available and created the sym-link in etc/nginx/sites-enabled

  3. I then updated that file with the following:

upstream newsite_app_server {
    server unix:/home/django/gunicorn.socket fail_timeout=0;
}

server {
    listen 80;
    listen [::]:80;

    root /usr/share/nginx/html;
    index index.html index.htm;

    client_max_body_size 4G;
    server_name newsite.com www.newsite.com;

    keepalive_timeout 5;

    # Your Django project's media files - amend as required
    location /media  {
        alias /home/django/newsite/django_project/media;
    }

    # your Django project's static files - amend as required
    location /static {
        alias /home/django/newsite/django_project/static;
    }

    # Proxy the static assests for the Django Admin panel
    location /static/admin {
       alias /usr/lib/python2.7/dist-packages/django/contrib/admin/static/admin/;
    }

    location / {
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $host;
            proxy_redirect off;
            proxy_buffering off;

            proxy_pass http://newsite_app_server;
    }

}
  1. I restarted the server but then received an error about DisallowedHost, so I then added the domain name to the Allowed_Host line in the settings.py file, restarted Nginx and Gunicorn and still got the same error. I tried using the wildcard ['*'] with no luck.

Am I missing something? It seems as though I should create some other instance of gunicorn but I'm not sure.

I tried to follow the instructions for setting up multiple Django sites for Ubuntu 14.04 at the following link but the settings seem a little different compared to version 16.04:
https://www.digitalocean.com/community/questions/setup-multiple-django-websites-on-django-one-click-install-image-vps

2 Answers

I am having the same issue... Is there any solution?

Thx in advance.

I found the solution:

  • I created a new gunicorn service at - /etc/systemd/system/
[Unit]
Description=Gunicorn daemon for Django Project
Before=nginx.service
After=network.target

[Service]
WorkingDirectory=/home/django/django_project
ExecStart=/usr/bin/gunicorn --name="YOUR PROJECT NAME" --pythonpath=/home/django/"YOUR PROJECT NAME"  --bind unix:/home/django/"New socket: ie: gunicorn2.socket" --config /etc/gunicorn.d/gunicorn.py django_project.wsgi:application
Restart=always
SyslogIdentifier=gunicorn
User=django
Group=django


[Install]
WantedBy=multi-user.target

After that start the new service and the new socket, in the example gunicorn2.socket, will be created.
- service gunicorn2 start
Set the nginx conf with the second upstream calling to this new socket
- upstream newsite_app_server {
server unix:/home/django/gunicorn.socket2 fail_timeout=0;
}

and restart nginx service.

That works for me. Hope to help.

Have another answer? Share your knowledge.