How to Deploy a flask app on Ubuntu 20.04 using Passenger Standalone

On a previous article, we looked at deploying a flask application on Ubuntu 20.04 using Apache and Passenger

It is also possible to deploy an application on Passenger alone, without integrating it with another web server such as Apache or Nginx.

Prerequisites

  • A domain name (or subdomain) we’ll use to access our deployed application
  • A server installed with Ubuntu 20.04
  • Python 3.x. Our sample app runs on python 3.
  • Your application should be pushed to Github or Gitlab. I use Gitlab here

Procedure

1. Acquire a server.

As I mentioned earlier, I use Truehost Cloud for my servers.

2. Uninstall Python 2 (If it comes by default with your installation) and Install Python3

To uninstall Python 2, run the commands below

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

To install python3, run the command below

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

You can then run the command below to set aliases for python and python3 commands

sudo apt-get install python-is-python3

3. Install Passenger

Install Passenger’s GPT keys

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

Add passengers repository

sudo sh -c 'echo deb https://oss-binaries.phusionpassenger.com/apt/passenger bionic main > /etc/apt/sources.list.d/passenger.list'
sudo apt-get update

Install Passenger

sudo apt-get install -y passenger

4. Create a user for your app.

We’ll create a user with the name portfolio, as our app is called portfolio

useradd portfolio
passwd portfolio

5.Install Git

We’ll deploy using git

sudo apt-get install -y git

6. Upload your files

I’ll navigate to my application root directory and pull my app files from Gitlab. My application root directory is This is /var/www/portfolio

sudo mkdir -p /var/www/portfolio
sudo chown portfolio: /var/www/portfolio

To pull your code, run the following

cd /var/www/portfolio
sudo -u portfolio -H git clone https://gitlab.com/wwmwabini/portfolio.git code

Replace the git URL with your application URL

7. Install app dependencies

App dependencies are normally saved in a file called requirements.txt. To get this file, access terminal on your web development environment and run the command below

pip freeze >> requirements.txt

You can then upload the requirements.txt file to your server document root and run the command below to install the dependencies

pip3 install -r requirements.txt

8. Create a Passenger config file

Navigate to the document root of your app and create a Passengerfile.json file

cd /var/www/portfolio/code
sudo vim Passengerfile.json

Insert the code below

{
  // Tell Passenger that this is a Python app.
  // Replace "passenger_wsgi.py" with your app's WSGI entry point file.
  "app_type": "wsgi",
  "startup_file": "passenger_wsgi.py",
  // Run the app in a production environment. The default value is "development".
  "environment": "production",
  // Run Passenger on port 80, the standard HTTP port.
  "port": 80,
  // Tell Passenger to daemonize into the background.
  "daemonize": true,
  // Tell Passenger to run the app as the given user. Only has effect
  // if Passenger was started with root privileges.
  "user": "portfolio"
}

Replace “portfolio” with your application user you created in step 4

9. Fix permissions

sudo chown portfolio: Passengerfile.json

10. Start Passenger

sudo passenger start

Walaah! Your application should be up and running

Troubleshooting.

If you bump into any errors, check out our previous article on how to debug or contact me via the contact page for assistance.

Leave a Reply

Your email address will not be published. Required fields are marked *