项目部署:flask+mod_wsgi+apache

2024-02-08 11:48
文章标签 mod 部署 项目 apache flask wsgi

本文主要是介绍项目部署:flask+mod_wsgi+apache,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

  • 安装apache
    root用户下安装的,安装完后,在以下目录中:
    • /usr/sbin/apache2 执行文件
    • /usr/lib/apache2 库文件
    • /etc/apache2 配置文件
    • /usr/share/man/man8/apache2.8.gz
apt-get install apache2
  • 安装mod-wsgi
    root用户下安装的
apt-get install libapache2-mod-wsgi #python2
apt-get install libapache2-mod-wsgi-py3 #python3

安装完后所在目录:
/usr/lib/apache2/modules/
在这里插入图片描述

  • 安装flask
    非root用户下安装的。
pip3 install flask --user
  • 创建flask程序
#test_hello.py
import os
from urllib.request import urlretrieve
import logging
from logging.handlers import RotatingFileHandler
from flask import Flask, request, jsonify
import requests
import json
import timedef generate_log(log_dir):if not os.path.exists(log_dir):os.makedirs(log_dir)now_time = time.strftime('%Y-%m-%d-%H-%M-%S')formatter_base = '%(asctime)s: %(levelname)s %(message)s'logging.basicConfig(level=logging.DEBUG)formatter = logging.Formatter(formatter_base)Rthandler = RotatingFileHandler(filename='%s' % os.path.join(log_dir, now_time + '.log'), \maxBytes=50 * 1024 * 1024, backupCount=1)Rthandler.setLevel(logging.DEBUG)Rthandler.setFormatter(formatter)logging.getLogger('').addHandler(Rthandler)app = Flask(__name__)
generate_log('/mnt/disk2/xxx/work/test/log/')
@app.route('/Test', methods=['GET', 'POST'])def test():try:strs = request.values.get('strs')except Exception as e:return jsonify({'result': 'ERR', 'message' : 'hello %s faild: %s' % (strs, e)})return jsonify({'result': 'OK', 'message' : 'hello %s ' % strs})
if __name__ == '__main__':app.run(host='0.0.0.0', port='7000')
  • 创建.wsgi配置文件
    在项目test目录下创建mod_wsgi.wsgi,因为test目录在非root用户下,需要用chmod 777 mod_wsgi.wsgi命令将mod_wsgi.wsgi文件改成可读可写模式,是root用户下,apache运行后能调用该文件。
#mod_wsgi.wsgi
#!/usr/bin/python3import sys
import os
import logging
import sitelogging.basicConfig(level=logging.DEBUG, stream=sys.stderr)
site.addsitedir('/home/xxx/.local/lib/python3.5/site-packages')
sys.path.insert(0, '/mnt/disk2/xxx/work/test')#logging.info(sys.path)#import torch
#logging.debug(torch.__version__)#def application(environ,start_response):
#    status='200 OK'
#    output=b'Hello wsgi!'
#    print(environ['wsgi.errors'], sys.path)
#    print (environ['wsgi.errors'], sys.prefix)
#    print (environ['wsgi.errors'], sys.executable)
#    response_headers=[('Content-type','text/plain'),
#                       ('Content-Length',str(len(output)))]
#    start_response(status,response_headers)
#    return[output]from test_hello import app as application
  • 创建apache2配置文件
    /etc/apache2/sites-available/目录下创建flask_app.conf配置文件,在该文件中同样需要对mod_wsgi.wsgi文件、项目test目录、日志test/log目录设置可读写权限,并且还需要用命令chmod 777 xxx修改可读写权限。
#flask_app.conf
WSGIPythonHome /home/xxx/.local/lib/python3.5/site-packages
ServerName 10.27.1.20
Listen 7000
<VirtualHost *:7000>ServerAdmin webmaster@localhostWSGIDaemonProcess test python-path=/mnt/disk2/xxx/work/test:/home/xxx/.local/lib/python3.5/site-packages python-home=/home/xxx/.local/lib/python3.5/site-packages display-name=%{GROUP}WSGIScriptAlias / /mnt/disk2/xxx/work/test/mod_wsgi.wsgi process-group=test application-group=%{GLOBAL}<Directory /home/xxx/.local/lib/python3.5/site-packages/>Require all granted</Directory><Directory /mnt/disk2/xxx/work/test/>WSGIScriptReloading OnRequire all granted<Files mod_wsgi.wsgi>Require all granted</Files></Directory><Directory /mnt/disk2/xxx/work/test/log/>Require all granted</Directory>LogLevel debugErrorLog /mnt/disk2/xxx/work/test/log/apache_error.logCustomLog /mnt/disk2/xxx/work/test/log/apache_access.log combined
</VirtualHost>
  • 启动站点配置文件
    可在任意目录下启动
a2ensite flask_app.conf # 激活站点
a2dissite flask_app.conf # 屏蔽站点
  • 启动mod_wsgi
    可在任意目录下启动
a2enmod wsgi # 查看是否启动
a2dismod wsgi # 禁用
  • 启动apache服务
apache2ctl start # 启动
apache2ctl restart # 重启
apache2ctl reload # 重新加载站点
apache2ctl stop # 关闭
  • 通过端口号查看服务是否运行
fuser -v -n tcp 7000 # 查看端口服务
fuser -k 7000/tcp #关闭端口
  • 测试
    浏览器中输入http://10.27.1.20:7000/Test?strs=apache-wsgi-flask,如果返回{"message":"hello apache-wsgi-flask ","result":"OK"}则成功。

参考资料
部署方式
Linux配置Apache2的经验总结

这篇关于项目部署:flask+mod_wsgi+apache的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/690935

相关文章

深度解析Java项目中包和包之间的联系

《深度解析Java项目中包和包之间的联系》文章浏览阅读850次,点赞13次,收藏8次。本文详细介绍了Java分层架构中的几个关键包:DTO、Controller、Service和Mapper。_jav... 目录前言一、各大包1.DTO1.1、DTO的核心用途1.2. DTO与实体类(Entity)的区别1

如何在Spring Boot项目中集成MQTT协议

《如何在SpringBoot项目中集成MQTT协议》本文介绍在SpringBoot中集成MQTT的步骤,包括安装Broker、添加EclipsePaho依赖、配置连接参数、实现消息发布订阅、测试接口... 目录1. 准备工作2. 引入依赖3. 配置MQTT连接4. 创建MQTT配置类5. 实现消息发布与订阅

springboot项目打jar制作成镜像并指定配置文件位置方式

《springboot项目打jar制作成镜像并指定配置文件位置方式》:本文主要介绍springboot项目打jar制作成镜像并指定配置文件位置方式,具有很好的参考价值,希望对大家有所帮助,如有错误... 目录一、上传jar到服务器二、编写dockerfile三、新建对应配置文件所存放的数据卷目录四、将配置文

怎么用idea创建一个SpringBoot项目

《怎么用idea创建一个SpringBoot项目》本文介绍了在IDEA中创建SpringBoot项目的步骤,包括环境准备(JDK1.8+、Maven3.2.5+)、使用SpringInitializr... 目录如何在idea中创建一个SpringBoot项目环境准备1.1打开IDEA,点击New新建一个项

springboot项目中整合高德地图的实践

《springboot项目中整合高德地图的实践》:本文主要介绍springboot项目中整合高德地图的实践,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一:高德开放平台的使用二:创建数据库(我是用的是mysql)三:Springboot所需的依赖(根据你的需求再

一文详解如何在idea中快速搭建一个Spring Boot项目

《一文详解如何在idea中快速搭建一个SpringBoot项目》IntelliJIDEA作为Java开发者的‌首选IDE‌,深度集成SpringBoot支持,可一键生成项目骨架、智能配置依赖,这篇文... 目录前言1、创建项目名称2、勾选需要的依赖3、在setting中检查maven4、编写数据源5、开启热

SpringBoot项目配置logback-spring.xml屏蔽特定路径的日志

《SpringBoot项目配置logback-spring.xml屏蔽特定路径的日志》在SpringBoot项目中,使用logback-spring.xml配置屏蔽特定路径的日志有两种常用方式,文中的... 目录方案一:基础配置(直接关闭目标路径日志)方案二:结合 Spring Profile 按环境屏蔽关

MySQL版本问题导致项目无法启动问题的解决方案

《MySQL版本问题导致项目无法启动问题的解决方案》本文记录了一次因MySQL版本不一致导致项目启动失败的经历,详细解析了连接错误的原因,并提供了两种解决方案:调整连接字符串禁用SSL或统一MySQL... 目录本地项目启动报错报错原因:解决方案第一个:第二种:容器启动mysql的坑两种修改时区的方法:本地

SpringBoot整合Apache Flink的详细指南

《SpringBoot整合ApacheFlink的详细指南》这篇文章主要为大家详细介绍了SpringBoot整合ApacheFlink的详细过程,涵盖环境准备,依赖配置,代码实现及运行步骤,感兴趣的... 目录1. 背景与目标2. 环境准备2.1 开发工具2.2 技术版本3. 创建 Spring Boot

Linux中修改Apache HTTP Server(httpd)默认端口的完整指南

《Linux中修改ApacheHTTPServer(httpd)默认端口的完整指南》ApacheHTTPServer(简称httpd)是Linux系统中最常用的Web服务器之一,本文将详细介绍如何... 目录一、修改 httpd 默认端口的步骤1. 查找 httpd 配置文件路径2. 编辑配置文件3. 保存