脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - Python WEB应用部署的实现方法

Python WEB应用部署的实现方法

2021-05-11 00:10Smi1e Python

这篇文章主要介绍了Python WEB应用部署的实现方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

本文介绍了python web应用部署的实现方法,分享给大家,具体如下:

Python WEB应用部署的实现方法 Python WEB应用部署的实现方法

使用apache模块mod_wsgi运行python wsgi应用

flask应用是基于wsgi规范的,所以它可以运行在任何一个支持wsgi协议的web应用服务器中,最常用的就是 apache+mod_wsgi 的方式

apache主配置文件是/etc/httpd/conf/httpd.conf

其他配置文件存储在/etc/httpd/conf.d/目录

安装mod_wsgi

安装httpd-devel

?
1
2
$ yum install httpd-devel
$ rpm -ql httpd-devel

安装mod__wsgi

?
1
$ yum install mod_wsgi

安装完成之后, mod_wsgi.so 会在apache的modules目录中

httpd.conf 文件中添加以下内容

?
1
loadmodule wsgi_module modules/mod_wsgi.so

重启apache来启用配置

?
1
$ sudo service httpd restart

测试mod_wsgi

在apache的documentroot根目录下创建一个文件 test.wsgi

?
1
2
3
4
5
6
7
8
9
def application(environ, start_response):
 status = '200 ok'
 output = 'hello world!'
 
 response_headers = [('content-type', 'text/plain'),
      ('content-length', str(len(output)))]
 start_response(status, response_headers)
 
 return [output]

这里的函数 application 即为wsgi应用对象,它返回的值就是该应用收到请求后的响应。

然后,再打开apache的配置文件httpd.conf,在其最后加上url路径映射:

?
1
wsgiscriptalias /test /var/www/html/test.wsgi

测试 curl http://localhost/test

Python WEB应用部署的实现方法

使用python虚拟环境

virtualenv 是一个创建隔绝的python环境的工具。virtualenv创建一个包含所有必要的可执行文件以及 pip 库的文件夹,用来使用python工程所需的包。

配置app.wsgi

?
1
2
3
4
5
6
7
8
9
activate_this = '/var/www/html/py3env/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))
 
from flask import flask
application = flask(__name__)
 
import sys
sys.path.insert(0, '/var/www/flask_test')
from app import app as application

我们的虚拟环境在目录 /var/www/html 下,你可以在其 /bin 子目录中找到启用脚本 activate_this.py 。在wsgi应用的一开始执行它即可。

apache配置文件

?
1
2
3
4
5
6
7
<virtualhost *:80>
 servername example.com
 wsgiscriptalias / /var/www/html/app.wsgi
 <directory /var/www/html>
  require all granted
 </directory>
</virtualhost>!

参考

在apache中运行python wsgi应用

使用nginx+uwsgi运行python wsgi应用

uwsgi是一个web应用服务器,它具有应用服务器,代理,进程管理及应用监控等功能。虽然uwsgi本身就可以直接用来当web服务器,但一般建议将其作为应用服务器配合nginx一起使用,这样可以更好的发挥nginx在web端的强大功能。

安装uwsgi

?
1
$ pip install uwsgi

创建 server.py

?
1
2
3
4
5
6
7
8
9
10
11
12
from flask import flask
 
app = flask(__name__)
 
 
@app.route('/')
def hello_world():
 return 'hello world!'
 
 
if __name__ == '__main__':
 app.run()

创建 uwsgi 配置文件 uwsgi.ini

?
1
2
3
4
5
6
7
8
9
10
11
12
13
[uwsgi]
http=0.0.0.0:8080 #指定项目执行的端口号
chdir=/var/www/html/# 项目目录
wsgi-file=/var/www/html/server.py # 项目启动文件目录
callable=app #指定应用对象,wsgi标准是"application"
master=true #主进程(监控其他进程状态,如果有进程死了,则重启)
touch-reload=/var/www/html/ #监听的文件路径,当要监听的文件路径下的文件发生变化的时候自动重新加载服务器。
daemonize=uwsgi.log #日志文件
stats = 127.0.0.1:9090 #在指定的地址上,开启状态服务
vacuum = true # 当服务器退出的时候自动清理环境,
# 多进程&多线程
processes = 6
threads = 2

启动

?
1
2
3
uwsgi --ini uwsgi.ini    # 启动
uwsgi --reload uwsgi.pid   # 重启
uwsgi --stop uwsgi.pid   # 关闭

Python WEB应用部署的实现方法

配置nginx

将uwsgi的http端口监听改为socket端口监听

?
1
socket=127.0.0.1:8080

修改nginx配置文件nginx.conf

?
1
2
3
4
5
6
7
8
9
10
11
12
server {
  listen  80;
  server_name localhost 192.168.1.5;
  #root   /usr/share/nginx/html;
 
  # load configuration files for the default server block.
  include /etc/nginx/default.d/*.conf;
 
  location / {
    include uwsgi_params;
    uwsgi_pass 127.0.0.1:8080;
  }

nginx会将收到的所有请求都转发到 127.0.0.1:8080 端口上,即uwsgi服务器上。

这里有一个坑,由于centos7 selinux导致的权限问题,nginx无法将请求转发到uwsgi,我直接把它关掉了。

vi /etc/selinux/config

selinux=enforcing 改成 selinux=disabled

重启nginx测试。

Python WEB应用部署的实现方法

使用python虚拟环境

?
1
2
3
[uwsgi]
...
virtualenv=/home/smi1e/virtualenv

部署多个应用

Python WEB应用部署的实现方法

参考

使用nginx和uwsgi来运行python应用

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://www.smi1e.top/python-web应用部署/

延伸 · 阅读

精彩推荐