In this post, I will be providing an overview on how I set up an application server to host my Python data visualization using popular frameworks like Bokeh or Dash. The server is built on Ubuntu and utilizes Nginx as a reverse proxy and Certbot for securing the connection with HTTPS.

Please note that this guide is not exhaustive or highly detailed, as it is intended for internal access and use only. The firewall configuration is also not covered in this guide as it is usually handled in the Azure environment that I frequently use.

It is important to understand that this guide serves as a starting point and there may be additional steps required depending on your specific setup. Nonetheless, I hope this will give you a general idea on how to set up an application server for your data visualization.

Get ready to dive into the world of application servers and start serving your data to the world!

Table of Contents

autoauto- [Update to pip3](#update-to-pip3)auto- [Install Git and Clone a Repository](#install-git-and-clone-a-repository)auto- [Install Python libraries and Run App.py](#install-python-libraries-and-run-apppy)auto- [Install Nginx and Configure](#install-nginx-and-configure)auto- [Restart Nginx](#restart-nginx)auto- [Install Cerbot](#install-cerbot)auto- [Testing](#testing)autoauto

Update to pip3

Upgrading to pip3 ensures that the correct version of pip is installed for python3, which is the recommended version for current python development.

$ sudo add-apt-repository universe
$ sudo apt update
$ sudo apt upgrade
$ sudo apt install python3-pip
$ pip3 --version

Install Git and Clone a Repository

Install Git on your server and use it to clone the repository of your Python application

# Install git
$ sudo apt install git

# Verify git is installed
$ git --version
$ git config --global user.name "username"
$ git config --global user.email "your@email.com"
$ git config --list

# For initial clone credentials are required
$ git clone https://<githubUserNane>:<password>@github.com/<git_path_url>.git

# Cache the credentials
$ git config --global credential.helper cache. 
$ cd <repo_name>

On your subsequent pull use this instead

$ git pull <githubUserNane>:<password>@github.com/git_path_url>.git

Install Python libraries and Run App.py

Once you have cloned or downloaded the code for your Python application, the next step is to install the necessary libraries. Keep in mind that the main function of Flask is typically saved as app.py, but make sure to adjust this if you have saved your function under a different name.

$ pip3 install -r requirements.txt  # Optional
$ python3 app.py                    # Test the application

Here’s a sample app.py using dash.

import dash
import dash_core_components as dcc
import dash_html_components as html

app = dash.Dash()

app.layout = html.Div(children=[
    html.H1(children='Dash Tutorials'),
        dcc.Graph(
        id='example',
        figure={
            'data': [
                {'x': [1, 2, 3, 4, 5], 'y': [9, 6, 2, 1, 5], 'type': 'line', 'name': 'Boats'},
                {'x': [1, 2, 3, 4, 5], 'y': [8, 7, 2, 7, 3], 'type': 'bar', 'name': 'Cars'},
            ],
            'layout': {
                'title': 'Basic Dash Example'
            }
        }
    )
])

if __name__ == '__main__':
    app.run_server(debug=True)

Install Nginx and Configure

In this step, we will be installing Nginx, a popular open source web server and reverse proxy, and setting it up to act as a reverse proxy for our Python application. This will allow us to serve our application over the network using Nginx as an intermediary between the any user and our application server.

The configuration process involves mapping requests to the appropriate location on our application server, and ensuring that data is securely transmitted. By using Nginx as a reverse proxy, we can provide an additional layer of security, performance optimization, and scalability to our application setup.

$ sudo apt-get install nginx
$ sudo vi /etc/nginx/sites-enabled/default # see below

This notice that this configutaion file below is only for HTTP we will configure HTTPS later on.

# filename : /etc/nginx/sites-enabled/default
server {
        listen 80 default_server;
        listen [::]:80 default_server;

        root /var/www/html;

        index index.html index.htm index.nginx-debian.html;

            server_name _;

            location / {
                proxy_set_header X-Forwarded-For 
                proxy_set_header Host $http_host;
                proxy_set_header X-NginX-Proxy true;
                proxy_set_header X-Script-Name /godash;
                proxy_add_x_forwarded_for;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "upgrade";
                proxy_max_temp_file_size 0;
                rewrite /dashboard/(.*) /dashboard/$1  break;
        }

}

Restart Nginx

Before (re)starting the Nginx service, it’s important to run a configuration test to ensure that the configuration file is error-free. After the test has passed, you can safely restart Nginx to apply the new settings.

$ sudo nginx  -t                # test config file
$ sudo nginx -s  reload         # Restart nginx

# or Restart usinng systemctl
$ sudo systemctl stop nginx     # stop
$ sudo systemctl start nginx    # start
$ sudo systemctl reload nginx   # restart

Install Cerbot

For enhanced security, we will be utilizing Certbot, a free and open source solution that allows us to easily obtain and install SSL/TLS certificates from the trusted certificate authority, Let’s Encrypt. This will secure the communication between the server and clients, ensuring that all data transmitted is protected and private.

$ sudo snap install core; sudo snap refresh core   
$ sudo snap install --classic certbot
$ sudo ln -s /snap/bin/certbot /usr/bin/certbot
$ sudo certbot --nginx          # follow installation prompts
$ sudo certbot renew --dry-run  # dry-run for auto renew
  • dry-run is to ensure that the auto renew can be executed without errors.
  • Notice that your /etc/nginx/sites-enabled/default will be updated.

Testing

If everything was installed and configured correctly, your python application should now be accessible at https://<domain_or_localhost>, ready to be viewed and explored by users!