Deploy multiple Django applications on same server using sub-path

Anubhaw Gupta
Clean Slate Technologies
3 min readApr 20, 2019

Django is a free and open source web application framework written in Python.

As the title suggests, we are going to host multiple django apps on the same server, by creating multiple subpaths. <There is always a need to host multiple applications on a single server instance which is not a straight forward task when working with Django project. I recently faced this issue and here’s the best possible solution I could figure out.>I am not getting into the details of how to setup a Django project, assuming that you have a working project with you. What we will talk about is that how to deploy multiple Django project on the server using sub-path, like www.yourwebsite.com/fdab/fdab_home & www.yourwebsite.com/pdab/pdab_home

Am using Gunicorn + Nginx to deploy my applications and Conda for managing the environments.

Firstly setup the Conda environment:

$ conda create — name fdab$ conda create — name pdab

Here fdab and pdab are the name of the environments. Activate it using the command “source activate fdab” and then install the required modules and libraries into it, for example

$ conda install django

Now let’s setup the Gunicorn.

We will create two different systemd services and socket files for both our applications.

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

The above socket file will have the following content:

[Unit]Description=gunicorn socket for fdab[Socket]ListenStream=/run/gunicorn_fdab.sock[Install]WantedBy=sockets.target

Now create the service file and add the content.

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

— -

[Unit]Description=gunicorn daemon for fdabRequires= gunicorn_fdab.socketAfter=network.target[Service]User=rootGroup=rootWorkingDirectory=/opt/fdabExecStart=/opt/anaconda/envs/fdab/bin/gunicorn \ — access-logfile — \ — workers 3 \ — bind unix:/run/gunicorn_fdab.sock \fdab.wsgi:application[Install]WantedBy=multi-user.target

In the above file, change the working directory to the appropriate location. Also change the ExecStart location in the above service file to where you created your conda environment. I have conda installed at /opt/anaconda , so change that to the location where you installed conda ( if you are not using anaconda, you can give the path of your environment). Lastly, for WSGI in above file , change “fdab” to your project name.

Next step is to start the socket file by using the command

$ sudo systemctl start gunicorn_fdab.socket
$ sudo systemctl enable gunicorn_fdab.socket

Repeat the process for second set “pdab” of gunicorn socket and service files.

sudo nano /etc/systemd/system/gunicorn_pdab.socket

The file will have the following content.

[Unit]Description=gunicorn socket for pdab[Socket]ListenStream=/run/gunicorn_pdab.sock[Install]WantedBy=sockets.target

Second service file for pdab.

sudo nano /etc/systemd/system/gunicorn_pdab.service

Content will be same as first one but with location and name changes.

[Unit]Description=gunicorn daemon for pdabRequires= gunicorn_pdab.socketAfter=network.target[Service]User=rootGroup=rootWorkingDirectory=/opt/pdabExecStart=/opt/anaconda/envs/pdab/bin/gunicorn \ — access-logfile — \ — workers 3 \ — bind unix:/run/gunicorn_pdab.sock \pdab.wsgi:application[Install]WantedBy=multi-user.target

Let’s come to configration part of Nginx and Django Project.

Assuming that Nginx is already installed on your system. So let’s create a conf file for our applications and put the below content.

$ sudo nano /etc/nginx/sites-available/dashboardsserver {listen 80;server_name 111.11.11.111;location = /favicon.ico { access_log off; log_not_found off; }location /fdab/static/ {     alias /opt/fdab/fdab/static;}location /fdab {    include proxy_params;    rewrite /fdab(.*) /fdab$1 break;    proxy_pass http://unix:/run/gunicorn_fdab.sock;}location /pdab/static/ {    alias /opt/pdab/pdab/static/;}location /pdab {    include proxy_params;    rewrite /pdab(.*) /pdab$1 break;    proxy_pass http://unix:/run/gunicorn_pdab.sock; }}

Test your Nginx configuration for syntax errors by typing:

sudo nginx -t

If no errors are reported, go ahead and restart Nginx by typing:

sudo systemctl restart nginx

Now it’s time to change the Django project’s setting.py

Change the static url and logout url(if you have one) so that it matches with the Nginx conf file :

STATIC_URL = ‘/fdab/static/’LOGOUT_REDIRECT_URL = ‘/fdab/fdab_home/login’

Voila, you have your apps up and running at the subpath confgiured.

Let me know in comments if you face any trouble or have a better working solution with you. Always remember, sharing is caring.

--

--