项目部署: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

相关文章

MySQL 主从复制部署及验证(示例详解)

《MySQL主从复制部署及验证(示例详解)》本文介绍MySQL主从复制部署步骤及学校管理数据库创建脚本,包含表结构设计、示例数据插入和查询语句,用于验证主从同步功能,感兴趣的朋友一起看看吧... 目录mysql 主从复制部署指南部署步骤1.环境准备2. 主服务器配置3. 创建复制用户4. 获取主服务器状态5

IntelliJ IDEA2025创建SpringBoot项目的实现步骤

《IntelliJIDEA2025创建SpringBoot项目的实现步骤》本文主要介绍了IntelliJIDEA2025创建SpringBoot项目的实现步骤,文中通过示例代码介绍的非常详细,对大家... 目录一、创建 Spring Boot 项目1. 新建项目2. 基础配置3. 选择依赖4. 生成项目5.

golang程序打包成脚本部署到Linux系统方式

《golang程序打包成脚本部署到Linux系统方式》Golang程序通过本地编译(设置GOOS为linux生成无后缀二进制文件),上传至Linux服务器后赋权执行,使用nohup命令实现后台运行,完... 目录本地编译golang程序上传Golang二进制文件到linux服务器总结本地编译Golang程序

如何在Ubuntu 24.04上部署Zabbix 7.0对服务器进行监控

《如何在Ubuntu24.04上部署Zabbix7.0对服务器进行监控》在Ubuntu24.04上部署Zabbix7.0监控阿里云ECS服务器,需配置MariaDB数据库、开放10050/1005... 目录软硬件信息部署步骤步骤 1:安装并配置mariadb步骤 2:安装Zabbix 7.0 Server

使用Docker构建Python Flask程序的详细教程

《使用Docker构建PythonFlask程序的详细教程》在当今的软件开发领域,容器化技术正变得越来越流行,而Docker无疑是其中的佼佼者,本文我们就来聊聊如何使用Docker构建一个简单的Py... 目录引言一、准备工作二、创建 Flask 应用程序三、创建 dockerfile四、构建 Docker

深度解析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所需的依赖(根据你的需求再