Flask uWSGI and Nginx : systemctl status myproject : FAILED - python

First of all, I am already running a similar Python flask web server on the machine.
I am following this tutorial in order to host a web server with Python Flask.
And in the 5th step (No problem so far), when i test with :
systemctl status myproject
I get
serveurB.service - uWSGI instance to serve serveurB
Loaded: loaded (/etc/systemd/system/serveurB.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Mon 2021-06-28 14:25:45 UTC; 1min 53s ago
Process: 7992 ExecStart=/home/sydney/serveurB/serveurB/bin/uwsgi --ini serveurB.ini
(code=exited, status=1/FAILURE)
Main PID: 7992 (code=exited, status=1/FAILURE)
Jun 28 14:25:45 ecocathlon uwsgi[7992]: detected binary path: /home/sydney/serveurB/serveurB/bin/uwsgi
Jun 28 14:25:45 ecocathlon uwsgi[7992]: !!! no internal routing support, rebuild with pcre support !!!
Jun 28 14:25:45 ecocathlon uwsgi[7992]: your processes number limit is 3789
Jun 28 14:25:45 ecocathlon uwsgi[7992]: your memory page size is 4096 bytes
Jun 28 14:25:45 ecocathlon uwsgi[7992]: detected max file descriptor number: 1024
Jun 28 14:25:45 ecocathlon uwsgi[7992]: lock engine: pthread robust mutexes
Jun 28 14:25:45 ecocathlon uwsgi[7992]: thunder lock: disabled (you can enable it with --thunder-lock)
Jun 28 14:25:45 ecocathlon uwsgi[7992]: bind(): Permission denied [core/socket.c line 230]
Jun 28 14:25:45 ecocathlon systemd[1]: serveurB.service: Main process exited, code=exited, status=1/FAILURE
Jun 28 14:25:45 ecocathlon systemd[1]: serveurB.service: Failed with result 'exit-code'.
In which
myproject = serveurB
myprojectenv = serveurB (yes same name, i misstyped that one but i don't think it is the issue)
user = sydney
All my previous steps and files seem correct.
Thanks in advance,
Sydney R.

They key line in the error message is this one:
Jun 28 14:25:45 ecocathlon uwsgi[7992]: bind(): Permission denied [core/socket.c line 230]
bind is the method that will "bind" your socket to a port for listening and you are receiving a "Permission denied" error. That means you are using a low number port (< 1024) and are not root.
I would recommend changing the port to something higher than 1024 since running an app as root can be dangerous. There are other solutions as well but they are out of scope for this answer.

You have to be careful about the naming when setting up your project on digital ocean. The best way to make sure you not getting any naming error just make the project name consistent from the project and when setting up your server as well. For this I would suggest that you double check your nguni service file and see if you actually referenced your uWSGI project.

Related

Python Nginx Restart and Build

I’m building a web api with Flask. I got error when I use “sudo systemctl restart nginx” for ngnix restart after server installations, wsgi installations
$ sudo systemctl status nginx.servic
nginx.service - A high performance web server and a reverse proxy server
Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Mon 2021-12-06 09:36:31 UTC; 23h ago
Docs: man:nginx(8)
Process: 3210037 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=1/FAILURE)
Dec 06 09:36:31 systemd[1]: Starting A high performance web server and a reverse proxy server…
Dec 06 09:36:31 nginx[3210037]: nginx: [emerg] unknown directive “brotli” in /etc/nginx/conf.d/brotli.conf:1
Dec 06 09:36:31 nginx[3210037]: nginx: configuration file /etc/nginx/nginx.conf test failed
Dec 06 09:36:31 systemd[1]: nginx.service: Control process exited, code=exited, status=1/FAILURE
Dec 06 09:36:31 systemd[1]: nginx.service: Failed with result ‘exit-code’.
Dec 06 09:36:31 systemd[1]: Failed to start A high performance web server and a reverse proxy server.
How can I solve this?
I checked sites-enabled and sites-available?

Starting bottle web server through systemd?

I am trying to start a bottle web app I wrote using systemd. I made the file /etc/systemd/user/bottle.service with the following contents:
[Unit]
Description=Bottled fax service
After=syslog.target
[Service]
Type=simple
User=fax
Group=fax
WorkingDirectory=/home/fax/bottlefax/
ExecStart=/usr/bin/env python3 server.py
StandardOutput=syslog
StandardError=syslog
Restart=always
RestartSec=2
[Install]
WantedBy=bottle.target
However, when I try to start it, it fails and this is printed in journalctl:
Jun 10 17:33:31 nano systemd[1]: Started Bottled fax service.
Jun 10 17:33:31 nano systemd[1]: Starting Bottled fax service...
Jun 10 17:33:31 nano systemd[2380]: Failed at step GROUP spawning /usr/bin/env: No such process
Jun 10 17:33:31 nano systemd[1]: bottle.service: main process exited, code=exited, status=216/GROUP
Jun 10 17:33:31 nano systemd[1]: Unit bottle.service entered failed state.
Jun 10 17:33:31 nano systemd[1]: bottle.service failed.
How should I fix this?
Edit:
Changing to /usr/bin/python3 as others have suggested results in the same error (changed file though):
Jun 10 18:43:48 nano systemd[1]: Started Bottled fax service.
Jun 10 18:43:48 nano systemd[1]: Starting Bottled fax service...
Jun 10 18:43:48 nano systemd[2579]: Failed at step GROUP spawning /usr/bin/python3: No such process
Jun 10 18:43:48 nano systemd[1]: bottle.service: main process exited, code=exited, status=216/GROUP
Jun 10 18:43:48 nano systemd[1]: Unit bottle.service entered failed state.
Jun 10 18:43:48 nano systemd[1]: bottle.service failed.
I would have commented but I can't :/
Have you tried something like with an absolute path ?
ExecStart=/usr/bin/python3 /path/to/your/server.py
That's the only issue I can see here.
Another possible cause of this error, is if you have created a system user by running adduser --system, and have not created the associated group.

Python uwsgi setup on ec2

I'm trying to follow the tutorial at http://uwsgi-docs.readthedocs.org/en/latest/tutorials/Django_and_nginx.html. Following the directions I have:
(env1)ubuntu#ip-172-31-**-**:~/tproxy/tp$ uwsgi --http :8000 --wsgi-file test.py
*** Starting uWSGI 2.0.9 (64bit) on [Tue Mar 3 11:25:12 2015] ***
compiled with version: 4.8.2 on 03 March 2015 02:58:28
os: Linux-3.13.0-44-generic #73-Ubuntu SMP Tue Dec 16 00:22:43 UTC 2014
nodename: ip-172-31-**-**
machine: x86_64
clock source: unix
detected number of CPU cores: 1
current working directory: /home/ubuntu/tproxy/tp
detected binary path: /home/ubuntu/.virtualenvs/env1/bin/uwsgi
!!! no internal routing support, rebuild with pcre support !!!
*** WARNING: you are running uWSGI without its master process manager ***
your processes number limit is 7862
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 :8000 fd 4
spawned uWSGI http 1 (pid: 11221)
uwsgi socket 0 bound to TCP address 127.0.0.1:55522 (port auto-assigned) fd 3
Python version: 3.4.0 (default, Apr 11 2014, 13:08:40) [GCC 4.8.2]
*** Python threads support is disabled. You can enable it with --enable-threads ***
Python main interpreter initialized at 0x786830
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 72760 bytes (71 KB) for 1 cores
*** Operational MODE: single process ***
failed to open python file test.py
unable to load app 0 (mountpoint='') (callable not found or import error)
*** no app loaded. going in full dynamic mode ***
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI worker 1 (and the only) (pid: 11220, cores: 1)
Since I'm on an ec2 ubuntu instance I tried the opening public and private ips at port 8000, eg http://172.31..*:8000/ , but the request times out. What am I doing wrong?
edit: I tried to make the changes you suggested. Here is the output:
(env1)ubuntu#ip-172-31-28-196:~$ ls
host_type.py requirements.txt test.py tproxy
(env1)ubuntu#ip-172-31-28-196:~$ sudo chmod 666 test.py
(env1)ubuntu#ip-172-31-28-196:~$ uwsgi --http :8000 --wsgi-file test.py
*** Starting uWSGI 2.0.9 (64bit) on [Tue Mar 3 14:45:58 2015] ***
compiled with version: 4.8.2 on 03 March 2015 02:58:28
os: Linux-3.13.0-44-generic #73-Ubuntu SMP Tue Dec 16 00:22:43 UTC 2014
nodename: ip-172-31-28-196
machine: x86_64
clock source: unix
detected number of CPU cores: 1
current working directory: /home/ubuntu
detected binary path: /home/ubuntu/.virtualenvs/env1/bin/uwsgi
!!! no internal routing support, rebuild with pcre support !!!
*** WARNING: you are running uWSGI without its master process manager ***
your processes number limit is 7862
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)
probably another instance of uWSGI is running on the same address (:8000).
bind(): Address already in use [core/socket.c line 764]
(env1)ubuntu#ip-172-31-28-196:~$ ls -la
total 60
drwxr-xr-x 7 ubuntu ubuntu 4096 Mar 3 03:03 .
drwxr-xr-x 3 root root 4096 Feb 20 22:20 ..
-rw------- 1 ubuntu ubuntu 1554 Mar 3 11:35 .bash_history
-rw-r--r-- 1 ubuntu ubuntu 220 Apr 9 2014 .bash_logout
-rw------- 1 ubuntu ubuntu 3751 Feb 27 19:49 .bashrc
drwx------ 3 ubuntu ubuntu 4096 Feb 27 21:08 .cache
-rw-rw-r-- 1 ubuntu ubuntu 0 Feb 23 17:15 host_type.py
drwxrwxr-x 2 ubuntu ubuntu 4096 Feb 25 18:02 .pip
-rw-r--r-- 1 ubuntu ubuntu 675 Apr 9 2014 .profile
-rw-rw-r-- 1 ubuntu ubuntu 73 Mar 2 02:35 requirements.txt
drwx------ 2 ubuntu ubuntu 4096 Feb 20 22:20 .ssh
-rw-rw-rw- 1 root root 136 Mar 3 03:02 test.py
drwxrwxr-x 3 ubuntu ubuntu 4096 Mar 2 18:33 tproxy
-rw------- 1 ubuntu ubuntu 4119 Feb 27 19:51 .viminfo
drwxrwxr-x 3 ubuntu ubuntu 4096 Feb 28 02:17 .virtualenvs
(env1)ubuntu#ip-172-31-28-196:~$
It couldn't find test.py. Make sure it has permissions to the file. Did you make your virtuslenv with sudo? If so do a chmod and change the folder permissions to something your nginx and uwsgi has access to. Or it could be the path to the test.py file us wrong, double check that.

uwsgi emperor mod not working

when i start it without emperor it works:
uwsgi --ini /etc/uwsgi/vassals/uwsgi.conf --uid books --gid books
But when i use emperor mode it freezes:
uwsgi --emperor /etc/uwsgi/vassals --uid books --gid books
And give such response:
*** Starting uWSGI 2.0.1 (64bit) on [Fri Oct 24 10:47:46 2014] ***
compiled with version: 4.8.2 20140120 (Red Hat 4.8.2-16) on 24 October 2014 10:36:47
os: Linux-3.10.0-123.8.1.el7.x86_64 #1 SMP Mon Sep 22 19:06:58 UTC 2014
nodename: essenly.com
machine: x86_64
clock source: unix
pcre jit disabled
detected number of CPU cores: 1
current working directory: /etc/uwsgi/vassals
detected binary path: /usr/bin/uwsgi
setgid() to 1000
setuid() to 1000
*** WARNING: you are running uWSGI without its master process manager ***
your processes number limit is 3839
your memory page size is 4096 bytes
detected max file descriptor number: 1024
*** starting uWSGI Emperor ***
Thanks for your help.
UPDATE. Ignore my advice regarding master. Doc says that emperor mode should not be used with master normally. http://uwsgi-docs.readthedocs.org/en/latest/Emperor.html#notes
Try to run emperor with master process. Add master = true to your ini file and run uwsgi as root. Workers will downgrade to non-priviledged user (if specified)

uWSGI Fatal Python error: Py_Initialize: Unable to get the locale encoding

I've seen at least 3 questions about this problem.
Virtualenv (Python 3.2)
Django project in this env
uWSGI
But I have plugins = python3 in uWSGI settings file and it's ok!
Server is started, everything is working...
Restart server - and everything is still working...
Reload server (or touch reload-file) and you have this error:
Fatal Python error: Py_Initialize: Unable to get the locale encoding
ImportError: No module named encodings
Even more... I have another project in another Env and touch reload-file works for it.
Any suggestions why can this happen?
PS: I tried to create a new virtualenv
Logs:
After service uwsgi restart:
...
Sun Dec 29 22:57:21 2013 - detected max file descriptor number: 1024
Sun Dec 29 22:57:21 2013 - lock engine: pthread robust mutexes
Sun Dec 29 22:57:21 2013 - uwsgi socket 0 bound to UNIX address /run/uwsgi/app/test/socket fd 3
Sun Dec 29 22:52:59 2013 - uwsgi socket 1 bound to TCP address 127.0.0.1:3045 fd 5
Sun Dec 29 22:52:59 2013 - Python version: 3.2.3 (default, Sep 25 2013, 19:38:45) [GCC 4.7.2]
Sun Dec 29 22:52:59 2013 - Set PythonHome to /pyenvs/test/
Sun Dec 29 22:52:59 2013 - Python main interpreter initialized at 0x688cc0
Sun Dec 29 22:52:59 2013 - threads support enabled
Sun Dec 29 22:52:59 2013 - your server socket listen backlog is limited to 10000 connections
Sun Dec 29 22:52:59 2013 - *** Operational MODE: preforking ***
Sun Dec 29 22:52:59 2013 - WSGI app 0 (mountpoint='') ready in 0 seconds on interpreter 0x688cc0 pid: 1619 (default app)
Sun Dec 29 22:52:59 2013 - *** uWSGI is running in multiple interpreter mode ***
Sun Dec 29 22:52:59 2013 - spawned uWSGI master process (pid: 1619)
Sun Dec 29 22:52:59 2013 - spawned uWSGI worker 1 (pid: 1628, cores: 1)
Sun Dec 29 22:52:59 2013 - spawned uWSGI worker 2 (pid: 1629, cores: 1)
After service uwsgi reload:
Sun Dec 29 22:55:31 2013 - *** Starting uWSGI 1.2.3-debian (64bit) on [Sun Dec 29 22:55:31 2013] ***
...
Sun Dec 29 22:55:31 2013 - detected max file descriptor number: 1024
Sun Dec 29 22:55:31 2013 - lock engine: pthread robust mutexes
Sun Dec 29 22:55:31 2013 - uwsgi socket 0 inherited UNIX address /run/uwsgi/app/test/socket fd 3
Sun Dec 29 22:55:31 2013 - uwsgi socket 1 inherited INET address 127.0.0.1:3045 fd 5
Sun Dec 29 22:55:31 2013 - Python version: 3.2.3 (default, Sep 25 2013, 19:38:45) [GCC 4.7.2]
Sun Dec 29 22:55:31 2013 - Set PythonHome to /pyenvs/test/
Fatal Python error: Py_Initialize: Unable to get the locale encoding
ImportError: No module named encodings
Update 1
It is definitely virtual env. Copying my old VEnv and using didn't cause this problem.
Still have no idea why a newly created one doesn't work.
Copying only python executable didn't help.
I don't know exactly what happened since I did a lot of things:
System upgrade
New python environment
Python version 3.3, not 3.2
uwsgi 1.2.4 system package (perhaps this helped)
or better use pip install uwsgi in your VE and run what you need with supervisord
Now it's working. Thanks roberto.

Categories

Resources