Wine Data App Part 3

Deployment

Our app can be run locally if a user decides to clone our repository and follows the instructions in the README file. We have also hosted the app in our very own AWS server for convenience and, of course, for the sake of learning.

Following the general set up of our server from the previous AWS post, we still need to set up the specific packages for this specific app.

Firstly, we need to download and install a new copy of anaconda in our server to organize our packages. It is recommended to install miniconda as it is lighter on disk space.

1
2
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
bash Miniconda3-latest-Linux-x86_64.sh

Once installed, we can either manually install the necessary packages to run our app or we can install from an exported list of packages for our particular environment. In a previous Anaconda post, I described how to export a conda environment as a .yml file and how to install the environment from that .yml file (the .yml file can be pushed to GitLab and pulled in the server for ease of transferring the appropriate environment to the server).

Which ever method we choose to install packages, we must still clone our app repository.

Once the repository is cloned and the necessary packages are installed, we can activate our environment and run the app locally to see if everything works.

1
2
cd <repo_folder>
python3 <app>.py

This should start the Flask app on the specified port of our server (we cannot access this localhost port to see if the app is running just yet).

Now for the complicated part: we need a way for our server to automatically serve its localhost ports when an HTTP request is made to our server. To accomplish this task, we will be using Nginx as a load balancer.

1
sudo apt install nginx

Next we will navigate to the Nginx config files and remove the default config (since this is in the root directory of the server, we will need to use sudo):

1
2
cd /etc/nginx/sites-enabled
sudo rm default

Now we must create a new config file using Vim:

1
sudo vim <name>

Press i to enter insert mode in Vim and add the following:

1
2
3
4
5
6
7
8
server {
location / {
proxy_pass http://localhost:8000;
}
location /templates {
alias /home/<repo>/templates/;
}
}

Press esc to exit insert mode and type :qw to quit Vim and write the file.

Finally, restart Nginx

1
sudo /etc/init.d/nginx restart

Now we can start up a tmux session and start our app in there to keep it persistent.

1
2
3
tmux
cd <repo_folder>
python3 <app>.py

We should now be able to detach from the tmux session by pressing ctrl-b d then typing exit in the server terminal to exit the server.

Navigate to the server’s Public DNS (IPv4) and we should see the app running.

Congratulations! We just deployed our first app on AWS for everyone to see and interact with.