Host a Dashboard using Python, Dash, and Linux in your own Linux server

Hosting a Dash app on Ubuntu 20.04 using uWSGI and Nginx

Carlos Piñero
6 min readNov 21, 2020

Why Host a Dashboard?

One of the aspects I enjoy most about data analysis is sharing insights I find from the data. Creating a dashboard to share insights using Dash is easy if you know a little Python, but hosting an app can get a little tricky. If you’re trying to break into a career in data, understanding how to host apps and dashboards gives you the ability to easily share a portfolio of projects to potential employers or clients.

I will cover to host a Dash app on a Linux server! Dash is an open-source framework for Python written on top of Flask, Plotly.js, and React.js. There are several ways to host a Dash app, and I am going to cover a method using the Ubuntu 20.04 operating system, Nginx (pronounced engine x), and uWSGI to serve the dashboard to external users.

Pre-installation

First of all, you must make all the previous configuration that I explain in my Create your own linux-server article.

Get down to it

The first thing you need if you want to show the world your work is to buy a domain.

I bought it at Godaddy, but you can buy it on the site that has the best offer at that time.

In my case I bought www.DataTown.es

Then you have to point your domain to your fixed DNS that you previously configured in Duck DNS.

We only have to modify the red box with your Duck DNS. You will have your domain pointed to your fixed dns.

Also in my case i will redirect the domain in the configuration of Godaddy so that both www.datatown.es and datatown.es work correctly.

This DNS configuration takes a few minutes to update, so don’t go crazy trying things if they don’t work for you until a few minutes have passed.

Dashboard App

Since this article is focused on hosting, I won’t cover all the code used in the DataTown Dashboard. My steps will apply to any dashboard if you want to use your own!

The DataTown Dashboard is available on my GitHub and step-by-step instructions for creating.

Check your app works

I strongly recommend creating a conda environment (server_env) for the app.

Activate de conda environment and clone the GitHub repo on your server and run the command in a terminal, in my case:

python app.py

If it errors, a library might have gotten missed. If successful, it should look something like this in the command line:

Install and prepare uWSGI

To serve the dashboard to external users requires some kind of web server back-end. Nginx is a lightning-fast, open-source web server that is easy to use and perfect for this use-case. Along with Nginx, I am using uWSGI to set the dashboard to run as a service that runs in the background when the Ubuntu 20.4 operating system boots up.

Install uWSGI in your environment (server_env).

conda install -c conda-force uwsgi

Create wsgi.py

The server needs instructions for interaction with the application. That is what the wsgi.py file is does! Create the wsgi.py file in the same directory as the app.py:

nano wsgi.py

Paste this bit of code into the file. In order to work correctly, it must use application.run():

from app import server as application
if __name__ == '__main__':
application.run()

Save the file and use this command to test that the application can be served through uwsgi protocol:

uwsgi --socket 127.0.0.1:8050 --protocol=http -w wsgi

Success will look like this in the console:

Create index.ini

This is the uWSGI configuration file that describes how to pass requests from the web. Nginx can use the uwsgi protocol natively, and it is faster and more secure than using HTTP.

Create the file in the root directory of the repo(ir/DataTown) using the nano editor:

nano index.ini

Paste this into the file:

[uwsgi]
module = wsgi
master = true
processes = 4
socket = index.sock
chmod-socket = 660
vacuum = true
die-on-term = true

The [uwsgi] header tells uWSGI to apply the settings in the file.
Specify the module by referring to the wsgi.py file.
This is the master with 4 worker processes to handle requests.
The vacuum = true option helps clean up the socket when the process stops.
The die-on-term = true option makes sure the system’s components play nicely with each other.

Create Index.service Systemd Unit File

This file is needed to allow the Ubuntu’s init system to automatically start uWSGI and run the dashboard when the server boots.

Use the following path to open a new file using nano named index.service.

nano /etc/systemd/system/index.service

Paste the following code that describes how the service should run. This file points to the directories and virtual environment if one is being used.

[Unit]
Description=uWSGI instance to serve index
After=network.target
[Service]
User=carpiero
Group=www-data
WorkingDirectory=/home/carpiero/ir/DataTown
ExecStart=/home/carpiero/miniconda3/envs/server_env/bin/uwsgi --force-cwd /home/carpiero/ir/DataTown --ini index.ini
[Install]
WantedBy = multi-user.target

The Unit section describes the metadata and dependencies.
The Service section describes the user and group under which the process run. The Nginx group owns it so it can communicate with uWSGI.
The Install section tells Systemd to boot the files when the multi-user system starts.

Now check that the service previously created works perfectly.

sudo systemctl restart index.service
sudo systemctl status index.service

Configure Nginx

Now that the service is configured and will run when Ubuntu boots up, Nginx must be set to pass requests that come from the web to the socket over the uwsgi protocol.

Instead of modifying the default configuration file directly, we will create a new one.

sudo nano /etc/nginx/sites-available/datatown

Paste it into the next configuration block, similar to the default, but updated for our new directory and domain name:

server { server_name www.datatown.es;if ($host = www.datatown.es) { 
return 301 https://datatown.es$request_uri;
}
location / {
include uwsgi_params;
uwsgi_pass unix:/home/carpiero/ir/DataTown/index.sock;
}
}

That is the complete setup. Go to your domain of your browser to load the dashboard!

That’s it

Thanks for reading this post, I hope this information will help you to advance your career or learn something new.

PS: Thanks to Pedro Muñoz for his help.

Check out my others Mediums:

--

--

Carlos Piñero

💻 Data Analyst | 📊 MBA | 🤖 Machine Learning | 📈 Controller