简体   繁体   English

使用 nginx + WSGI 运行 Flask 应用程序时 Python 子进程出错

[英]Error with Python subprocess when running Flask app using nginx + WSGI

I have developed a Python web server using Flask, and some of the endpoints make use of the subprocess module to call different executables.我使用 Flask 开发了一个 Python web 服务器,一些端点利用子进程模块调用不同的可执行文件。 On development, using the Flask debug server, everything works fine.在开发中,使用 Flask 调试服务器,一切正常。 However, when running the server along with nginx+WSGI (on the exact same machine), some subprocess calls fail.但是,当服务器与 nginx+WSGI 一起运行时(在同一台机器上),一些子进程调用会失败。

For example, one of the tools I'm using is Microsoft's do.net, which I installed from my user as sudo apt-get install -y as.netcore-runtime-5.0 and is then called from Python with the subprocess module.例如,我使用的工具之一是 Microsoft 的 do.net,我从我的用户那里安装了sudo apt-get install -y as.netcore-runtime-5.0 ,然后使用子进程模块从 Python 调用它。 When I run the server with python3 server.py , it works like a charm.当我使用python3 server.py运行服务器时,它就像一个魅力。 However, when using nginx and WSGI, the subprocess call fails with an exception that says: /bin/sh: 1: do.net: not found .但是,当使用 nginx 和 WSGI 时,子进程调用失败并出现异常: /bin/sh: 1: do.net: not found

I suspect this is due to the command not being accessible to the user and group running the server.我怀疑这是因为运行服务器的用户和组无法访问该命令。 I have used this guide as a reference to deploy the app, and on the wsgi.ini file, I have set uid = javierd and gid = www-data , while on the systemd.service file I have User=javierd , Group=www-data .我使用指南作为部署应用程序的参考,在 wsgi.ini 文件上,我设置了uid = javierdgid = www-data ,而在 systemd.service 文件上,我有User=javierdGroup=www-data

I have tried to add the executables' paths to /etc/profile , but it didn't work, and I don't know any other way to fix it.我试图将可执行文件的路径添加到/etc/profile ,但它没有用,而且我不知道任何其他修复方法。 I find also very surprising that this happens to some executables, but not to all, and that it happes to do.net, for example, which is located at /usr/bin/do.net and therefore should be accessible to every user.我也很惊讶这发生在一些可执行文件上,但不是全部,而且它发生在 do.net 上,例如,它位于/usr/bin/do.net ,因此每个用户都应该可以访问。 Any idea on how to solve this problem?关于如何解决这个问题的任何想法? Furthermore, if somebody could explain me why this is happening, I would really appreciate the effort.此外,如果有人可以向我解释为什么会发生这种情况,我将非常感谢您的努力。

Thanks a lot!多谢!

Ok, finally after having a big headache, I noticed the error, and it was really simple.好吧,终于在头疼了很久之后,发现了错误,果然很简单。

On the tutorial I linked, when creating the system service file, the following line was included: Environment="PATH=/home/myuser/myfolder/enviroment/bin" .在我链接的教程中,创建系统服务文件时,包含以下行: Environment="PATH=/home/myuser/myfolder/enviroment/bin"

Of course, as this was overriding the path, there was no way of executing the commands.当然,由于这是覆盖路径,因此无法执行命令。 Once I notices it I just removed that line, restarted the service, and it was fixed.一旦我注意到它,我就删除了那条线,重新启动服务,它就被修复了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM