flask + nginx + uwsgi 项目部署上线

安装 nginx

yum install nginx  或者   apt-get install nginx

nginx 配置成服务

第一步:
	编写nginx文件,放入/etc/init.d/
	参考链接:https://www.cnblogs.com/shihaiming/p/6290219.html
	#!/bin/bash

# nginx Startup script for the Nginx HTTP Server

# it is v.0.0.2 version.

# chkconfig: - 85 15

# description: Nginx is a high-performance web and proxy server.

#              It has a lot of features, but it's not for everyone.

# processname: nginx

# pidfile: /var/run/nginx.pid

# config: /usr/local/nginx/conf/nginx.conf

nginxd=自己nginx的安装地址

nginx_config=自己nginx的配置文件

nginx_pid=/var/run/nginx.pid

RETVAL=0

prog="nginx"

# Source function library.

. /etc/rc.d/init.d/functions

# Source networking configuration.

. /etc/sysconfig/network

# Check that networking is up.

[ ${NETWORKING} = "no" ] && exit 0

[ -x $nginxd ] || exit 0

# Start nginx daemons functions.

start() {

if [ -e $nginx_pid ];then

   echo "nginx already running...."

   exit 1

fi

   echo -n $"Starting $prog: "

   daemon $nginxd -c ${nginx_config}

   RETVAL=$?

   echo

   [ $RETVAL = 0 ] && touch /var/lock/subsys/nginx

   return $RETVAL

}

# Stop nginx daemons functions.

stop() {

        echo -n $"Stopping $prog: "

        killproc $nginxd

        RETVAL=$?

        echo

        [ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /var/run/nginx.pid

}

# reload nginx service functions.

reload() {

    echo -n $"Reloading $prog: "

    #kill -HUP `cat ${nginx_pid}`

    killproc $nginxd -HUP

    RETVAL=$?

    echo

}

# See how we were called.

case "$1" in

start)

        start

        ;;

stop)

        stop

        ;;

reload)

        reload

        ;;

restart)

        stop

        start

        ;;

status)

        status $prog

        RETVAL=$?

        ;;

*)

        echo $"Usage: $prog {start|stop|restart|reload|status|help}"

        exit 1

esac

exit $RETVAL
第二步:  执行 以下 命令
	chkconfig --add /etc/init.d/nginx
	chmod 755 /etc/init.d/nginx
	chkconfig --add nginx
如果想随系统启动就执行
	/sbin/chkconfig --level 345 nginx on
第三步:
	nginx启动、停止、无间断服务重启,可选  start | stop | restart | reload | status |  help
	# 启动
service nginx start
	# 停止
service nginx stop
	# 重启
service nginx reload

uwsgi 配置

在flask项目里手动添加文件 文件名称为: uwsgi.ini

[uwsgi]
socket=0.0.0.0:5000
# 主程序 主文件名
wsgi-file= /home/www/img_textFlask/main.py
# 主程序 静态文件名
static-map = /static=/home/image
# 主程序 启动名
callable = app
master = true
vhost = true
cache = true
lazy-apps = true
enable-threads = true
reload-mercy = 10
buffer-size = 65535
memory-report = true
vacuum = true
limit-as = 2048
workers = 8
thunder-lock = True
# 进程文件   (自动创建)
pidfile= /home/www/img_textFlask/uwsgi.pid
# 程序log日志文件  (自动创建)
daemonize= /home/www/img_textFlask/uwsgi.log
http-timeout = 4800
harakiri = 4800

nginx 配置文件:

	nginx.conf   默认安装位置  ---  /etc/nginx       文件内容如下:
# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    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  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;


    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    # include /etc/nginx/conf.d/*.conf;
    # 我把这个文件有复制了一份在nginx文件夹里 新建了一个 名为 sites-enabled 的文件夹、在 sites-enabled文件夹里放入了新的一个 .conf 文件  文件名自己取。
    include /etc/nginx/sites-enabled/*.conf;
    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

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

        #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;
        #}
    }


# Settings for a TLS enabled server.
#
#    server {
#        listen       443 ssl http2 default_server;
#        listen       [::]:443 ssl http2 default_server;
#        server_name  _;
#        root         /usr/share/nginx/html;
#
#        ssl_certificate "/etc/pki/nginx/server.crt";
#        ssl_certificate_key "/etc/pki/nginx/private/server.key";
#        ssl_session_cache shared:SSL:1m;
#        ssl_session_timeout  10m;
#        ssl_ciphers PROFILE=SYSTEM;
#        ssl_prefer_server_ciphers on;
#
#        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;
#
#        location / {
#        }
#
#        error_page 404 /404.html;
#            location = /40x.html {
#        }
#
#        error_page 500 502 503 504 /50x.html;
#            location = /50x.html {
#        }
#    }

}
 新的.conf  文件 位置    ----   /etc/nginx/sites-enabled/ new.conf       文件内容如下:
server { 
	  # 这是配置 ssl 时填的、、、如果没有域名    直接 填 80 就行    我的服务器 80 
    listen 443 ssl;
    server_name  qq.com;    #自己的域名
    # ssl 证书位置     把得到的ssl 证书 放到 /etc/ssl/自己起个名/证书
    ssl_certificate /etc/ssl/startao/******.crt;
    # # ssl 证书位置     把得到的ssl 证书 放到 /etc/ssl/自己起个名/证书
    ssl_certificate_key /etc/ssl/startao/******.key;
    #请按照以下协议配置     这是好像是 死的 
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    #请按照以下套件配置,配置加密套件,写法遵循 openssl 标准。
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
    ssl_prefer_server_ciphers on;

    location / {
        uwsgi_pass  127.0.0.1:5000;         # 监听成功后请求的ip端口,与uwsgi中socket的IP端口一致
        include     uwsgi_params;
    }

    # 配置静态文件
    location /image {
        autoindex on;
        alias ******;  # 自己的静态文件位置
        # 要与之前django的setting.py中添加的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;
    #}
}

server {
    listen 80;
    #填写绑定证书的域名
    server_name 自己的域名;
    #把http的域名请求转成https   重定向到了 https 上面了 
    return 301 https://$host$request_uri;
}

配置好了之后重启 nginx 如果重启不成功 可能是防火墙的问题(我的就是防火墙的问题),也许不是,自己百度解决吧、
如果是防火墙的问题:
配置https -> SSL证书需要向防火墙添加端口

# 执行命令   添加防火墙开放端口
firewall-cmd --zone=public --add-port=443/tcp --permanent
	#重启防火墙
firewall-cmd --reload
至此 配置结束,如果途中出现其他问题,请留言一起商讨!!!
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值