nginx和uwsgi部署_使用nginx和uWSGI Emperor部署Python Web应用程序

nginx和uwsgi部署

You just wrote a great Python web application. Now, you want to share it with the world. In order to do that, you need a server, and some software to do that for you.

您刚刚编写了一个很棒的Python Web应用程序。 现在,您想与世界分享。 为此,您需要一台服务器和一些软件来为您执行此操作。

The following is a comprehensive guide on how to accomplish that, on multiple Linux-based operating systems, using nginx and uWSGI Emperor. It doesn’t force you to use any specific web framework — Flask, Django, Pyramid, Bottle will all work. Written for Ubuntu, Fedora and Arch Linux (should be helpful for other systems, too)

以下是有关如何使用nginx和uWSGI Emperor在多个基于Linux的操作系统上实现此目标的综合指南。 它不会强迫您使用任何特定的Web框架-Flask,Django,Pyramid,Bottle都可以使用。 为Ubuntu,Fedora和Arch Linux写的(对其他系统也应该有帮助)

入门 (Getting Started)

In order to deploy your web application, you need a server that gives you root and ssh access — in other words, a VPS (or a dedicated server, or a datacenter lease…). If you’re looking for a great VPS service for a low price, I recommend DigitalOcean (reflink [1]), which offers a $5/mo service [2]. If you want to play along at home, without buying a VPS, you can create a virtual machine on your own, or use a Vagrant with a Vagrant box for Fedora 23 (I recommend disabling SELinux, more on that later).

为了部署Web应用程序,您需要一台具有root和ssh访问权限的服务器-换句话说,是VPS(或专用服务器或数据中心租用…)。 如果您想以低廉的价格寻求优质的VPS服务,我建议您使用DigitalOcean (reflink [1] ),该服务每月提供$ 5的服务[2] 。 如果您想在家中玩游戏而无需购买VPS,则可以自己创建虚拟机,或者在Fedora 23上使用带有Vagrant的Vagrant框 (我建议禁用SELinux,稍后再介绍)。

Your server should also run a modern Linux-based operating system. I tested and wrote this guide for Ubuntu 15.10 [3], Fedora 23 and Arch Linux, but other Linux distributions (and perhaps *BSD) will work (in places where the instructions are split three-way, try coming up with your own, reading documentation and config files). Unfortunately, all Linux distributions have their own ideas when it comes to running and managing nginx and UWSGI.

您的服务器还应该运行基于Linux的现代操作系统。 我测试并编写了适用于Ubuntu 15.10 [3] ,Fedora 23和Arch Linux的指南,但是其他Linux发行版(也许* BSD)也可以使用(在将指令分为三部分的地方,尝试自己编写,阅读文档和配置文件)。 不幸的是,在运行和管理Nginx和UWSGI方面,所有Linux发行版都有自己的想法。

Note

注意

All the commands in this tutorial are meant to be run as root — run su or sudo su first to get an administrative shell.

本教程中的所有命令均应以root用户身份运行-首先运行susudo su以获得管理外壳程序。

Start by installing virtualenv, nginx and uWSGI. I recommend using your operating system packages. For uWSGI, we need the logfile and python3 plugins. (Arch Linux names the python3 plugin python; the logfile plugin may be built-in — check with your system repositories!).

首先安装virtualenv,nginx和uWSGI。 我建议您使用操作系统软件包。 对于uWSGI,我们需要日志文件python3插件。 (Arch Linux将python3插件命名为python日志文件插件可能是内置的-请检查系统存储库!)。

Ubuntu:

Ubuntu:

aptitude install virtualenv python3 uwsgi uwsgi-emperor uwsgi-plugin-python3 nginx-full
aptitude install virtualenv python3 uwsgi uwsgi-emperor uwsgi-plugin-python3 nginx-full

Fedora:

软呢帽:

Arch Linux:

Arch Linux:

pacman -S python-virtualenv uwsgi uwsgi-plugin-python nginx
pacman -S python-virtualenv uwsgi uwsgi-plugin-python nginx

准备申请 (Preparing your application)

This tutorial will work for any web framework. I will, use a really basic Flask app that has just one route (/), a static hello.png file and a favicon.ico for demonstration purposes. Note that the app does not use app.run(). While you could add it, it would be used for local development and debugging only, and would be prepended by if __name__ == '__main__': — uWSGI doesn’t work alongside it.

本教程适用于任何Web框架。 我将使用一个只有一个路由( / ),一个静态hello.png文件和一个favicon.ico 的非常基本的Flask应用程序进行演示。 请注意,该应用程序不使用app.run() 。 尽管可以添加它,但它只能用于本地开发和调试,并且如果__name__ =='__main__' ,将以它为前缀 — uWSGI不能与其一起使用。

The app will be installed somewhere under the /srv directory, which is a great place to store things like this. I’ll choose /srv/myapp for this tutorial, but for real deployments, you should use sometihing more distinguishable — the domain name is a great idea.

该应用程序将安装在/ srv目录下的某个位置,这是存储此类内容的好地方。 在本教程中,我将选择/ srv / myapp ,但是对于实际的部署,您应该使用更具区别性的名称-域名是一个好主意。

We’ll start by creating a virtualenv:

我们将从创建一个virtualenv开始:

Ubuntu:

Ubuntu:

Fedora:

软呢帽:

 cd /srv
cd /srv
virtualenv-3.4 myapp
virtualenv-3.4 myapp

Arch Linux:

Arch Linux:

(Make sure you create a Python 3 environment!)

(确保您创建了Python 3环境!)

Now, we need to get our app there and install requirements. An example for the tutorial demo app (adjust for your clone/download path):

现在,我们需要在那里安装我们的应用程序并安装要求。 教程演示应用程序的示例(根据您的克隆/下载路径进行调整):

 cd myapp
cd  myapp
cp -r ~/git/flask-demo-app appdata
cp -r ~/git/flask-demo-app appdata
bin/pip install -r appdata/requirements.txt
bin/pip install -r appdata/requirements.txt

I’m storing my application data in the appdata subdirectory so that it doesn’t clutter the virtualenv (or vice versa). You may also install the uwsgi package in the virtualenv, but it’s optional.

我将应用程序数据存储在appdata子目录中,以免使virtualenv混乱(反之亦然)。 您也可以将uwsgi软件包安装在virtualenv中,但这是可选的。

What this directory should be depends on your web framework. For example, for a Django app, you should have an appdata/manage.py file (in other words, appdata is where your app structure starts). I also assumed that the appdata folder should have a static subdirectory with all static files, including favicon.ico if you have one (we will add support for both in nginx).

该目录应该是什么取决于您的Web框架。 例如,对于Django应用,您应该有一个appdata / manage.py文件(换句话说, appdata是您的应用结构开始的位置)。 我还假设appdata文件夹应具有一个包含所有静态文件的静态子目录,如果有的话,包括favicon.ico (我们将在nginx中添加对这两个文件的支持)。

At this point, you should chown this directory to the user and group your server is going to run as. This is especially important if uwsgi and nginx run as different users (as they do on Fedora). Run one of the following commands:

在这一点上,您应该将此目录授予用户,并且将服务器作为运行组。 如果uwsgi和nginx以不同的用户身份运行(就像在Fedora上一样),这一点尤其重要。 运行以下命令之一:

Ubuntu:

Ubuntu:

Fedora:

软呢帽:

chown -R uwsgi:nginx /srv/myapp
chown -R uwsgi:nginx /srv/myapp

Arch Linux:

Arch Linux:

配置uWSGI和Nginx (Configuring uWSGI and nginx)

Note

注意

Parts of the configuration depend on your operating system. I tried to provide advice for Ubuntu, Fedora and Arch Linux. If you experience any issues, in particular with plugins, please consult the documentation.

部分配置取决于您的操作系统。 我试图为Ubuntu,Fedora和Arch Linux提供建议。 如果您遇到任何问题,尤其是插件问题,请查阅文档。

We need to write a configuration file for uWSGI and nginx.

我们需要为uWSGI和nginx编写一个配置文件。

uWSGI配置 (uWSGI configuration)

Start with this, but read the notes below and change the values accordingly:

从此开始,但请阅读以下说明并相应地更改值:

 [uwsgi]
[uwsgi]
 socket socket = = /srv/myapp/uwsgi.sock
/srv/myapp/uwsgi.sock
 chmod-socket chmod-socket = = 775
775
 chdir chdir = = /srv/myapp/appdata
/srv/myapp/appdata
 master master = = true
true
 binary-path binary-path = = /srv/myapp/bin/uwsgi
/srv/myapp/bin/uwsgi
 virtualenv virtualenv = = /srv/myapp
/srv/myapp
 module module = = flaskapp:app
flaskapp:app
 uid uid = = www-data
www-data
 gid gid = = www-data
www-data
 processes processes = = 1
1
 threads threads = = 1
1
 plugins plugins = = python3,logfile
python3,logfile
 logger logger = = file:/srv/myapp/uwsgi.log
file:/srv/myapp/uwsgi.log

Save this file as:

将此文件另存为:

  • Ubuntu: /etc/uwsgi-emperor/vassals/myapp.ini
  • Fedora: /etc/uwsgi.d/myapp.ini
  • Arch Linux: /etc/uwsgi/vassals/myapp.ini (create the directory first and chown it to http: mkdir -p /etc/uwsgi/vassals; chown -R http:http /etc/uwsgi/vassals)
  • Ubuntu: /etc/uwsgi-emperor/vassals/myapp.ini
  • Fedora: /etc/uwsgi.d/myapp.ini
  • Arch Linux: /etc/uwsgi/vassals/myapp.ini (首先创建目录并将其chown到http: mkdir -p / etc / uwsgi / vassals; chown -R http:http / etc / uwsgi / vassals

The options are:

选项包括:

  • socket — the socket file that will be used by your application. It’s usually a file path (Unix domain socket). You could use a local TCP socket, but it’s not recommended.
  • chdir — the app directory.
  • binary-path — the uWSGI executable to use. Remove if you didn’t install the (optional) uwsgi package in your virtualenv.
  • virtualenv — the virtualenv for your application.
  • module — the name of the module that houses your application, and the object that speaks the WSGI interface, separated by colons. This depends on your web framework:
    • For Flask: module = filename:app, where filename is the name of your Python file (without the .py part) and app is the Flask object
    • For Django: module = project.wsgi:application, where project is the name of your project (directory with settings.py). You should also add an environment variable: env = DJANGO_SETTINGS_MODULE=project.settings
    • For Bottle: module = filename:app, where app = bottle.default_app()
    • For Pyramid: module = filename:app, where app = config.make_wsgi_app() (make sure it’s not in a if __name__ == '__main__': block — the demo app does that!)
  • uid and gid — the names of the user account to use for your server. Use the same values as in the chown command above.
  • processes and threads — control the resources devoted to this application. Because this is a simple hello app, I used one process with one thread, but for a real app, you will probably need more (you need to see what works the best; there is no algorithm to decide). Also, remember that if you use multiple processes, they don’t share data, so you need an out-of-process database if you want that.
  • plugins — the list of uWSGI plugins to use. For Arch Linux, use plugins = python (the logfile plugin is always active).
  • logger — the path to your app-specific logfile. (Other logging facilities are available, but this one is the easiest, especially for multiple applications on the same server)
  • socket —您的应用程序将使用的套接字文件。 通常是文件路径(Unix域套接字)。 您可以使用本地TCP套接字,但不建议这样做。
  • chdir —应用程序目录。
  • 二进制路径 -要使用的uWSGI可执行文件。 如果您没有在virtualenv中安装(可选) uwsgi软件包,请删除。
  • virtualenv —您的应用程序的virtualenv。
  • module-包含您的应用程序的模块的名称,以及表示WSGI接口的对象,以冒号分隔。 这取决于您的Web框架:
    • 对于Flask: module = filename:app ,其中filename是您的Python文件的名称(不带.py部分),而appFlask对象
    • 对于Django: module = project.wsgi:application ,其中project项目的名称(具有settings.py的目录)。 您还应该添加一个环境变量: env = DJANGO_SETTINGS_MODULE = project.settings
    • 对于Bottle: module = filename:app ,其中app = bottle.default_app()
    • 对于金字塔: module = filename:app ,其中app = config.make_wsgi_app() (确保它不在 if __name__ =='__main__': block中-演示应用程序会这样做 !)
  • uidgid-用于服务器的用户帐户的名称。 使用与上面的chown命令相同的值。
  • 进程线程 -控制用于此应用程序的资源。 因为这是一个简单的hello应用程序,所以我使用了一个进程和一个线程,但是对于真正的应用程序,您可能需要更多(您需要了解哪种方法最有效;没有算法可决定)。 另外,请记住,如果您使用多个进程,则它们不会共享数据,因此,如果需要,您需要一个进程外数据库。
  • 插件 - uWSGI列表插件使用。 对于Arch Linux,请使用plugins = python日志文件插件始终处于活动状态)。
  • logger-特定于应用程序的日志文件的路径。 (可以使用其他日志记录功能,但这是最简单的,尤其是对于同一服务器上的多个应用程序而言)

You can test your configuration by running uwsgi --ini /path/to/myapp.ini (disable the logger for stderr output or run tail -f /srv/myapp/uwsgi.log in another window).

您可以通过运行uwsgi --ini /path/to/myapp.ini (禁用记录程序的stderr输出或在另一个窗口中运行tail -f /srv/myapp/uwsgi.log )来测试配置。

If you’re using Fedora, there are two configuration changes you need to make globally: in /etc/uwsgi.ini, disable the emperor-tyrant option (which seems to be buggy) and set gid = nginx. We’ll need this so that nginx can talk to your socket.

如果您使用的是Fedora ,则需要全局进行两项配置更改:在/etc/uwsgi.ini中 ,禁用emperor-tyrant选项(这似乎是错误的)并设置gid = nginx 。 我们需要它,以便nginx可以与您的套接字通信。

nginx配置 (nginx configuration)

We need to configure our web server. Here’s a basic configuration that will get us started:

我们需要配置我们的Web服务器。 这是使我们入门的基本配置:

Save this file as:

将此文件另存为:

  • Ubuntu: /etc/nginx/sites-enabled/myapp.conf
  • Fedora: /etc/nginx/conf.d/myapp.conf
  • Arch Linux: add include /etc/nginx/conf.d/*.conf; to your http directive in /etc/nginx/nginx.conf and use /etc/nginx/conf.d/myapp.conf
  • Ubuntu: /etc/nginx/sites-enabled/myapp.conf
  • Fedora: /etc/nginx/conf.d/myapp.conf
  • Arch Linux:添加include /etc/nginx/conf.d/*.conf;/etc/nginx/nginx.conf中http指令,并使用/etc/nginx/conf.d/myapp.conf

Note that this file is a very basic and rudimentary configuration. This configuration is fine for local testing, but for a real deployment, you will need to adjust it:

请注意,此文件是非常基本且基本的配置。 此配置适用于本地测试,但对于实际部署,您需要对其进行调整:

  • set listen to 443 ssl and create a http→https redirect on port 80 (you can get a free SSL certificate from Let’s Encrypt; make sure to configure SSL properly).
  • set server_name to your real domain name
  • you might also want to add custom error pages, or change anything else that relates to your web server — consult other nginx guides for details
  • nginx might have some server already enabled by default — edit /etc/nginx/nginx.conf to disable it
  • 设置监听 443 SSL并在端口80上创建http→https重定向(您可以从Let's Encrypt获得免费的SSL证书;请确保正确配置SSL )。
  • server_name设置为您的真实域名
  • 您可能还想添加自定义错误页面,或更改与您的Web服务器相关的其他任何内容-有关详细信息,请参阅其他nginx指南
  • 默认情况下,nginx可能已启用了某些服务器-编辑/etc/nginx/nginx.conf以将其禁用

服务设置 (Service setup)

After you’ve configured uWSGI and nginx, you need to enable and start the system services.

配置uWSGI和nginx之后,需要启用并启动系统服务。

I’m going to use systemd here. If your system does not support systemd, please consult your OS documentation for instructions.

我将在这里使用systemd 。 如果您的系统不支持systemd ,请查阅操作系统文档以获取说明。

对于Arch Linux (For Arch Linux)

All you need is:

所有你需要的是:

systemctl systemctl enable nginx emperor.uwsgi
enable  nginx emperor.uwsgi
systemctl start nginx emperor.uwsgi
systemctl start nginx emperor.uwsgi

Verify the service is running with systemctl status emperor.uwsgi

使用systemctl status emperor.uwsgi验证服务是否正在运行

对于Fedora (For Fedora)

Make sure you followed the extra note about editing /etc/uwsgi.ini and run:

确保您遵循有关编辑/etc/uwsgi.ini的附加说明并运行:

Verify the service is running with systemctl status uwsgi

验证服务是否以systemctl状态uwsgi运行

This is enough to get an app working, if you disabled SELinux (if you want to do it, edit /etc/selinux/config and reboot), but if you want to keep SELinux happy, you need to do the following:

如果您禁用了SELinux,这足以使应用程序正常运行(如果要执行此操作,请编辑/ etc / selinux / config并重新启动),但是如果您想让SELinux保持愉快,则需要执行以下操作:

setenforce 0
setenforce 0
chcon -R system_u:system_r:httpd_t:s0 /srv/myapp/appdata/static
chcon -R system_u:system_r:httpd_t:s0 /srv/myapp/appdata/static
setenforce 1
setenforce 1

We now need to install a SELinux policy (that I created for this project). If it doesn’t work, look into audit2allow.

现在,我们需要安装SELinux策略 (是我为此项目创建的)。 如果不起作用,请查看audit2allow

Hopefully, this is enough. In case it isn’t, please read SELinux documentation, and check audit logs.

希望这足够了。 如果不是这样,请阅读SELinux文档,并检查审核日志。

Also if you’re on Fedora, to make your website accessible from the outside Internet, you need to configure the built-in firewall accordingly — for ports 80/443, use:

另外,如果您使用的是Fedora,要使网站可以从外部Internet访问,则需要相应地配置内置防火墙-对于端口80/443,请使用:

firewall-cmd --add-service http
firewall-cmd --add-service http
firewall-cmd --add-service https
firewall-cmd --add-service https

对于Ubuntu (For Ubuntu)

Ubuntu does not ship the uWSGI Emperor service by default. However, you can easily create it. Copy the .service file from the uWSGI systemd documentation to /etc/systemd/system/emperor.uwsgi.service. Change the ExecStart line to:

默认情况下,Ubuntu不提供uWSGI Emperor服务。 但是,您可以轻松创建它。 将.service文件从uWSGI systemd文档复制到/etc/systemd/system/emperor.uwsgi.service 。 将ExecStart行更改为:

You can now reload systemd daemons and enable the services:

现在,您可以重新加载systemd守护程序并启用服务:

systemctl daemon-reload
systemctl daemon-reload
systemctl systemctl enable nginx emperor.uwsgi
enable  nginx emperor.uwsgi
systemctl start nginx emperor.uwsgi
systemctl start nginx emperor.uwsgi

Verify the service is running with systemctl status emperor.uwsgi

使用systemctl status emperor.uwsgi验证服务是否正在运行

测试-最终结果 (Testing — end result)

Your web service should now be running at http://localhost:8080/.

您的Web服务现在应该在http:// localhost:8080 /上运行

If you used the demo application, you should see something like this (complete with the favicon and image greeting):

如果您使用了演示应用程序,则应该看到类似以下内容(带有favicon和图像问候):

/images/nginx-uwsgi-demo.png

Hopefully, everything works. If it doesn’t, check nginx and uwsgi logs for details, and make sure you followed all instructions.

希望一切正常。 如果不是,请检查nginx和uwsgi日志以获取详细信息,并确保已遵循所有说明。



For easy linking, I set up some aliases: https://go.chriswarrick.com/pyweb and https://go.chriswarrick.com/uwsgi-tut (powered by a Django web application, deployed with nginx and uwsgi!)

为了便于链接,我设置了一些别名: https : //go.chriswarrick.com/pywebhttps://go.chriswarrick.com/uwsgi-tut (由Django Web应用程序提供支持,已与nginx和uwsgi一起部署!)

Update 2016-02-10 17:00 UTC: This guide uses nginx and uWSGI, because they are considered best practices by most people. nginx is a fast, modern web server, with uWSGI support built in (without resorting to reverse proxying). uWSGI is similarly aimed at speed. The Emperor mode of uWSGI is recommended for init system integration by the uWSGI team, and it’s especially useful for multi-app deployments. (This guide is opinionated.)

更新2016-02-10 17:00 UTC:本指南使用nginx和uWSGI,因为它们被大多数人视为最佳实践。 nginx是一种快速,现代化的Web服务器,内置了uWSGI支持(无需求助于反向代理)。 uWSGI同样针对速度。 uWSGI团队建议将uWSGI的Emperor模式用于init系统集成,这对于多应用程序部署特别有用。 (本指南是自以为是的。)

[1][1] This reflink gives you $10 in credit, which is enough to run a server for up to two months without paying a thing. I earn $15. 此reflink会给您$ 10的信用额度,足以使服务器运行两个月而无需支付任何费用。 我赚了15美元。
[2][2] If you’re in the EU (and thus have to pay VAT), or want DO to handle your backups, it will cost you a little more. 如果您在欧盟境内(因此必须支付增值税),或者想让DO处理您的备份,这将使您花费更多。
[3][3] Ubuntu 14.04 LTS does not use systemd — you’re on your own (upstart services exist, figure out how to use them yourself). Note that other software might be outdated as well — proceed with care, or just use something more modern. Ubuntu 14.04 LTS不使用systemd -您是自己一个人(存在新贵服务,请自己弄清楚如何使用它们)。 请注意,其他软件也可能已过时,请谨慎操作,或仅使用更现代的软件。

翻译自: https://www.pybloggers.com/2016/02/deploying-python-web-applications-with-nginx-and-uwsgi-emperor/

nginx和uwsgi部署

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值