当前位置: 移动技术网 > IT编程>脚本编程>Python > Python WEB应用部署的实现方法

Python WEB应用部署的实现方法

2019年06月12日  | 移动技术网IT编程  | 我要评论

流行歌曲dj,聚美优品官方网站,核燃料研究获突破

本文介绍了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

$ yum install httpd-devel
$ rpm -ql httpd-devel

安装mod__wsgi

$ yum install mod_wsgi

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

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

loadmodule wsgi_module modules/mod_wsgi.so

重启apache来启用配置

$ sudo service httpd restart 

测试mod_wsgi

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

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路径映射:

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

测试 curl http://www.lhsxpumps.com/_localhost/test

使用python虚拟环境

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

配置app.wsgi

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配置文件

<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

$ pip install uwsgi

创建 server.py

from flask import flask

app = flask(__name__)


@app.route('/')
def hello_world():
 return 'hello world!'


if __name__ == '__main__':
 app.run()

创建 uwsgi 配置文件 uwsgi.ini

[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

启动

uwsgi --ini uwsgi.ini    # 启动
uwsgi --reload uwsgi.pid   # 重启
uwsgi --stop uwsgi.pid   # 关闭

配置nginx

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

socket=127.0.0.1:8080

修改nginx配置文件nginx.conf

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虚拟环境

[uwsgi]
...
virtualenv=/home/smi1e/virtualenv

部署多个应用

参考

使用nginx和uwsgi来运行python应用

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网