flask+python+uwgsi+nginx+supervisor云服务器部署项目

配置环境

  • 首先安装pip3 apt install python3-pip

  • 安装虚拟环境 pip3 install virtualenv

  • 创建存放网站的文件夹并进入
    在这里插入图片描述

  • 创建python虚拟环境virtualenv venv

    会生成一个venv文件夹
    在这里插入图片描述

  • 打开虚拟环境source ./venv/bin/activate
    在这里插入图片描述
    这时在命令行最前面会出现(venv)可以通过deactivate 退出虚拟环境

  • 然后把项目文件放入www文件夹,然后安装相应的库

    这里我把需要的库都放在requirement.txt中,直接用命令行安装全部库

    切记要在虚拟环境下安装
    在这里插入图片描述

  • (可选)这时可以将app.run()指定为host=‘0.0.0.0’ 然后python运行主文件 app.py 就可以在浏览器里访问该网站了

安装uwsgi

  • 首先还是通过pip3安装uwsgi

  • 然后在www目录下创建一个uwsgi.ini的配置文件

    写入如下内容

    
    [uwsgi]
      
    socket = 127.0.0.1:5000#或者可以填写内网ip
    chdir =/root/www/WebDemo/
    wsgi-file = app.py
    callable = app
    processes = 4
    threads = 2
    stats = 127.0.0.1:9191#同上 可以填写内网ip但是要保持一致
    

安装nginx

  • apt install nginx

  • 检查配置文件是否正确 nginx -t

    如果不出现successful 就重新安装下
    在这里插入图片描述

  • 然后用vim打开 /etc/nginx/sites-available/default

    修改里面的内容,

    ##
    # You should look at the following URL's in order to grasp a solid understanding
    # of Nginx configuration files in order to fully unleash the power of Nginx.
    # https://www.nginx.com/resources/wiki/start/
    # https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/
    # https://wiki.debian.org/Nginx/DirectoryStructure
    #
    # In most cases, administrators will remove this file from sites-enabled/ and
    # leave it as reference inside of sites-available where it will continue to be
    # updated by the nginx packaging team.
    #
    # This file will automatically load configuration files provided by other
    # applications, such as Drupal or Wordpress. These applications will be made
    # available underneath a path with that package name, such as /drupal8.
    #
    # Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
    ##
    
    # Default server configuration
    #
    server {
            listen 80 ;#监听80端口
            server_name  107.191.41.24;#公网ip 通过这个ip在浏览器里访问
            # SSL configuration
            #
            # listen 443 ssl default_server;
            # listen [::]:443 ssl default_server;
            #
            # Note: You should disable gzip for SSL traffic.
            # See: https://bugs.debian.org/773332
            #
            # Read up on ssl_ciphers to ensure a secure configuration.
            # See: https://bugs.debian.org/765782
            #
            # Self signed certs generated by the ssl-cert package
            # Don't use them in a production server!
            #
            # include snippets/snakeoil.conf;
    
    
            # Add index.php to the list if you are using PHP
    		# 这里本来有一些其他的代码,可以删去,和这里保持一致
    
            location / {
                    # First attempt to serve request as file, then
                    # as directory, then fall back to displaying a 404.
                    include uwsgi_params;
                    uwsgi_pass  127.0.0.1:5000;#这里填写127.0.0.1或者是内网ip 必须和uwsgi.ini中写的ip一致
                    uwsgi_param UWSGI_PYTHON /root/www/venv/;#之前创建的虚拟环境目录
                    uwsgi_param UWSGI_CHDIR /root/www/WebDemo/;#项目的目录
                    uwsgi_param UWSGI_SCRIPT app:app;#第一个app是程序的入口,就是含有app.run的那个程序的名字 ,第二个app是主程序里的变量名,如果写的是xxx.run(),那xxx就是变量名
            }
    
    
            # pass PHP scripts to FastCGI server
            #
            #location ~ \.php$ {
            #       include snippets/fastcgi-php.conf;
            #
            #       # With php-fpm (or other unix sockets):
            #       fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
            #       # With php-cgi (or other tcp sockets):
            #       fastcgi_pass 127.0.0.1:9000;
            #}
    
            # deny access to .htaccess files, if Apache's document root
            # concurs with nginx's one
            #
            #location ~ /\.ht {
            #       deny all;
            #}
    }
    
    
    # Virtual Host configuration for example.com
    #
    # You can move that to a different file under sites-available/ and symlink that
    # to sites-enabled/ to enable it.
    #
    #server {
    #       listen 80;
    #       listen [::]:80;
    #
    #       server_name example.com;
    #
    #       root /var/www/example.com;
    #       index index.html;
    #
    #       location / {
    #               try_files $uri $uri/ =404;
    #       }
    #}
    
    

    保存,退出

  • 接着可以用nginx -t检验下是否配置正确

    然后nginx -s reload重新加载nginx

    然后killall nginx关闭所有相关进程

    最后重新打开nginx

在这里插入图片描述

  • 最后 在运行nginx情况下 执行uwsgi --ini uwsgi.ini
    在这里插入图片描述
    然后就可以在浏览器里访问了
    在这里插入图片描述

安装supervisor

  • 通过apt-get install supervisor安装

  • 在**/etc/supervisor/conf.d/**目录下新建一个.conf文件

    写入以下内容

    [program:uwsgi]#项目名称  
    command = /root/www/venv/bin/uwsgi --ini /root/www/uwsgi.ini #uwsgi在虚拟环境中的位置 和uwsgi.ini位置
    autostart = true
    autorestart = true
    user=root
    startretries = 5
    startsecs = 10
    stdout_logfile=/root/www/logs/uwsgi_supervisor.log#日志保存的位置
    [supervisord]
    nodaemon=true
    
    [supervisorctl]
    
    
  • 重启supervisor

  • 启动进程supervisorctl start uwsgi

    然后就可以访问网站了

  • 如果要停止进程 supervisorctl start uwsgi

  • 重启supervisor

  • 启动进程supervisorctl start uwsgi

    然后就可以访问网站了

  • 如果要停止进程 supervisorctl start uwsgi

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值