Setup Flask on Webhost (Phusion Passenger)

Purpose: Set up a Flask project on the webhost, which uses Phusion Passenger.

Create a New Python Project

Log into your subdomain and press the "Setup Python App" button, which should look like this:

The screen should then look like this, more or less:

Explanation by example, assuming your username is student11 and your subdomain is student11.cs3660.xyz:

Python Version: The system defaults to a low version. Pick the highest unless you have an opinion which version.

Application root: sets the project document root to <Your User Root>/flasktest.

Application URL: sets the project URL to student11.cs3660.xyz/flasktest.

Application startup file: sets the project file to app.py.

Application Entry point: sets the project to import the object named app from app.py.

Passenger log file: sets the log location at <Your User Root>/flasktest/output.log, since your user root would be /home/student11 if you are student11.

Environment Variables: (not used at this point) allows you to add custom environment variables that will be passed to your Flask app.

When these values are submitted, Phusion Passenger generates a Python file called passenger_wsgi.py. Phusion Passenger operates as a WSGI server, similar to wsgiref.handlers.CGIHandler, but Passenger is multi-threaded and more suitable for production.

Upload Content

Use the same content as in Exercise 2.2 (Sample Flask Project)

Install Flask using requirements.txt

The Problem: The Python on our webhost does not come with Flask pre-installed. You may find that the Terminal widget on your cpanel interface blocks you from using pip directly to install it.

The Solution:: The "Setup Python App" widget creates a virtual environment (venv) behind the scenes, and the same widget allows you to run pip on a requirements.txt file within your project's virtual environment. Exercise 2.2 explains how venv and requirements.txt work. Use it to install Flask. Run "pip freeze" from your local machine and copy the lines that are related to Flask and Jinja into a requirements.txt file on your webhost. Then go to the "Setup Python App" widget and open up your project and press the pencil button to "edit" its content. Scroll down far enough, and you will see a menu that looks like this:

Enter the name of your requirements.txt file and press the "Run Pip Install" button. The dependencies in your requirements.txt will be installed.

Alternative: Install Flask using Virtual Environment

Alternatively, on your Setup Python App page (Edit mode) there is a linux command given to enter the virtual environment for your project, which will copy to the clipboard when clicked. If you copy-paste that link to the Terminal, you can then simply run "pip3 install flask" from within the Virtual Environment, and the system will allow you to do so.

Check passenger_wsgi.py

The file passenger_wsgi.py operates similarly function as your index.cgi in Exercise 2.2, except that it will be imported by Phusion Passenger which then does the CGI handling in its multi-threaded way. The only important thing is that your app object is properly imported renamed as "application" in passenger_wsgi.py. Check that this is the case.