How to deploy multiple Django apps (as subdomains) using nginx and Gunicorn

I have a new 18.04 Droplet running a Django website on nginx. I’d like to use this droplet to serve several websites at subdomains. I have successfully:

  1. Followed this excellent tutorial to set up my first website:

  2. Configured nginx and DNS to serve (static) websites & subdomains on this server

  3. Set up a second Django app that I can run on port 8000, using Gunicorn as a console process

So where I’m hung up is what I add to the systemd configuration? I tried copy/pasting the [Service] block for my new site but when I try to restart gunicorn I get this error:

systemd[1]: gunicorn.service: Service has more than one ExecStart= setting, which is only allowed for Type=one

Should I create a second *.service file in /etc/systemd/system/? Is there some way to put multiple Django apps into one ExecStart directive? Each app runs in its own virtual environment.

Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Accepted Answer

I believe you need to create multiple socket and service files.

  1. I created a separate *.service and *.socket file for each web app
  2. I changed the WorkingDirectory and ExecStart values in the *.service files appropriately

Then I was able to host multiple django projects, each one with a different domain.

From my surface understanding, the process goes something like this. Nginx listening on port 80 receives an HTTP request. This request includes a Host header, which includes Nginx then looks through it’s sites-available configs for a site that accepts If it finds one it will look at the appropriate location section in the config file. In my case this includes

proxy_pass http://unix:/run/[MySockFileName].sock;

Some kind of information is then pushed into the unix socket file, named [MySockFileName].sock, inside the /run directory. SystemD notices this and starts up gunicorn if it’s not running already. Gunicorn consumes the information in the socket and after that magically runs python and sends you a rendered web page.

If you find best practices on this for production please let me know.

Thank you so much BenSa! This got me on the right path. I’m not sure if this is “best practices” but it works.

It helps to walk through DO’s tutorial (linked above) but substitute a handle for the subdomain for gunicorn. So for a subdomain

File /etc/systemd/system/foobar.service:

Description=gunicorn daemon

ExecStart=/home/myUserName/foobar/.env/bin/gunicorn \
    --access-logfile - \
    --workers 3 \
    --bind unix:/run/foobar.sock \


File /etc/systemd/system/foobar.socket:

Description=gunicorn socket



File /etc/nginx/sites-available/foobar:

server {

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /home/myUserName/foobar;
    location / {
        include proxy_params;
        proxy_pass http://unix:/run/foobar.sock;

Make sure to symlink the nginx site file:

$ ln -s /etc/nginx/sites-available/foobar /etc/nginx/sites-enabled/foobar

And enable the socket for the subdomain:

$ systemctl start foobar.socket
$ systemctl enable foobar.socket

Each socket is its own service. So if I need to restart the foobar site (for example after updating

$ systemctl restart foobar

Muchas gracias a todos por el aporte, me ha servido mucho!!

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel