I am trying to run a flask application with NGINX server on Pi Model 3B+. The code in the directory /home/pi/api/api.py. I am following this article https://www.raspberrypi-spy.co.uk/2018/12/running-flask-under-nginx-raspberry-pi/
My WSGI ini file
[uwsgi]
chdir = /home/pi/api
module = api:app
master = true
processes = 1
threads = 2
uid = www-data
gid = www-data
socket = /tmp/api.sock
chmod-socket = 664
vacuum = true
My uwsgi proxy after removing defaults
sudo nano /etc/nginx/sites-available/api_proxy
server {
listen 80;
server_name localhost;
location / { try_files $uri @app; }
location @app {
include uwsgi_params;
uwsgi_pass unix:/tmp/api.sock;
}
}
My uwsgi.service file
[Unit]
Description=uWSGI Service
After=network.target
[Service]
User=www-data
Group=www-data
WorkingDirectory=/home/pi/flasktest/
ExecStart=/usr/local/bin/uwsgi --ini /home/pi/api/uwsgi.ini
[Install]
WantedBy=multi-user.target
But when I start the server using
sudo systemctl status uwsgi.service
I get this error
uwsgi.service - uWSGI Service
Loaded: loaded (/etc/systemd/system/uwsgi.service; enabled; vendor preset: enabled)
Active: active (running) since Wed 2020-04-22 02:58:37 IST; 51min ago
Main PID: 464 (uwsgi)
Tasks: 3 (limit: 2319)
Memory: 13.5M
CGroup: /system.slice/uwsgi.service
├─464 /usr/local/bin/uwsgi --ini /home/pi/api/uwsgi.ini
└─728 /usr/local/bin/uwsgi --ini /home/pi/api/uwsgi.ini
Apr 22 03:40:32 websync uwsgi[464]: File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
Apr 22 03:40:32 websync uwsgi[464]: File "<frozen importlib._bootstrap_external>", line 724, in exec_module
Apr 22 03:40:32 websync uwsgi[464]: File "<frozen importlib._bootstrap_external>", line 859, in get_code
Apr 22 03:40:32 websync uwsgi[464]: File "<frozen importlib._bootstrap_external>", line 916, in get_data
Apr 22 03:40:32 websync uwsgi[464]: PermissionError: [Errno 13] Permission denied: './api.py'
Apr 22 03:40:32 websync uwsgi[464]: unable to load app 0 (mountpoint='') (callable not found or import error)
Apr 22 03:40:32 websync uwsgi[464]: *** no app loaded. going in full dynamic mode ***
Apr 22 03:40:32 websync uwsgi[464]: *** uWSGI is running in multiple interpreter mode ***
Apr 22 03:40:32 websync uwsgi[464]: gracefully (RE)spawned uWSGI master process (pid: 464)
Apr 22 03:40:32 websync uwsgi[464]: spawned uWSGI worker 1 (pid: 728, cores: 2)
Kindly help.
The error indicates that UWSGI does not have permission to access the file api.py. From your configuration, UWSGI is running with the user and group www-data. Check that that user or group has at least read permissions on that file.
As an aside, unless you need some of the capabilities of nginx (certificate handling, multiple concurrent connections, very fast response times for static files, or some other features), you could eliminate nginx and UWSGI and have that much less to worry about. If this is going to be a public-facing server, then yes, keep on your route. But if this is some private device with a simple web interface, eliminate complexity.