Some considerations for configuring Nginx + uWSGI to host Flask applications under virtualenv in Ubuntu 1404

WBOY
Release: 2016-07-29 08:52:21
Original
1055 people have browsed it

The title is very long, but the content of this article is very simple. In today's Python development, virtual environments are essential, but sometimes virtual environments can bring us some unnecessary troubles or pitfalls, such as the issue of Flask application hosting that we will talk about today.

Nginx + uWSGI can be said to be the best choice for Python web project hosting. Of course, if you only develop and test by yourself, or apply it to applications with very small traffic, Gunicorn or even the web server that comes with the web framework may be enough. , but we can't escape the situation of encountering this king combination. Today I encountered such a pit. After configuring Nginx and uWSGI respectively according to the official documentation, and using the command statement of uwsgi to start, there is no problem. However, after trying to use the ini configuration to start uWSGI, you will encounter an error of 502 bad gateway.

After some troubleshooting - the troubleshooting process is not clear - I found the problem. My uWSGI was installed in the virtualenv directory of the project, but the ini configuration file did not configure the virtual environment directory, and the existing blog tutorials on the Internet said The ini configuration does not mention this issue, so we add the configuration of the virtual environment, and note that it is best to add the configuration plugin = python. Of course, this needs to be installed in advance. How to install it specifically is not The key points of this article will not be elaborated for now.

So, my final ini configuration file content is as follows:

<code><span>[uwsgi]</span><span>plugins = <span>python</span></span><span>vhost = <span><span>true</span></span></span><span>chdir = <span>/home/k/Documents/demo</span></span><span>venv = <span>/home/k/Documents/demo/venv</span></span>#python module to import
<span>app = <span>main</span></span><span>module = <span>%(app)</span></span>#socket file's location
<span>socket = <span>/home/k/Documents/demo/%n.sock</span></span>#permissions for the socket file
<span>chmod-socket    = <span><span>666</span></span></span>#the variable that holds a flask application inside the module imported at line #6
<span>callable = <span>app</span></span>#location of log files
<span>logto = <span>/home/k/Documents/demo/%n.log</span></span><span>processes = <span><span>4</span></span></span><span>threads = <span><span>2</span></span></span></code>
Copy after login

And my project directory structure is:

<code>demo/
|____venv/
|____main<span>.py</span><span># 这个是应用的启动文件(对应 ini 配置中的 app)</span>
|____uwsgi<span>.ini</span>
|____uwsgi<span>.sock</span>
|____uwsgi<span>.log</span>
|____access<span>.log</span><span># 这个是 Nginx 中的 access 日志配置</span>
|____error<span>.log</span><span># 这个是 Nginx 中的 error 日志配置</span></code>
Copy after login
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

The above introduces some precautions for configuring Nginx + uWSGI to host Flask application under virtualenv in Ubuntu 1404, including some aspects. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!