介绍
当你第一次开始开发PythonWeb应用程序时,特别是如果你来自不同的语言或在桌面(GUI)应用程序中有背景时,你可能会在网上获得新创作的过程中有点迷路。
在这篇DigitalOcean文章中,根据我们之前的文章(https://www.digitalocean.com/community/articles/how-to-use-the-pyramid-framework-to-build-your-python-web-app-on-ubuntu)和使用不同的Web服务器部署Python Web应用程序,我们将看到包装Pyramid WSGI Web应用程序,创建其依赖的列表,并部署(即在线发布)。
注意: 本文的示例,虽然提供一个使用例子,也遵循在开始文章中使用的命名惯例. 如果您有关于使用Pyramid开始Web应用程序开发的问题,或者只是想熟悉以前使用的名称,您可以查看文章(https://www.digitalocean.com/community/articles/how-to-use-the-pyramid-framework-to-build-your-python-web-app-on-ubuntu)。
词汇
1. Pyramid in Brief
- Web 应用程序部署 2. WSGI 3. 如何使用 Nginx 作为反向代理
2. Deploying Pyramid Based WSGI Web-Applications
- 更新系统 2. 设置 Python、pip 和 virtualenv 3. 准备一个简单的应用程序与
应用程序
对象暴露 4. 使用.ini 设置暴露一个模糊的应用程序对象 5. 使用 pip 处理应用程序依赖 6. 创建应用程序依赖的列表 7. 从应用程序依赖的列表下载
3. Setting Up Python WSGI Web Application Servers
- 使用 CherryPy Web 服务器提供金字塔应用程序 2. 运行和管理应用程序服务器
4. Setting Up Nginx
- 安装 Nginx 2. 配置 Nginx
《金字塔》简介
与一些较重的对手相比,Pyramid是轻量级的Python网络应用框架之一,但是,与Flask或其他微型
的框架不同,Pyramid仍然带有许多功能和功能。
作为一个非常好的项目,Pyramid非常受欢迎,有几种不同的方法(Web服务器和方法)来部署基于它的Web应用程序。
Web 应用部署
对于所有 Python WSGI Web 应用程序,部署包括准备一个 WSGI 模块,其中包含对您的 application 对象的引用,然后被 Web 服务器用作输入点来传递请求。
名称:WSGI
WSGI,简而言之,是网络服务器和应用程序本身之间的接口,它存在于确保各种服务器和应用程序(框架)之间相互工作的标准化方式,在必要时允许互换性(例如从开发到生产环境的切换),这是当今必需的。
注: 如果您有兴趣了解更多关于 WSGI 和 Python 网页服务器的信息,请参阅我们的文章: 对比 Python 基于 Web 应用程序的网页服务器。
如何使用 Nginx 作为反向代理
Nginx 是一个非常高性能的 Web 服务器 / (反向) - 代理服务器. 它因其轻量,相对易于使用和易于扩展(通过插件 / 插件)而获得了普及。 由于其架构,它能够处理 a lot 的请求(几乎无限),这 - 取决于您的应用程序或网站的负载 - 可能很难使用其他一些较旧的替代方案来解决。
记住:处理
连接在技术上意味着不放弃它们,并且能够用 something 服务它们,您仍然需要您的应用程序和数据库正常运作,以便 Nginx 服务客户端的响应而不是错误消息。
Python WSGI Web 应用服务器
Python Web 应用服务器通常是基于 C 的独立解决方案或完全(或部分)基于 Python 的解决方案(即纯 Python)。
它们通过接受包含 - 如前所述 - 可调用到 contain 的 Web 应用程序的 Python 模块而运作,并在网络上提供服务。
虽然其中一些是可以直接使用的高性能服务器,但出于上述原因(例如较高的性能)建议在前面使用 Nginx。
一些受欢迎的 Python WSGI 网页服务器是:
- CherryPy
- Gunicorn
- uWSGI
- waitress
部署基于金字塔的 WSGI Web 应用程序
由于Python的WSGI中间件规格,对于所有设置在协议上运行的应用程序,在Web应用程序服务器方面有很多选择。
Recap:更新系统
为了有一个稳定的部署服务器,保持事物的最新和良好的维护至关重要。
为了确保我们拥有最新的默认应用程序版本,我们需要更新我们的系统。
对于基于 Debian 的系统(即 Ubuntu、Debian),运行以下操作:
1aptitude update
2aptitude -y upgrade
对于基于 RHEL 的系统(即 CentOS),运行以下操作:
1yum -y update
提示: 在 CentOS 中,升级
意味着升级
操作系统,但是更新
更新了应用程序,在 Debian/Ubuntu 中,更新
更新了应用程序源列表,而升级
则将其升级到更新的版本。
Recap:设置Python、Pip和virtualenv
翻译: CentOS / RHEL 用户说明: 翻译: CentOS / RHEL ,默认为作为非常精细的服务器而来. 它的"工具"可能为您的需要而过时,不是** ** 在那里运行您的应用程序,而是为服务器的系统工具提供动力(例如YUM). 翻译: 翻译: 为了准备您的 CentOS 系统, Python 需要设置( 即从源头编译) , pip / virtualenv 需要使用该解释器安装 。 翻译: 翻译: 欲了解如何在CentOS 6.4和5.8 上设置Python 2.7.6和3.3.3,并附有pip和vitualenv,请参见:[如何在CentOS上设置Python 2.7.6和3.3.3] (https://www.digitalocean.com/community/articles/how-to-set-up-python-2-7-6-and-3-3-3-on-centos-6-4).
在Ubuntu和Debian上,你可以使用的Python翻译器的最新版本是默认的,它只留给了我们一个有限的额外包来安装:
python-dev - 开发工具* pip* - 管理包** virtualenv* - 创建孤立的虚拟环境
此分類上一篇: python-dev
python-dev 是一个操作系统级包,包含用于构建 Python 模块的扩展开发工具。
运行以下命令以使用 aptitude 安装 python-dev:
1aptitude install python-dev
《PIP》:
pip 是一个包管理器,可以帮助我们安装我们需要的应用程序包。
运行以下命令来安装 pip:
1curl https://bitbucket.org/pypa/setuptools/raw/bootstrap/ez_setup.py | python -
2curl https://raw.github.com/pypa/pip/master/contrib/get-pip.py | python -
3export PATH="/usr/local/bin:$PATH"
您可能需要 sudo 特权。
《虚拟》:
最好把一个Python应用程序包含在其自身的环境中,以及其所有依赖性。一个环境可以最好地描述(简单地说)作为一个孤立的位置(一个目录),其中一切都在。
运行以下操作以使用 pip 安装 virtualenv:
1sudo pip install virtualenv
准备一个简单的应用程序与应用
对象暴露
注意: 本节专注于从上一个教程中在线获取我们的单页应用程序样本。
目前,我們有一個單頁應用程式(即「application.py」),用Pyramid的開發伺服器在端口 8080上提供「Hello world!」服務。
我們目前的工作目錄位於 ~/pyramid_sites/hello_world
我們目前的 pyramid 應用例子 (application.py
) 看起來如下:
1from wsgiref.simple_server import make_server
2from pyramid.config import Configurator
3from pyramid.response import Response
4
5def hello_world(request):
6 return Response('<h1>Hello world!</h1>')
7
8if __name__ == '__main__':
9 config = Configurator()
10 config.add_view(hello_world)
11 app = config.make_wsgi_app()
12 server = make_server('0.0.0.0', 8080, app)
13 server.serve_forever()
为了将此示例转化为包含 WSGI 调用模块,我们可以做出以下修改:
1from wsgiref.simple_server import make_server
2from pyramid.config import Configurator
3from pyramid.response import Response
4
5def hello_world(request):
6 return Response('<h1>Hello world!</h1>')
7
8# Move the application object here.
9# Create a configurator to make *wsgi app(lication)*
10config = Configurator()
11config.add_view(hello_world)
12
13# The "app" object to be exposed
14app = config.make_wsgi_app()
15
16# If run directly, still construct a [development]
17# server process and start serving on port #8080.
18if __name__ == '__main__':
19 server = make_server('0.0.0.0', 8080, app)
20 server.serve_forever()
更改后,按 CTRL + X 并用 Y 确认保存和退出。
从现在开始,我们可以将此文件作为一个模块,该应用程序可调用到任何Python WSGI网络服务器。
使用.ini 设置暴露一个模糊的应用程序对象
或者,如果您使用Pyramid’s scaffolding创建了一个更复杂的应用程序,您可以创建一个)配置。
注意: 此文件必须位于您的 root 应用程序文件夹内,并与 requirements.txt
或您的.ini 文件一起。
注意: 您可以从技术上选择任何名称(包括 application.py )为此文件. 然而,‘wsgi.py’可能是 - 而且通常 - 比另一个更受欢迎。
若要使用 nano 文本编辑器创建 wsgi.py
文件,请执行以下操作:
1nano wsgi.py
复制并粘贴下面的内容:
1from pyramid.config import Configurator
2from pyramid.response import Response
3from pyramid.paster import get_app
4
5def hello(request):
6 return Response('Hello!')
7
8config = Configurator()
9config.add_route('hello', '/')
10config.add_view(home, route_name='hello')
11
12app = config.make_wsgi_app()
13
14# Or from an .ini file:
15# app = get_app('config.ini', 'application_name')
使用 pip 处理应用程序依赖
由于您很可能已经在本地机器上启动了开发过程,因此在部署应用程序时,您需要确保其所有依赖性都安装在您的虚拟环境中。
创建应用依赖的列表
使用一个命令,它能够生成您已安装的所有包(或依赖)(在您的激活环境中,如果没有,在您的系统上全球),再一次用一个命令,它允许您下载和安装它们。
注意: 本节包含在本地开发机器上或您想要生成应用程序依赖性列表的任何地方执行的信息,该文件应放在您的应用程序目录中并上传到您的服务器。
使用pip
创建安装包列表:
1pip freeze > requirements.txt
此命令将创建一个名为requirements.txt
的文件,其中包含所有安装的包的列表. 如果您在 virtualenv 中运行它,则该列表将包括在环境中安装的包 仅 。
从应用程序依赖的列表中下载
使用Pip从列表中安装包:
注:本节包含在您的生产(即部署)机器/环境上执行的信息。
1pip install -r requirements.txt
此命令将下载并安装列出的所有包. 如果您在启用环境中工作,文件将被下载到那里.否则,它们将被全球安装 - 这是 not 推荐的方式,原因在上一节解释。
创建 Python WSGI Web 应用服务器
在设置我们的Pyramid应用程序以曝光其应用程序对象后,我们可以开始实际部署过程,下载和安装我们所选择的Web应用程序服务器。
在本文中,我们将专注于使用 CherryPy ,因为其功能和简单的结合。
注: 这里的说明很简短。 欲多学习,请查看我们如何撰写关于pip和vitualenv的文章Common Python工具:vitualenv和pip. 如果您正在使用基于 CentOS 的服务器, 您可能也想要看到[如何在 CentOS (https://www.digitalocean.com/community/articles/how-to-set-up-python-2-7-6-and-3-3-3-on-centos-6-4) 上设置 Python 2.7. 6 和 3. 3. 3 。 Name.
使用 CherryPy Web 服务器服务金字塔应用程序
CherryPy 的纯 Python Web 服务器是一个紧凑的解决方案,配备了类似的框架,该项目将其定义为高速,生产准备,线程聚合,通用 HTTP 服务器
,它是一个可用于服务任何 Python WSGI Web 应用程序的模块化组件。
按照我们的第一个金字塔教程,我们可以继续在虚拟环境中工作(~/pyramid_sites/env)并在那里安装CherryPy。
1# Install CherryPy Framework and HTTP Web-Server
2pip install cherrypy
在安装应用程序包时,为了服务您的Pyramid应用程序,您需要创建一个server.py
文件,以便您的Phython翻译器运行。
要创建一个 server.py 文件 ,请执行以下操作:
1nano server.py
复制并粘贴下面的内容以使用 CherryPy 的 HTTP 服务器创建样本服务器应用程序:
1# Import your application as:
2# from wsgi import application
3# Example:
4
5# If you are using the wsgi.py (standard Pyramid)
6# from wsgi import app
7
8# If using application.py (single page example):
9from application import app
10
11# Import CherryPy
12import cherrypy
13
14if __name__ == '__main__':
15
16 # Mount the application (or *app*)
17 cherrypy.tree.graft(app, "/")
18
19 # Unsubscribe the default server
20 cherrypy.server.unsubscribe()
21
22 # Instantiate a new server object
23 server = cherrypy._cpserver.Server()
24
25 # Configure the server object
26 server.socket_host = "0.0.0.0"
27 server.socket_port = 8080
28 server.thread_pool = 30
29
30 # For SSL Support
31 # server.ssl_module = 'pyopenssl'
32 # server.ssl_certificate = 'ssl/certificate.crt'
33 # server.ssl_private_key = 'ssl/private.key'
34 # server.ssl_certificate_chain = 'ssl/bundle.crt'
35
36 # Subscribe this server
37 server.subscribe()
38
39 # Example for a 2nd server (same steps as above):
40 # Remember to use a different port
41
42 # server2 = cherrypy._cpserver.Server()
43
44 # server2.socket_host = "0.0.0.0"
45 # server2.socket_port = 8080
46 # server2.thread_pool = 30
47 # server2.subscribe()
48
49 # Start the server engine (Option 1 *and* 2)
50
51 cherrypy.engine.start()
52 cherrypy.engine.block()
保存并再次退出,按 CTRL + X 并用 Y 确认。
运行和管理应用程序服务器
要開始服務您的應用程式,您只需使用 Python 安裝程式執行「server.py」。
运行以下操作以按配置启动服务器:
1python server.py
这将使服务器在前面运行. 如果您想阻止它,请按 CTRL+C。
要在背景中运行服务器,请使用以下方法:
1python server.py &
当您在背景中运行应用程序时,您需要使用一个流程管理器(例如 htop)来杀死(或停止)它。
设置 Nginx
安装 Nginx
请参阅 CentOS 的指示(https://www.digitalocean.com/community/articles/how-to-install-nginx-on-centos-6-with-yum)。
运行以下命令以使用 aptitude 安装 Nginx:
1sudo aptitude install nginx
要运行 Nginx,请使用以下方法:
1sudo service nginx start
要停止 Nginx,请使用以下方法:
1sudo service nginx stop
若要重新启动 Nginx,请使用以下方法:
1# After each time you reconfigure Nginx, a restart
2# or reload is needed for the new settings to come
3# into effect.
4sudo service nginx restart
注意: 要了解有关 Ubuntu 上的 Nginx 的更多信息,请参阅我们的文章: 如何在 Ubuntu 12.0 上安装 Nginx 。
配置 Nginx
注意: 以下是关于使用 Nginx 作为反向代理的简短教程。 有关 Nginx 的更多信息,请参阅 如何在 VPS 上配置 Nginx Web 服务器。
选择和设置一个 Web 服务器来运行我们的应用程序后,我们可以继续与 Nginx 做同样的事情,并准备与后端服务器(s)进行交谈(运行 WSGI 应用)。
要做到这一点,我们需要修改 Nginx 的配置文件: nginx.conf
运行以下命令,打开 nginx.conf 并使用 nano 文本编辑器编辑它:
1sudo nano /etc/nginx/nginx.conf
您可以用以下示例配置来替换文件,以便 Nginx 作为反向代理程序工作,与您的应用程序交谈。
复制并粘贴下面的示例配置:
1worker_processes 1;
2
3events {
4
5 worker_connections 1024;
6
7}
8
9http {
10
11 sendfile on;
12
13 gzip on;
14 gzip_http_version 1.0;
15 gzip_proxied any;
16 gzip_min_length 500;
17 gzip_disable "MSIE [1-6]\.";
18 gzip_types text/plain text/xml text/css
19 text/comma-separated-values
20 text/javascript
21 application/x-javascript
22 application/atom+xml;
23
24 # Configuration containing list of application servers
25 upstream app_servers {
26
27 server 127.0.0.1:8080;
28 # server 127.0.0.1:8081;
29 # ..
30 # .
31
32 }
33
34 # Configuration for Nginx
35 server {
36
37 # Running port
38 listen 80;
39
40 # Settings to serve static files
41 location ^~ /static/ {
42
43 # Example:
44 # root /full/path/to/application/static/file/dir;
45 root /app/static/;
46
47 }
48
49 # Serve a static file (ex. favico)
50 # outside /static directory
51 location = /favico.ico {
52
53 root /app/favico.ico;
54
55 }
56
57 # Proxy connections to the application servers
58 # app_servers
59 location / {
60
61 proxy_pass http://app_servers;
62 proxy_redirect off;
63 proxy_set_header Host $host;
64 proxy_set_header X-Real-IP $remote_addr;
65 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
66 proxy_set_header X-Forwarded-Host $server_name;
67
68 }
69 }
70}
当您完成更改配置时,请按 CTRL + X 并用 Y 确认保存和退出,您需要重新启动 Nginx 以便更改生效。
运行以下操作来重新启动 Nginx:
1sudo service nginx stop
2sudo service nginx start
在将应用程序服务器连接到 Nginx 后,您现在可以通过使用您喜爱的浏览器访问您的 Droplet 的 IP 地址。
1http://[your droplet's IP adde.]/
2
3# Hello world!
阅读更多
如果您想了解更多关于 Python Web 应用程序部署的信息,建议您查看以下有关该主题的文章,以获得更全面的了解:
- [如何部署 Python WSGI 应用程序,使用 uWSGI 网络服务器与 Nginx] (https://www.digitalocean.com/community/articles/how-to-deploy-python-wsgi-applications-using-uwsgi-web-server-with-nginx)
- [如何使用 Gunicorn HTTP 服务器在 Nginx 后面部署 Python WSGI Apps] (https://www.digitalocean.com/community/articles/how-to-deploy-python-wsgi-apps-using-gunicorn-http-server-behind-nginx)
- Python基于Web应用的网络服务器比较.