flask项目部署到云服务器,并通过域名访问后端服务
本篇介绍一下如何把flask项目部署到腾讯云服务器,同时使用域名来访问后端服务
在之前一篇文章中写了一下如何在flask中配置https请求
1、把代码打包上传到服务器
上传至服务器前,需要修改一下启动ip
我的项目启动文件为run_main.py
from data_factory import create_app
app = create_app()
if __name__ == '__main__':
# app.run(debug=True, host='0.0.0.0', port=5000)
app.run(debug=True, host='10.xxx.xxx.xxx', port=5000, ssl_context=('7784675_bingshuang.top.pem','7784675_bingshuang.top.key'))
因为要部署到云服务器,所以host
需要改为云服务器地址
注意这里不要改为公网ip,要填写内网ip!!
然后把代码上传到服务器,如下
2、域名设置
给域名配置ip地址,这里需要写入服务器的公网ip
3、启动项目
在云服务器中启动flask项目,命令如下
# 首先进入项目目录,然后运行run_main.py
[root@VM-8-3-centos flaskProject]# cd /app/flaskProject/
[root@VM-8-3-centos flaskProject]# python3 run_main.py
/usr/local/lib/python3.6/site-packages/pymysql/_auth.py:8: CryptographyDeprecationWarning: Python 3.6 is no longer supported by the Python core team. Therefore, support for it is deprecated in cryptography and will be removed in a future release.
from cryptography.hazmat.backends import default_backend
* Tip: There are .env or .flaskenv files present. Do "pip install python-dotenv" to use them.
* Serving Flask app "data_factory" (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: on
* Running on https://11.1.2.3:5000/ (Press CTRL+C to quit)
* Restarting with stat
/usr/local/lib/python3.6/site-packages/pymysql/_auth.py:8: CryptographyDeprecationWarning: Python 3.6 is no longer supported by the Python core team. Therefore, support for it is deprecated in cryptography and will be removed in a future release.
from cryptography.hazmat.backends import default_backend
* Tip: There are .env or .flaskenv files present. Do "pip install python-dotenv" to use them.
* Debugger is active!
* Debugger PIN: 145-638-942