阿里雲服務器 debian9 apache2 部署flask+wsgi 過程總結

由於阿里雲鏡像自帶的源更新比較緩慢,最新的python3版本還是3.5,而我的代碼使用的庫需要python3.6+,因此需要先更新debian源:

vim /etc/apt/sources.list

在其中添加:

deb http://mirrors.163.com/debian/ testing main

執行:

apt-get update
apt-get install python3.6 python3.6-dev python3.6-distutils
apt-get install python-pip

安裝wsgi:

sudo apt-get install libapache2-mod-wsgi-py3

在/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

保存。

在網站根目錄(這裏是)新建app.wsgi文件,輸入:

from server import app as application

再在同一個目錄下新建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')

打開網頁就可以看到啦。


這裏搭建的網頁:

使用 flask + pyechart 搭建的簡單新冠肺炎疫情數據可視化交互分析平臺,包含疫情數據獲取、態勢感知、預測分析、輿情監測等任務:http://flask.yunwei123.tech/進行查看

包含完整代碼和實現的github地址:
https://github.com/yunwei37/COVID-19-NLP-vis

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章