单位建设网站需要招标,沈阳大东区做网站公司,网站后台上传不了图片,可以免费做试卷题目的网站起因
想把自己的Flask或者Django网站#xff0c;发布到服务器上#xff0c;让大家都可以访问。网上搜的结果#xff0c;要么是用NginxuWSGI#xff0c;要么是用NginxGunicorn。大名鼎鼎的Nginx我早有耳闻#xff0c;那么两位俩玩意是啥呢。
WSGI是什么
uwsgi是Nginx和w…起因
想把自己的Flask或者Django网站发布到服务器上让大家都可以访问。网上搜的结果要么是用NginxuWSGI要么是用NginxGunicorn。大名鼎鼎的Nginx我早有耳闻那么两位俩玩意是啥呢。
WSGI是什么
uwsgi是Nginx和web框架比如flask和Django之间的桥梁。Nginx处理静态文件非常优秀却不能直接与我们的Python Web应用程序进行交互。flask或Django的测试服务器处理并发效果很差。
为了解决Web 服务器与应用程序之间的交互问题就出现了Web 服务器与应用程序之间交互的规范。最早出现的是CGI,后来又出现了改进 CGI 性能的FasgCGIJava 专用的 Servlet 规范。在Python领域最知名的就是WSGI规范了。
这其中尤其以为uWSGI和Gunicorn最为有名
各自的优劣
那问题来了二选一该选谁呢。
uwsgi目前好像只支持到比较低的版本比如python3.8。这是因为我在python3.10的环境安装总是报错。后来网上冲浪了很久得出的答案。
而Gunicorn不支持windows。但好处是他安装简单因为不依赖其他库。
pip3 install gunicorn用gunicorn启动flask
# 在虚拟环境安装一遍
pip install gunicorn #安装gunicorn
ln -s /usr/local/python3/bin/gunicorn /usr/bin/gunicorn #配置环境变量启动项目
# equivalent to from hello import app
$ gunicorn -w 4 main:app# equivalent to from hello import create_app; create_app()
$ gunicorn -w 4 main:create_app()可以指定端口
gunicorn -w 4 -b 0.0.0.0:8080 main:create_app() #main文件下的app变量用gunicorn关闭
# 查看进程
pstree -ap|grep gunicorn
# 如果无法使用pstree命令请yum安装
yum -y install psmisc假如搜索结果如下 通过如下命令杀进程
kill -HUP 76537实际操作
下面举例说明如何操作
环境
centos7.9/Mysql8.0/Python3.10.10
首先默认以上都已OK然后创建一个虚拟环境
mkdir /opt/my_pj
cd /opt/my_pj
python3 -m venv venv310
# 进入虚拟环境的bin目录
cd py310\bin
# 启动虚拟环境
source activate上传代码
通过git也好ftp工具也好将项目上传。例如我的项目叫hanayo然后安装项目依赖
pip install -r requirements.txt当然还需要安装gunicorn
pip install gunicorn然后通过gunicorn启动项目这里是在8080端口运行
gunicorn -w 4 -b 0.0.0.0:8080 hanayo:create_app() #main文件下的app变量这里我的代码中hanayo是项目文件夹名然后init文件中有create_app函数这在Flask文档中好像叫工厂模式 Nginx简单设置
接下来设置nginxyum安装的nginx设置文件在/etc/nginx/nginx.conf
我这里设置在/opt/nginx/conf/nginx.conf
server {listen 80;server_name _;location / {proxy_pass http://127.0.0.1:8080/;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_set_header X-Forwarded-Proto $scheme;proxy_set_header X-Forwarded-Host $host;proxy_set_header X-Forwarded-Prefix /;}
}然后刷新nginx
nginx -s reload然后就大工告成啦可以看到我的网站已经可以访问了 参考
https://pythondjango.cn/python/tools/6-uwsgi-configuration/ https://dormousehole.readthedocs.io/en/latest/deploying/nginx.html https://dormousehole.readthedocs.io/en/latest/deploying/gunicorn.html