Running flask automatically after boot [duplicate] - flask

This question already has answers here:
Are a WSGI server and HTTP server required to serve a Flask app?
(3 answers)
How to enable a virtualenv in a systemd service unit?
(4 answers)
Closed 1 year ago.
I'm trying to run flask when my raspberry pi turns on.
I created a service for my flask API, this is the service file:
[Unit]
Description= Flask API
After=network.target
[Service]
WorkingDirectory=/home/pi/bucketlist/
ExecStart=flask run --host=0.0.0.0
Restart=Always
[Install]
WantedBy=multi-user.target
But when I try sudo systemctl start flask then sudo systemctl status flask I get this error.
Jun 10 09:56:40 raspberrypi flask[4769]: Usage: flask run [OPTIONS]
Jun 10 09:56:40 raspberrypi flask[4769]: Error: While importing "app", an ImportError was raised:
Jun 10 09:56:40 raspberrypi flask[4769]: Traceback (most recent call last):
Jun 10 09:56:40 raspberrypi flask[4769]: File "/usr/lib/python3/dist-packages/flask/cli.py", line 235, in locate_app
Jun 10 09:56:40 raspberrypi flask[4769]: __import__(module_name)
Jun 10 09:56:40 raspberrypi flask[4769]: File "/home/pi/bucketlist/app/__init__.py", line 5, in <module>
Jun 10 09:56:40 raspberrypi flask[4769]: from flask_api import FlaskAPI
Jun 10 09:56:40 raspberrypi flask[4769]: ModuleNotFoundError: No module named 'flask_api'
Jun 10 09:56:40 raspberrypi systemd[1]: flask_api.service: Main process exited, code=exited, status=2/INVALIDARGUMENT
Jun 10 09:56:40 raspberrypi systemd[1]: flask_api.service: Failed with result 'exit-code'.
When I type flask run --host=0.0.0.0 manually it works fine, I don't know why the service gives this error.
More info:
I also tried python /home/pi/bucketlist/run.py instead of flask run --host=0.0.0.0 in ExecStart.
I have a virtual environment set-up in the bucketlist directory.
Python 2.7.16
Flask 1.1.4
Werkzeug 1.0.1

Related

apache wsgi_mod fails to run on python 3.10

I am developing a django app using django 4 and python 3.10.
I had a server using apache and mod_wsgi, that used to run an old app using python3.8.
I've created a virtual env from 3.10. Command line manage.py is working well.
I've changed apache's conf:
WSGIDaemonProcess my_app310 python-path=/home/ubuntu/my_app310:/home/ubuntu/venv/my_app310/lib/python3.10/site-packages
WSGIProcessGroup my_app310
WSGIScriptAlias / /home/ubuntu/my_app310/my_app310/wsgi.py
But I keep getting:
[mpm_event:notice] AH00489: Apache/2.4.41 (Ubuntu) mod_wsgi/4.6.8 Python/3.8 configured -- resuming normal operations
[core:notice] AH00094: Command line: '/usr/sbin/apache2'
[wsgi:error] mod_wsgi (pid=1724834): Failed to exec Python script file '/home/ubuntu/my_app310/my_app310/wsgi.py'.
[wsgi:error] mod_wsgi (pid=1724834): Exception occurred processing WSGI script '/home/ubuntu/my_app310/my_app310/wsgi.py'.
[wsgi:error] Traceback (most recent call last):
[wsgi:error] File "/home/ubuntu/venv/my_app310/lib/python3.10/site-packages/django/utils/timezone.py", line 10, in <module>
[wsgi:error] import zoneinfo
[wsgi:error] ModuleNotFoundError: No module named 'zoneinfo'
Which suggest that something is running using python3.8 and not python3.10 (mod_wsgi?). What am I missing?
to configure apache2 with a certain Python Version just do the following:
go into your virtual_env that you have created with Python3.10
$ pip install mod_wsgi
$ mod_wsgi-express module-config
you get an output like
LoadModule wsgi_module "..../lib/python3.10/site-packages/mod_wsgi/server/mod_wsgi-py310.cpython-310-x86_64-linux-gnu.so"
SSGIPythonHome "..."
copy the 2 lines into /etc/apache2/apache2.conf
restart apache2
you should see something like
[mpm_event:notice] AH00489: Apache/2.4.41 (Ubuntu) mod_wsgi/4.6.8 Python/3.10 config .....
WSGI Clearly says that File "/home/ubuntu/venv/my_app310/lib/python3.10/site-packages/django/utils/timezone.py", given this path has no module named zoneinfo try importing module into your python virtual environment.
Example:
pip install (module)

uwsgi in virtualenv but attach-daemon for django doesn't get venv

I'm building a django project (mailman3) and accessing it with uwsgi. I have it running successfully when launching uwsgi within the virtualenv from the command line.
I'm trying to build a systemd service to manage uwsgi. It successfully loads the virtual environment for uwsgi and runs. But when it tries to run the django process with attach-daemon, manage.py can't find the django module, i.e., it's not picking up the virtual environment.
In the /etc/uwsgi.ini file I have:
virtualenv = /opt/mailman/venv
chdir = /opt/mailman/mailman-suite/mailman-suite_project
attach-daemon = ./manage.py qcluster
The systemd service has:
ExecStart=/opt/mailman/venv/bin/uwsgi --ini /etc/uwsgi.ini
When systemd starts the service, my error log reports:
[...]
WSGI app 0 (mountpoint='') ready in 1 seconds on interpreter 0x558c5945bc30 pid: 15392 (default app)
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI master process (pid: 15392)
spawned uWSGI worker 1 (pid: 15416, cores: 2)
Traceback (most recent call last):
File "./manage.py", line 8, in <module>
from django.core.management import execute_from_command_line
ModuleNotFoundError: No module named 'django'
Here's the puzzle piece:
It worked running locally with the virtualenv active. But when run by systemd, even using uwsgi from within the virtualenv, the attach-daemon process would not inherit the virtualenv.
Changing the config line to the following enables the virtualenv:
attach-daemon = /opt/mailman/venv/bin/python3 ./manage.py qcluster

ModuleNotFoundError: No module named 'gunicorn'

I followed a [DigitalOcean guide]((https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-debian-10) to set up my Django site. It usually works fine but lately I keep running into this.
root#localhost:/usr/bin# sudo systemctl status gunicorn
Jun 13 21:27:43 localhost systemd[1]: Started gunicorn daemon.
Jun 13 21:27:43 localhost gunicorn[20611]: Traceback (most recent call last):
Jun 13 21:27:43 localhost gunicorn[20611]: File "/usr/bin/gunicorn", line 6, in <module>
Jun 13 21:27:43 localhost gunicorn[20611]: from gunicorn.app.wsgiapp import run
Jun 13 21:27:43 localhost gunicorn[20611]: ModuleNotFoundError: No module named 'gunicorn'
Jun 13 21:27:43 localhost systemd[1]: gunicorn.service: Main process exited, code=exited, status=1/FAILURE
Jun 13 21:27:43 localhost systemd[1]: gunicorn.service: Failed with result 'exit-code'.
Jun 13 21:27:43 localhost systemd[1]: gunicorn.service: Start request repeated too quickly.
Jun 13 21:27:43 localhost systemd[1]: gunicorn.service: Failed with result 'exit-code'.
Jun 13 21:27:43 localhost systemd[1]: Failed to start gunicorn daemon.
root#localhost:/home/development/django# which gunicorn
/usr/bin/gunicorn
/etc/systemd/system/gunicorn.service
[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target
[Service]
User=root
Group=www-data
WorkingDirectory=/home/development/django/
ExecStart=/usr/bin/gunicorn \
--access-logfile - \
--workers 3 \
--bind unix:/run/gunicorn.sock \
app.wsgi:application
[Install]
WantedBy=multi-user.target
I had the exact same problem and was going quite mad but here is what fixed it :
I guess systemd executes with sudo, but I did the pip install blabla without being in sudo.
Found out by doing sudo gunicorn --version and getting nothing.
So just doing sudo pip install gunicorn solved it for me !
Disclaimer : I am probably doing something not at all in the "best practices" so don't hesitate to correct me or give a more detailed response than mine : First time answering :D

Manually start uwsgi with python3.6 and python2 and django 2

I just started to use uWSGI and Django. I want to invoke it manually.
I'm using python36, Django 2 and if I install uWSGI (pip install uwsgi) in venv and everything is ok.
Q1. When using outside of venv I first installed uWSGI:
pip install uwsgi -> 2.0.18 version
Installed plugins: sudo apt-get install python3-setuptools
Trying to run with python36:
uwsgi --plugin-dir=/usr/lib/uwsgi/plugins --plugin python3 --master --http :5000 --home ~/my_playground/webapps/ --chdir ~/my_playground/webapps/p_tscze/ --module p_tscze.wsgi:application
Plugins in /usr/lib/uwsgi/plugins:
/usr/lib/uwsgi/plugins$ ls | grep python3
asyncio_python36_plugin.so
asyncio_python3_plugin.so
python36_plugin.so
python3_plugin.so
Result obtained -> note python 2.7:
!!! UNABLE to load uWSGI plugin: ./python36_plugin.so: undefined symbol: uwsgi_legion_scrolls !!!
*** Starting uWSGI 2.0.18 (64bit) on [Wed May 29 18:04:24 2019] ***
compiled with version: 7.4.0 on 29 May 2019 15:30:14
os: Linux-4.15.0-45-generic #48-Ubuntu SMP Tue Jan 29 16:28:13 UTC 2019
nodename: start-tehnicka
machine: x86_64
clock source: unix
pcre jit disabled
detected number of CPU cores: 1
current working directory: /usr/lib/uwsgi/plugins
detected binary path: /usr/local/bin/uwsgi
chdir() to /home/anel/my_playground/webapps/p_tscze/
your processes number limit is 3618
your memory page size is 4096 bytes
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uWSGI http bound on :5000 fd 4
uwsgi socket 0 bound to TCP address 127.0.0.1:34279 (port auto-assigned) fd 3
Python version: 2.7.15rc1 (default, Nov 12 2018, 14:31:15) [GCC 7.3.0]
Set PythonHome to /home/anel/my_playground/webapps/
ImportError: No module named site
So please can you let me know what I'm doing wrong?
Q2. When comparing the result of the admin page (./manage.py runserver) and result obtained using uwsgi frontend is not the same, why?
Thanks in advance
For future references:
wget https://projects.unbit.it/downloads/uwsgi-2.0.18.tar.gz
tar -xzvf uwsgi-2.0.18.tar.gz
cd uwsgi-2.0.18
python3 uwsgiconfig.py --build nolang #build binary with python3
python3 uwsgiconfig.py --plugin plugins/python nolang python36 # create a new binary
Run newly created binary uwsgi (we can link to this binary):
./uwsgi --plugin-dir=/home/anel/uwsgi-2.0.18 --plugin python36 --master --http :5000 --home ~/my_playground/webapps/ --chdir ~/my_playground/webapps/p_tscze/ --module p_tscze.wsgi:application

uwsgi + python2.7 unable to import anything

I'm trying to use uwsgi + python2.7 + django + nginx on debian 6.
I installed uwsgi with the command: pip2.7 install uwsgi so it's running with python 2.7.
I'm running uwsgi in emperor mode with the command:
uwsgi --emperor /etc/uwsgi/vassals/ -d /var/log/uwsgi.log --pidfile /var/run/uwsgi.pid
The vassals folder contains only one app for the moment. Here is it's yaml file:
uwsgi:
socket: /home/uwsgi/uwsgi/uwsgi.sock
virtualenv: /opt/myproj/virt
pythonpath: /home/uwsgi/project/my_proj
pidfile: /home/uwsgi/uwsgi/uwsgi.pid
uid: uwsgi
gid: uwsgi
chmod-socket: 1
module: wsgi_app
daemonize: /home/uwsgi/uwsgi/uwsgi.log
harakiri-verbose: 1
This is the content of the Django project file:
#! /usr/bin/python
SITE_DIR = '/home/uwsgi/project/my_proj'
import site
site.addsitedir(SITE_DIR)
import os
import sys
sys.path.append(SITE_DIR)
sys.path.append('/home/uwsgi/project/')
os.environ['DJANGO_SETTINGS_MODULE'] = 'my_proj.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
Now, here is the result in the /home/uwsgi/uwsgi/uwsgi.log file when I try to launch it:
*** Starting uWSGI 1.0.4 (32bit) on [Fri Mar 2 19:00:46 2012] ***
compiled with version: 4.6.2 on 02 March 2012 18:03:07
current working directory: /etc/uwsgi/vassals
writing pidfile to /home/uwsgi/uwsgi/uwsgi.pid
detected binary path: /usr/local/bin/uwsgi
setgid() to 1001
setuid() to 1001
your memory page size is 4096 bytes
chmod() socket to 666 for lazy and brave users
uwsgi socket 0 bound to UNIX address /home/uwsgi/uwsgi/uwsgi.sock fd 3
Python version: 2.7.2+ (default, Dec 1 2011, 02:17:49) [GCC 4.6.2]
Set PythonHome to /opt/myproj/virt
ImportError: No module named site
*** Starting uWSGI 1.0.4 (32bit) on [Fri Mar 2 19:00:47 2012] ***
compiled with version: 4.6.2 on 02 March 2012 18:03:07
current working directory: /etc/uwsgi/vassals
writing pidfile to /home/uwsgi/uwsgi/uwsgi.pid
detected binary path: /usr/local/bin/uwsgi
setgid() to 1001
setuid() to 1001
your memory page size is 4096 bytes
chmod() socket to 666 for lazy and brave users
uwsgi socket 0 bound to UNIX address /home/uwsgi/uwsgi/uwsgi.sock fd 3
Python version: 2.7.2+ (default, Dec 1 2011, 02:17:49) [GCC 4.6.2]
Set PythonHome to /opt/myproj/virt
ImportError: No module named site
As you can see, uwsgi can't import the site module so uwsgi keeps trying to restart the app.
So I tried to add the no-site :1 option in the yaml file. The result is that I can't import anything in my Django project file...
I also know it's not linked to the project because if I run uwsgi with python 2.6, it just works fine... Unfortunally, I have to run it with python2.7...
Do you have any idea what happens ?
Thanks a lot!
Be sure your virtualenv is built for python2.7, otherwise it cannot be used.
It has to contains /opt/myproj/virt/lib/python2.7 directory