Setup Multiple Django Websites on Django One Click Install Image VPS

July 20, 2014 7.9k views

I have a Django One Click Install Image VPS on Ubuntu 14.04 and have a site running which has minimal traffic.
I need to host another independent Django website on it.
As I am new to webserver configuration. Could Someone please guide me through the NGINX and Gunicorn Upstart configuration to host multiple sites.
Consider my Sites are:
www.mysite1.com
www.mysite2.com
I have already referred the questions:

https://www.digitalocean.com/community/questions/how-can-i-host-multiple-websites-on-a-droplet-using-nginx

But it does not mention the NINX and Gunicorn configuration to serve 2 independent websites on the same server.

1 comment
  • How would we host a second site in OneClickInstallation if its settings are based on Virtual environment. Settings explained here are without virtualenv.

4 Answers

Refer to this article for the specifics on how the the Django one-click app is configured:

How To Use the Django One-Click Install Image

Copy the Nginx configuration for your new site:

cp /etc/nginx/sites-available/django /etc/nginx/sites-available/site2
ln -s /etc/nginx/sites-available/site2 /etc/nginx/sites-enabled/site2

Then edit its contents to match where you installed your new Django app:

upstream app_server {
    server 127.0.0.1:9500 fail_timeout=0;
}

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

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

    client_max_body_size 4G;
    server_name www.mysite2.com;

    keepalive_timeout 5;

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

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

    location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_pass http://app_server;
    }
}

Do the same for the Upstart script used to start gunicorn:

cp /etc/init/gunicorn.conf /etc/init/new_site.conf

And change the port and path for the project:

description "Gunicorn daemon for Django project"

start on (local-filesystems and net-device-up IFACE=eth0)
stop on runlevel [!12345]

# If the process quits unexpectedly trigger a respawn
respawn

setuid django
setgid django
chdir /home/django

exec gunicorn \
    --name=new_project \
    --pythonpath=new_project \
    --bind=0.0.0.0:9500 \
    --config /etc/gunicorn.d/gunicorn.py \
    django_project.wsgi:application

You should now be able to start your project with:

service nginx restart
service new_site start

Notice that the way you start the project is determined by the name you give the Upstart script.

Hope this points you in the right direction! Let us know how it goes.

  • This gives you nginx errors

    2014/10/10 06:59:33 [emerg] 17626#0: duplicate upstream "app_server" in /etc/nginx/sites-enabled/django:1
    2014/10/10 07:03:31 [emerg] 17714#0: a duplicate default server for 0.0.0.0:80 in /etc/nginx/sites-enabled/django:6
    
  • Anyone got this to work? Would mean a lot to me!

  • Any update?, this is important to me right now

  • for this line
    ln -s /etc/nginx/sites-available/site2 /etc/nginx/sites-available/site2
    Isn't it sites-enabled for the second argument?

  • @curiousprgrmr: You're right, it should be sites-enabled. I've corrected it. Thanks!

  • I setup everything without errors, but why everytime I enter in browser my newsite.com it opens oldsite.com?

  • everything works fine, I just messed path in my app folder.

  • I am having the same issue of

    nginx: [emerg] duplicate upstream "app_server" in /etc/nginx/sites-enabled/new_site:1
    nginx: configuration file /etc/nginx/nginx.conf test failed
    

    I changed the server to

    server 127.0.0.1:9500 fail_timeout=0;
    

    and also removed the "default_server" from

        listen 80 default_server;
        listen [::]:80 default_server ipv6only=on;
    

    Could anyone who resolved this issue help me?

    Thank you

  • For those, who still have
    [emerg]: duplicate upstream "app_server" in /etc/nginx/sites-enabled/site2:1
    problem.
    All you need to do is change upstream name to another, then your nginx configuration file for second domain should look like this (at least, that's how mine looks):

    upstream app_server2 {
        server 127.0.0.1:9500 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 www.mysite2.com mysite2.com;
    
        keepalive_timeout 5;
    
        # Your Django project's media files - amend as required
        location /media  {
            alias /home/django/new_project/new_app/media;
        }
    
        # your Django project's static files - amend as required
        location /static {
            alias /home/django/new_project/new_app/static; 
        }
    
        location / {
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_redirect off;
            proxy_pass http://app_server2;
        }
    }
    

    After this everything should work :)

  • Someone has figured out how to make this work I only see internal server error.

To fix the

a duplicate default server for 0.0.0.0:80

just remove the default_server in your second website, your need just one default_server

Hi After setting up the new site. When I try to restart nginx server. I get following error

  • Reloading nginx configuration nginx [failed]

Is there any thing else to modify ?

Have another answer? Share your knowledge.