阿里云服务器 debian9 apache2 部署flask+wsgi 过程总结

简介:

由于阿里云镜像自带的源更新比较缓慢,最新的python3版本还是3.5,而我的代码使用的库需要python3.6+,因此需要先更新debian源:

vim /etc/apt/sources.list
AI 代码解读

在其中添加:

deb http://mirrors.163.com/debian/ testing main
AI 代码解读

执行:

apt-get update
apt-get install python3.6 python3.6-dev python3.6-distutils
apt-get install python-pip
AI 代码解读

安装wsgi:

sudo apt-get install libapache2-mod-wsgi-py3
AI 代码解读

在/etc/apache2/sites-available中新建flask.conf文件,在其中输入:

WSGIPythonPath /var/www/html/flask

<VirtualHost *:80>
    # The ServerName directive sets the request scheme, hostname and port that
    # the server uses to identify itself. This is used when creating
    # redirection URLs. In the context of virtual hosts, the ServerName
    # specifies what hostname must appear in the request's Host: header to
    # match this virtual host. For the default virtual host (this file) this
    # value is not decisive as it is used as a last resort host regardless.
    # However, you must set it for any further virtual host explicitly.

    ServerName flask.yunwei123.tech
    # ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html/flask

    # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
    # error, crit, alert, emerg.
    # It is also possible to configure the loglevel for particular
    # modules, e.g.
    #LogLevel info ssl:warn

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
    
    WSGIScriptAlias / /var/www/html/flask/app.wsgi

     <Directory /var/www/html/flask>
    Require all granted  
    <Files app.wsgi>                                                                                                                                                      
        Allow from all
    </Files>
        </Directory>
    
    # For most configuration files from conf-available/, which are
    # enabled or disabled at a global level, it is possible to
    # include a line for only one particular virtual host. For example the
    # following line enables the CGI configuration for this host only
    # after it has been globally disabled with "a2disconf".
    #Include conf-available/serve-cgi-bin.conf
</VirtualHost>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
AI 代码解读

保存。

在网站根目录(这里是)新建app.wsgi文件,输入:

from server import app as application
AI 代码解读

再在同一个目录下新建server.py, 输入:

from flask import Flask
import platform 
print(platform.python_version())
app = Flask(__name__)
 
@app.route('/')
def hello_world():
    return platform.python_version()
 
if __name__ == '__main__':
    app.run(host='0.0.0.0')
AI 代码解读

打开网页就可以看到啦。


这里搭建的网页:

使用 flask + pyechart 搭建的简单新冠肺炎疫情数据可视化交互分析平台,包含疫情数据获取、态势感知、预测分析、舆情监测等任务:http://flask.yunwei123.tech/进行查看

包含完整代码和实现的github地址:
https://github.com/yunwei37/COVID-19-NLP-vis

相关实践学习
快速体验PolarDB开源数据库
本实验环境已内置PostgreSQL数据库以及PolarDB开源数据库:PolarDB PostgreSQL版和PolarDB分布式版,支持一键拉起使用,方便各位开发者学习使用。
7天玩转云服务器
云服务器ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,可降低 IT 成本,提升运维效率。本课程手把手带你了解ECS、掌握基本操作、动手实操快照管理、镜像管理等。了解产品详情:&nbsp;https://www.aliyun.com/product/ecs
目录
打赏
0
0
0
0
3
分享
相关文章
将 Python UDF 部署到 Apache IoTDB 的详细步骤与注意事项
【10月更文挑战第21天】将 Python UDF 部署到 Apache IoTDB 中需要一系列的步骤和注意事项。通过仔细的准备、正确的部署和测试,你可以成功地将自定义的 Python UDF 应用到 Apache IoTDB 中,为数据处理和分析提供更灵活和强大的支持。在实际操作过程中,要根据具体情况进行调整和优化,以确保实现最佳的效果。还可以结合具体的代码示例和实际部署经验,进一步深入了解和掌握这一过程。
61 2
win7系统搭建PHP+Mysql+Apache环境+部署ecshop项目
这篇文章介绍了如何在Windows 7系统上搭建PHP、MySQL和Apache环境,并部署ECShop项目,包括安装配置步骤、解决常见问题以及使用XAMPP集成环境的替代方案。
94 1
win7系统搭建PHP+Mysql+Apache环境+部署ecshop项目
kali向Apache上部署网页
kali向Apache上部署网页
177 5
【Azure App Service】本地Git部署Python Flask应用上云(Azure App Service For Linux)关键错误
【Azure App Service】本地Git部署Python Flask应用上云(Azure App Service For Linux)关键错误
如何在 Ubuntu VPS 上部署 Flask 应用程序
如何在 Ubuntu VPS 上部署 Flask 应用程序
75 1
【Azure 应用服务】Azure App Service (Windows) 使用Flask框架部署Python应用,如何在代码中访问静态文件呢?如何设置文件路径?是相对路径还是绝对路径呢?
【Azure 应用服务】Azure App Service (Windows) 使用Flask框架部署Python应用,如何在代码中访问静态文件呢?如何设置文件路径?是相对路径还是绝对路径呢?
107 0
如何在 Ubuntu 14.04 上使用 Passenger 和 Apache 部署 Rails 应用
如何在 Ubuntu 14.04 上使用 Passenger 和 Apache 部署 Rails 应用
42 0
"Linux系统实战:从零开始部署Apache+PHP Web项目,轻松搭建您的在线应用"
【8月更文挑战第9天】Linux作为服务器操作系统,凭借其稳定性和安全性成为部署Web项目的优选平台。本文以Apache Web服务器和PHP项目为例,介绍部署流程。首先,通过包管理器安装Apache与PHP;接着创建项目目录,并上传项目文件至该目录;根据需要配置Apache虚拟主机;最后重启Apache服务并测试项目。确保防火墙允许HTTP流量,正确配置数据库连接,并定期更新系统以维持安全。随着项目复杂度提升,进一步学习高级配置将变得必要。
517 0
安装和配置Apache Tomcat是部署Java Web应用程序的常见任务
安装和配置Apache Tomcat是部署Java Web应用程序的常见任务
217 7
Linux _ apache服务器部署 不同域名—访问不同网站(多网站)
Linux _ apache服务器部署 不同域名—访问不同网站(多网站)
300 1

热门文章

最新文章