Skip to content

ExecuteFlaskAppOnLinux

Oren Mishali edited this page Dec 5, 2023 · 6 revisions

Install and Execute a Flask App on Linux

Assumptions:

  • You develop on Windows but deploy on Linux.
  • You have a Flask project in GitHub.
  • You project has a Python Virtual Environment and a requirements.text file for the dependencies.
  • You have python 3.6+ installed (we had to upgrade our Linux 16.04 to python 3.7 as e.g. described here, or here for python 3.8 (note: after step 2 run sudo apt update).
  • You have sudo permissions on the machine.
  • For DMZ machines open ports are 80 and 443. Set your Flask app to run on 443.

Now follow these steps (for python3.7 that we use):

  • Clone the project and change to project directory.
  • Create a Virtual Python Environment (You may need to sudo apt-get install python3.7-venv)
python3.7 -m venv env

(This will create the env/ directory.)

  • Activate the environment:
screen // from this point we work in a dedicated screen for a long-running process.
source env/bin/activate

(Windows: source env/Scripts/activate.bat // use Git for Windows SHELL).

  • Install dependencies:
pip install -r requirements.txt
  • Run the server:
python app.py // if PermissionDenied see notes

Notes:

  • For the machine to be accesses from outside, use app.run(host='0.0.0.0', port=port).
  • For ports such as 443/80 you need sudo, use sudo env/bin/python app.py (and not sudo python app.py since sudo does not recognize environment vars).
  • A better and more secure option is to run on port 80 as a regular user:
sudo touch /etc/authbind/byport/80
sudo chmod 777 /etc/authbind/byport/80
authbind --deep python app.py

(Taken from this link).

  • The server is run in development mode, deployment mode is not covered here).

Clone this wiki locally