I hope this short article can be useful to you, I already covered this topic in details in my blog post, feel free to read it.

Now, I’ll assume that the only problem for you is serving multiple Django projects with multiple domain names in one server and you already know the basics of setting up a VPS server for a Django project.

We can solve this issue in 3 steps, you repeat each step for each Django project:

1 — Create a socket file for each Django proejct

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

The content:

[Unit]

Description=gunicorn socket

[Socket]

ListenStream=/run/project1.sock

[Install]

WantedBy=sockets.target

Start and enable it

$ sudo systemctl start project1.socket
$ sudo systemctl enable project1.socket

→ Do the same thing for the other projects

2 — Create a service file for each Django proejct

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

The content:

[Unit]

Description=gunicorn daemon

Requires=project1.socket

After=network.target

[Service]

User=user

Group=www-data

WorkingDirectory=/path/projects1

ExecStart=/path/venv/bin/gunicorn --workers 3 --bind unix:/run/project.sock config.wsgi:application


[Install]

WantedBy=multi-user.target

Start and enable it

$ sudo systemctl start project1.service
$ sudo systemctl enable project1.service

3— Configure Nginx to Proxy Pass to Gunicorn

$ sudo nano /etc/nginx/sites-available/project1

The content:

server {

listen 80;

server_name doamin.com;


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


location / {

include proxy_params;

proxy_pass http://unix:/run/project1.sock;

}

}

Enable it

$ sudo ln -s /etc/nginx/sites-available/project1 /etc/nginx/sites-enabled

Restart Nginx and stop buying a server for each project.

--

--

Selmi Abderrahim

I’m a computer science student. I do freelancing in my free time. I am a big fan of affiliate marketing, it helped me a lot to monetize my daily stuff.