Run Multiple Django Projects on One Server

How to run multiple Django projects on one Ubuntu server using Gunicorn and Nginx
July 25, 2024 by
Run Multiple Django Projects on One Server
Hamed Mohammadi
| No comments yet

In a previous post we went through the process of Deploying a Django Project on Ubuntu using Nginx, Gunicorn and PostgreSQL.

If you need to deploy more than one Django project on a single Ubuntu VPS server you can do it by using the procedures in that article with a little twist.

Consider you have deployed your first Django project using instructions on that post. For next Django project you only need create an additional Gunicorn socket and systemd service.

First create the second socket:

$ sudo nano /etc/systemd/system/gunicorn2.socket

 In that file type:

[Unit]
Description=gunicorn2 socket

[Socket]
ListenStream=/run/gunicorn2.sock

[Install]
WantedBy=sockets.target


Next we should create the second systemd service file:

$ sudo nano /etc/systemd/system/gunicorn2.service

 In that file type:

[Unit]
Description=gunicorn daemon
Requires=gunicorn2.socket
After=network.target

[Service]
User=mysite2
Group=www-data
WorkingDirectory=/opt/mysite2/mysite2
ExecStart=/opt/mysite2/mysite2/.venv/bin/gunicorn \
          --access-logfile - \
          --workers 3 \
          --bind unix:/run/gunicorn2.sock \
          mysite2.wsgi:application

[Install]
WantedBy=multi-user.target

 Now Start the second Gunicorn socket:

$ sudo systemctl start gunicorn2.socket
$ sudo systemctl enable gunicorn2.socket

Check the status of the process to find out whether it was able to start:

$ sudo systemctl status gunicorn2.socket

For nginx configuration of the second site after obtaining a certificate you can change your site configuration to:

server {
    listen 80;
    server_name www.mysite2.com mysite2.com;

    include snippets/letsencrypt.conf;
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl http2;
    server_name www.mysite2.com;

    ssl_certificate /etc/letsencrypt/live/mysite2.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/mysite2.com/privkey.pem;
    ssl_trusted_certificate /etc/letsencrypt/live/mysite2.com/chain.pem;
    include snippets/ssl.conf;
    include snippets/letsencrypt.conf;

    return 301 https://mysite2.com$request_uri;
}

server {
    listen 443 ssl http2;
    server_name mysite2.com;

    ssl_certificate /etc/letsencrypt/live/mysite2.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/mysite2.com/privkey.pem;
    ssl_trusted_certificate /etc/letsencrypt/live/mysite2.com/chain.pem;
    include snippets/ssl.conf;
    include snippets/letsencrypt.conf;

    # . . . other code

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /opt/mysite2;
    }

    location /media/ {
            root /opt/mysite2;
    }

    location / {
        include proxy_params;

        proxy_pass http://unix:/run/gunicorn2.sock; # use the second socket
    }
}

Now check if Nginx site configuration is Okay:

$ sudo nginx -t


And then restart the Nginx service:

$ sudo systemctl restart nginx

You can repeat this process for as many Django projects since your server performance allows.


Run Multiple Django Projects on One Server
Hamed Mohammadi July 25, 2024
Share this post
Tags
Archive

Please visit our blog at:

https://zehabsd.com/blog

A platform for Flash Stories:

https://readflashy.com

A platform for Persian Literature Lovers:

https://sarayesokhan.com

Sign in to leave a comment