Skip links

How to Deploy a Flask App on a VPS using SSH

View Categories

How to Deploy a Flask App on a VPS using SSH

1 min read

This guide will help us deploy a flask application on a VPS via SSH. There are several ways to deploy python based apps but in this case, we will use Apache and Phusion Passenger – the same software used in shared hosting.

Requirements #

  • root access to the server
  • Ubuntu 20.04 installed
  • Minimum 512MB RAM, 10GB space
  • a domain/subdomain pointed to your VPS IP address

The steps involved are as follows:

1.Install needed software (Python3, Apache, Passenger and Git)

2.Upload you website

3.Setup server configuration

Lets get started

Install needed software #

The software needed are python3, Apache, Passenger and Git

Uninstall Python 2

$ sudo apt remove python2
$ sudo apt remove python-is-python2
$ sudo apt autoremove --purge

Install Python 3

$ sudo apt-get update
$ sudo apt-get install -y python3 python3-pip

Install Apache

$ sudo apt-get install apache2

Install Passenger and needed Apache modules

$ sudo apt-get install -y dirmngr gnupg
$ sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 561F9B9CAC40B2F7
$ sudo apt-get install -y apt-transport-https ca-certificates
$ sudo apt-get install -y libapache2-mod-passenger
$ sudo a2enmod passenger
$ systemctl restart apache2

Install Git

$ sudo apt-get install -y git

Upload your website #

We will use git to upload our website. To do this, you need to have your website already available in a repo such as Github or Gitlab.

Create a user for your app. We will call our user appuser

$ useradd appuser
$ passwd appuser

Create your application directory

$ sudo mkdir -p  /var/www/awesomeapp
$ sudo chown appuser:  /var/www/awesomeapp

Clone your app

$ cd /var/www/awesomeapp && sudo -u appuser -H git clone https://gitlab.com/username/awesomeapp.git

Note: If you do not have an app in a repo such as Github or Gitlab already, please manually upload your app files to/var/www/awesomeapp

Setup server configuration #

The next thing is to complete the set up of our app. This means we install the application requirements and configure Apache

Install app requirements

Your app should have a requirements.txt file. Run the command below to install required files

$ pip3 install -r requirements.txt

Create an Apache VHOST file using the following command. We’ll assume your domain is called truehosttestdomain.com

$ sudo vim /etc/apache2/sites-enabled/awesomeapp.conf

Copy the content below

<VirtualHost *:80>
    ServerAdmin [email protected]
    ServerName truehosttestdomain.com
    ServerAlias truehosttestdomain.com
 
    # Tell Apache and Passenger where your app's code directory is
    DocumentRoot /var/www/awesomeapp/code/
    PassengerAppRoot /var/www/awesomeapp/code
 
    # Tell Passenger that your app is a Python app
    PassengerAppType wsgi
    PassengerStartupFile passenger_wsgi.py
 
    ErrorLog /var/www/truehosttestdomain.com/logs/error.log
    CustomLog /var/www/truehosttestdomain.com/logs/access.log combined
 
    Alias /static/ /var/www/awesomeapp/static
    <Directory /var/www/awesomeapp/static>
        Order allow,deny
        Allow from all
    </Directory>
 
</VirtualHost>

Restart apache and check your domain online!

$ systemctl restart apache2