python Flask web项目uwsgi + nginx部署

这篇具有很好参考价值的文章主要介绍了python Flask web项目uwsgi + nginx部署。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

1.安装python

2.虚拟环境

2.1安装vertualenv

pip3 install virtualenv

2.2创建虚拟环境

创建保存环境的目录:

mkdir venvs

创建虚拟环境:

[root@root /]# virtualenv /home/xxx/venvs/flask2 --python=python3

查看虚拟环境:

[root@root venvs]# ls
flask2

2.3激活虚拟环境

activiate是激活虚拟环境的命令脚本,在虚拟环境的bin目录下

[root@root bin]# ls
activate      activate.fish  activate.ps1      deactivate.nu  pip3     pip3.7  python3    wheel   wheel-3.7
activate.csh  activate.nu    activate_this.py  pip            pip-3.7  python  python3.7  wheel3  wheel3.7

执行activate激活环境

[root@root bin]# source activate
(flask2) [root@root bin]# 

3.环境-uwsgi

3.1安装uwsgi

激活虚拟环境,安装uwsgi

source activate
pip install uwsgi

3.2基于uwsgi运行flask项目

3.2.1命令的方式
uwsgi --http :8080 --wsgi-file app.py --callable app
3.2.2配置文件(推荐)

uwsgi.ini

[uwsgi]
socket = 127.0.0.1:8001
chdir = /home/xxx/data/code/xxx
wsgi-file = app.py
callable = app
processes = 1
virtualenv = /home/xxx/venvs/flask2

启动命令

uwsgi --ini uwsgi.ini

ctrl + c停止

后台启动

uwsgi -d --ini uwsgi.ini

停止

(flask2) [root@root flask]# ps -ef | grep uwsgi
root      7114  6277  0 22:13 pts/1    00:00:00 uwsgi --ini uwsgi.ini
root      7118  6277  0 22:15 pts/1    00:00:00 grep --color=auto uwsgi
(flask2) [root@root flask]# kill -9 7114

4.环境-nginx

4.1安装

4.1.1 yum安装
yum install nginx -y

yum安装失败,未找到nginx包,换使用压缩包编译安装

4.1.2 编译安装

原文:https://www.kuangstudy.com/bbs/1511610238649233410

下载nginx包

下载链接:https://nginx.org/en/download.html

python Flask web项目uwsgi + nginx部署

1.nginx的环境依赖下载

编译工具gcc,一般系统都存在

yum install gcc-c++

pcre正则表达式库

yum install -y pcre pcre-devel

zlib解压和压缩库

yum install -y zlib zlib-devel

OpenSSL安全套接字密码库

yum install -y openssl openssl-devel

2.解压

tar -zxvf nginx-1.18.0.tar.gz 

3.执行configure

./configure

说明:–prefix参数表示把nginx编译到指定目录

如:--prefix=/www/server/nginx/表示编译到/www/server/nginx/目录下

4.编译

make

5.安装

make install

默认安装目录为/usr/local/nginx

查看nginx安装是否成功

在/usr/local/nginx/sbin目录下执行,无报错则启动成功

./nginx

浏览器输入ip:80查看

python Flask web项目uwsgi + nginx部署

如果访问失败,首先检查安全组是否开放80端口,若开放查看防火墙

查看防火墙是否开启

systemctl  status  firewalld

修改iptables防火墙规则,允许访问80

iptables -I INPUT -p tcp --dport 80 -j ACCEPT

4.2配置

普通请求 -> 8001

/static/ -> /home/xxx/data/flask/static


#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    upstream flask {
        server 127.0.0.1:8001;
    }

    server {
        listen       80;
        listen       [::]:80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        #location / {
        #    root   html;
        #   index  index.html index.htm;
        #}
        location / {
           uwsgi_pass   flask;
           include  uwsgi_params;
        }

        location /static {
           alias   /home/xxx/data/flask/static;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

4.3启动

在/usr/local/nginx/sbin目录下执行,无报错则启动成功

./nginx -s restart

5.部署

使用uwsgi后台启动项目,在浏览器输入url查看

python Flask web项目uwsgi + nginx部署文章来源地址https://www.toymoban.com/news/detail-498288.html

到了这里,关于python Flask web项目uwsgi + nginx部署的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 2-Docker-应用-多容器部署Django+Vue项目(nginx+uwsgi+mysql)

    基于Linux CentOS 7系统(虚拟机),使用Docker,多容器部署Django+Vue项目 整体部署用到了:Django+Vue+nginx+mysql+uwsgi 先每一个容器单独部署,最后用Docker compose 语法整合,统一部署 参考文章:https://blog.csdn.net/qq_45445505/article/details/135563784 章标题:Docker介绍 节标题:Docker安装 总结梳

    2024年03月10日
    浏览(84)
  • 使用IIS服务器部署Flask python Web项目

    参考文章 参考文章 将Flask应用程序部署到IIS服务器上需要一些步骤,因为IIS是为.NET应用程序设计的。要将Flask Python Web项目部署到IIS服务器,您需要使用一个称为\\\"FastCGI\\\"的桥接工具来连接IIS和Python应用程序。以下是将Flask应用程序部署到IIS服务器的一般步骤: 安装IIS和FastCG

    2024年04月16日
    浏览(43)
  • flask+Python+Vue实现前后端分离的web项目并部署至云服务器

    1 后台+算法模型 1.1 训练机器学习模型 1.2 基于Flask框架搭建后台接口 注意:前后端跨域问题,可引入CORS解决,具体如代码: 2 前端搭建 3 云服务器部署 详细设置教程可参考linux CentOS 宝塔面板安装设置教程 安装Nginx和python项目管理器,后续可在面板上操作,完成项目打包上传

    2024年02月07日
    浏览(47)
  • linux+python3.6.8+uwsgi+postgresql+django部署web服务器

    我这是使用华为云服务器

    2024年02月06日
    浏览(35)
  • Python对接微信小程序V3接口进行支付,并使用uwsgi+nginx+django进行https部署

    网上找了很多教程,但是很乱很杂,并且教程资源很少且说的详细。这里就记录一下分享给大家 共分为以下几个步骤: 目录 一、开始前准备信息 二、使用前端code获取用户的openid 三、对接小程序v3接口下单 四、小程序支付的回调 五、安装并启动uwsgi 六、安装并启动nginx 七、

    2024年02月12日
    浏览(33)
  • Python web实战 | Docker+Nginx部署python Django Web项目详细步骤【干货】

      在这篇文章中,我将介绍如何使用 Docker 和 Nginx 部署 Django Web 项目。一步步讲解如何构建 Docker 镜像、如何编写 Docker Compose 文件和如何配置 Nginx。 1.1 配置 Django 项目 在开始之前,我们需要有一个 Django 项目。如果你还没有 Django 项目,可以按照 Django 官方文档的指导创建一

    2024年02月15日
    浏览(37)
  • docker+mysql+flask+redis+vue3+uwsgi+docker部署

    首先拉取mysql的镜像,这里用的mysql5.7.6 docker pull mysql:5.7.6 镜像拉取完成后启动: 利用dockerfile构建python3.9.11+uwsgi+nginx 构建: docker build -t mydemo . flask配置: 安装:pip install -r requirements.txt celery: 微信支付python的库 --link redis:redis-server flask容器就可以读取redis容器redis服务了:

    2024年02月07日
    浏览(39)
  • 【Django】在Linux上部署Django(nginx+uwsgi)

    1.说明 关于在Linux上使用uwsgi部署Django的项目的过程并不难,主要是配置文件的写法,尤其是nginx的配置文件,本文在Ubuntu20.04上通过uwsgi和nginx部署Django项目 2.安装环境 安装环境主要有Nginx、Python、MySQL、Redis,可以根据你的实际情况进行安装 2.1 安装nginx 大多数发行版Linux都支

    2024年02月12日
    浏览(35)
  • 阿里云服务器部署flask项目「gunicorn + nginx + 支持https」

    最近做了一个微信小程序,使用 flask 实现了对应的后台,上线需要部署到服务器上,之前只是了解并没有全链路试过,靠着网上的资料最终完成部署上线,但中间遇到了较多的一些问题,网上的资料也比较零碎,所以整理了这篇文章,一方面是作为记录方便后续查阅,另一方

    2024年02月06日
    浏览(34)
  • Django使用uwsgi+nginx部署,admin没有样式解决办法

    若在服务器上部署的项目admin没有样式,则检查下一下配置: settings uwsgi.ini urls nginx.conf } 如果使用了虚拟环境则修改nginx.conf文件中的/static/路径为你虚拟环境的路径,没有使用虚拟环境则改为你python安装路径下的static 修改完后,cd/usr/sbin,使用./nginx -s reload,重启nginx,重启项目

    2024年02月14日
    浏览(31)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包