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

在这里插入图片描述

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查看

在这里插入图片描述

如果访问失败,首先检查安全组是否开放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查看

在这里插入图片描述

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jiucheng18

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值