python-3.xflaskuwsgisystemddirenv

Deploying Flask APP with UWSGI,Nginx,direnv and systemd


I have created an API using FLASK which I am trying to deploy on a linux server by creating a systemd service.

I have used direnv to setup input parameters to the app like database connections. Below is what the file looks like : enter image description here

The uwsgi config is as below :

enter image description here

The systemd file has the following entries:

enter image description here

I get the follwing error in my uwsgi logs whenever I try to reach the service on my browser :

--- no python application found, check your startup logs for errors --- [pid: 23791|app: -1|req: -1/3] 192.168.9.180 () {44 vars in 719 bytes} [Thu Oct 11 14:35:09 2018] GET / => generated 21 bytes in 0 msecs (HTTP/1.1 500) 2 headers in 83 bytes (1 switches on core 0)

My understanding is the ExecStart command in the systemd file is not able to invoke the direnv set variables , hence i added the ExecStartPre entry but even does not seem to work.

Any hints/ideas are appreciated.

Note: The application is accessible without errors when I run the uwsgi via command line from my python virtual environment :

uwsgi --socket 0.0.0.0:5000 --protocol=http -w app:app


Solution

  • i have a few advises that may help you, probably only the first one is the one you need...

    1) Either move all your env variable defined in direnv to the systemd unit as Environment or move them into a special file (similar to the you already have) without the "source activate" line and the export, and then pass that file as EnvironmentFile , this is the doc for that https://www.freedesktop.org/software/systemd/man/systemd.exec.html#Environment

    2) Your ExecStartPre does nothing really, even tho you do "cd" into the path, that is lost and is not persistent. you should remove it.

    3) By setting your PATH to only that path, you are restricting your self, i would recommend see the value of your current PATH and then set it to that value. but otherwise at least add "/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin". now spoiler alert, you probably dont need to set it.

    4) put the socket in /run//socket.socket directory and let systemd manage your /run/<yourapp> with RuntimeDirectory directive.

    good luck!