Skip to content

Deploy with systemd

Berg Paulo edited this page Apr 9, 2021 · 3 revisions

Install OS dependencies

Obs: Assuming it was installed Raspberry Pi OS (32-bit) with Desktop version: Maybe with other version, be necessary install another dependencies.

sudo apt install python3-venv


Creating virtualenv and running project

  1. Clone project,
  2. Enter in project folder,
  3. Create a virtualenv: python3 -m venv .venv,
  4. Activate virtualenv: source .venv/bin/activate,
  5. Install python dependencies: pip install -r requirements_prod.txt,
  6. Create a .env file in root of project and add FLASK_ENV and SECRET_KEY variables with your values.
    • FLASK_ENV - Flask environment, development or production. Ex: FLASK_ENV=development
    • SECRET_KEY - Your secret key into app. Ex: SECRET_KEY=your_secret_key
  7. Run flask db init && flask db migrate && flask db upgrade && flask seed to run migrations and create admin user,
  8. Run server: gunicorn wsgi:app --worker-class eventlet -w 1 --timeout 120 --no-sendfile -b 0.0.0.0:8000,
  9. Access http://your_ip:8000,
  10. Lisa is up.

Creating a systemd configuration

  1. Running with systemd you have many options to control application execution. This configuration starts application with O.S. start, and restarts automatically if something going wrong. Change pi with your system user.

sudo nano /etc/systemd/system/lisapi.service

[Unit]
Description=LisaPi Application
After=network.target

[Service]
User=pi
WorkingDirectory=/home/pi/lisapi
ExecStart=/home/pi/lisapi/.venv/bin/gunicorn --worker-class eventlet -w 1 --timeout 120 --no-sendfile -b 0.0.0.0:8000 wsgi:app
Restart=always

[Install]
WantedBy=multi-user.target
  1. Save file.

  2. Restart systemd configurations:

sudo systemctl daemon-reload
sudo systemctl start lisapi

  1. Access http://your_ip:8000 to open application.