Skip to main content

install flask websites into nginx

Project description

footprint 👣

I use this to generate config files for my flask apps. Currently systemd and nginx. It only depends on Flask.

It is intended to be installed into the same virtual environment that the flask app inhabits so it can introspect the app. It has no dependencies other than Flask.

export FLASK_APP=your_package.wsgi
footprint config nginx www.example.com > example.conf
footprint config nginx-install example.conf
# install in ~/.config/systemd/user
export FLASK_APP=your_package.wsgi
footprint config systemd --user  > example.service
footprint config systemd-install --user example.service

will install nginx and systemd files that will statically serve you 'static' assets and run the Flask app with gunicorn.

Mostly I've found that confectioning these files by hand are highly error prone. These commands will at least get the absolute pathnames correct :)

footprint will install a Quart (ASGI) service using the --asgi to flag. In this case you need both the quart and uvicorn-worker packages installed also.

Install with:

python -m pip install flask-nginx
python -m pip install -U git+https://github.com/arabidopsis/footprint.git
# or
# git clone https://github.com/arabidopsis/footprint.git
# cd footprint
# python -m pip install [--editable] .

or add to your pyproject.toml file

footprint = { git = "https://github.com/arabidopsis/footprint.git", branch="main" }

Once installed you can upgrade with:

footprint update
# or
uv pip install -U $(footprint repo)

If footprint finds a pyproject.toml file in the current directory if will try to load [tool.footprint] values into its global configuration object.

nginx, systemd and all that

Note that these configuration generating functions are not infallible. Please examine the generated configure files carefully! They are mainly useful for getting the directory names correct etc. So if you move your repo then you will have to regenerate and reinstall the files.

Test an nginx config with e.g.:

website=~/Sites/websites/ppr
export FLASK_APP=ppr.wsgi
footprint config nginx --app-dir=$website example.org | footprint config nginx-run --app-dir=$website -

This will run nginx at the terminal listening on port 2048 and run the backend website.

To install a website:

footprint config nginx --app-dir=$website example.org -o website.conf
footprint config systemd [--user] --app-dir=$website -o website.service
# nginx requires sudo (default) or su
footprint config nginx-install website.conf
# if you can install into ~/.config/systemd/user
footprint config systemd-install [--user] website.service

You can test this locally by editing /etc/hosts and adding a line:

127.0.0.1 example.org

to the file.

REMEMBER: Unix file permissions mean that you should edit /etc/nginx/nginx.conf and change user www-data; to user {you}; Or (recursively) change the owner on all the repo directories to www-data.

If you install as "user" (i.e. footprint config systemd --user ...) then to ensure that the user systemd starts at boot time use: sudo loginctl enable-linger <user>

See here:

But what’s the real reason for having user services? To answer that, we have to realize when the enabled service starts and stops. If we enable a user service, it starts on user login, and runs as long as there is a session open for that user. Once the last session dies, the service stops.


See digitalocean.com here for a tutorial about serving flask from nginx.

Uninstall with footprint config nginx-uninstall website.conf and footprint config systemd-uninstall [--user] website.service

.flaskenv

If a .flaskenv is found in the repo directory then nginx and systemd will read paramters from that file. The keywords should be uppercase version of the known parameters. Unknown parameters will be ignored.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

flask_nginx-0.8.17.tar.gz (34.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

flask_nginx-0.8.17-py3-none-any.whl (49.6 kB view details)

Uploaded Python 3

File details

Details for the file flask_nginx-0.8.17.tar.gz.

File metadata

  • Download URL: flask_nginx-0.8.17.tar.gz
  • Upload date:
  • Size: 34.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.5

File hashes

Hashes for flask_nginx-0.8.17.tar.gz
Algorithm Hash digest
SHA256 844e09e7a60c78efd70b39aeda1649694eb42f24a09d2a84fd9c715c78a9822a
MD5 fc4326fd59aece942e6eb0cdf59e8f1a
BLAKE2b-256 acaeed0d319d441bbc569a2d39cc0ad931af564054e588568a47ac9361c27428

See more details on using hashes here.

File details

Details for the file flask_nginx-0.8.17-py3-none-any.whl.

File metadata

File hashes

Hashes for flask_nginx-0.8.17-py3-none-any.whl
Algorithm Hash digest
SHA256 443e691513fe44a836e4be3452abbf5f53d005eee92eb8604867060b79ba59f4
MD5 4ea573ea6b448c6f80dc68125ce087f8
BLAKE2b-256 212d241630bc0b1ea9d51633b05dc040c7552a6e3e1928875155015678271b9b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page