Flask—10-项目部署(02)
2024-10-21 10:04:07
项目部署
WEB工作原理
- 客户端(chrom) <=> WEB服务器(nginx) <=> WSGI(uWSGI) <=> Python(Flask) <=> 数据库(MySQL)
- Flask框架自带一个测试服务器,开发时直接运行即可;但是在生成环境中,必须进行完整部署。
nginx服务器
安装:
sudo apt-get install nginx
添加虚拟主机:
- 在
/etc/nginx/sites-available
目录下创建虚拟主机配置文件www.blog.com
server {
listen 80;
server_name www.blog.com blog.com; location / {
root /var/www/blog;
index idnex.html;
}
}
- 创建软链接
sudo ln -s /etc/nginx/sites-available/www.blog.com /etc/nginx/sites-enabled
- 重启nginx服务(若重启失败,可以关闭然后再打开)
- 创建目录:
/var/www/blog
,然后在目录下创建index.html
文件 - 添加本地域名解析,修改配置文件
/etc/hosts
,在末尾添加如下内容:
127.0.0.1 www.blog.com
127.0.0.1 blog.com
- 在
移植博客项目
- 将书写好的项目拷贝到
/var/www/blog
uWSGI
说明:是一个实现了WSGI协议的应用程序。
安装:
pip3 install uwsgi
配置:
http: # 以HTTP协议启动
socket: # 以socket方式启动
chdir: # uwsgi启动项目的根目录
wsgi-file: # 数据交给哪个模块处理
callable: # 具体可调用的对象
daemonize: # 后台运行(指定一个日志文件即可)
processes: # 指定进程数
threads: # 指定线程数
启动演示(以HTTP协议启动)
uwsgi --http 127.0.0.1:5000 --wsgi-file blog.py --callable app
以socket形式启动
- nginx转发请求
server {
listen 80;
server_name www.blog.com blog.com; location / {
#root /var/www/blog;
#index idnex.html;
# 包含请求参数
include uwsgi_params;
# 以socket形式转发请求数据
uwsgi_pass 127.0.0.1:5000;
}
}
提醒:一定不要忘记重启nginx。
- 启动uwsgi
uwsgi --socket 127.0.0.1:5000 --wsgi-file blog.py --callable app
- 将启动参数写入配置文件
uwsgi.ini
[uwsgi]
socket = 127.0.0.1:5000
wsgi-file = blog.py
callable = appp
# 后台运行
daemonize = /var/log/uwsgi.log
启动:uwsgi uwsgi.ini
静态资源处理
说明:静态资源nginx可以自行处理,没有必要转发到python中。因此需要添加路由转发规则。
静态资源路由规则:
location /static {
root /var/www/blog;
# 等价于
alias /var/www/blog/static;
}
测试准备:在blog目录下创建static目录,并拷贝一张图片
在浏览器地址栏输入:www.blog.com/static/gyy.jpg
最新文章
- Dapper 数据操作框架
- C# 创建开机启动核心代码
- Android -- android.os.Parcelable[] cannot be cast to ...
- python-操作excel数据文件
- POJ1743 Musical Theme
- Mysql,SqlServer,Oracle主键自动增长的设置
- tf–idf算法解释及其python代码实现(下)
- Asp.Net Core
- 【转】关于C语言生成不重复的随机数
- Android超炫日期日历控件:TimesSquare
- win10环境下jdk1.8+Android Developer Tools Build: v22.3.0-887826的问题
- 面向面试编程——javascript对象的几种创建方式
- 使用Jquery.cookie.js操作cookie
- selenium之元素定位-css
- 2019-3-22KeyDown,KeyPress 和 KeyUp 事件
- Azure中block和Page的比较 Azure: Did You Know? Block vs Page Blobs
- Apache kylin 入门
- 前端 搜索样式 html
- DB安装
- VMware虚拟机安装红帽系统无法上网解决办法(转)