美文网首页
Flask+uwsgi+Nginx+Ubuntu部署爬坑过程

Flask+uwsgi+Nginx+Ubuntu部署爬坑过程

作者: 猕猴桃syh | 来源:发表于2020-05-18 17:57 被阅读0次

    整个爬坑过程整整花了我两天的时间,最后由于硬件相关自带程序的问题,最后还没有应用该方案。想了想还是应该把这两天的爬坑过程记录一下,一方面,为了纪念一下此次爬坑过程,另一方面也希望可以帮到有需要的同学。

    准备工作:

    1、 Ubuntu系统安装
    2、 Python安装及项目虚拟环境的建立
    本人项目中使用的是Ubuntu20.4,该版本的系统自带Python3.8,所以省去了安装Python3.8的过程,只需要为本项目建立环境即可。

    安装uwsgi

    apt-get install python3-venv
    

    使用以上语句创建虚拟环境,创建虚拟环境之后,把虚拟环境进行激活,然后安装项目所需要的包。

     apt-get install build-essential python python-dev   #先安装python-dev,不然后续的uwsgi安装会失败
    
    pip install uwsgi    #使用pip进行uwsgi进行安装
    

    安装及运行的过程中出现的错误

    1、error: invalid command 'bdist_wheel'

    出错:ERROR: Command errored out with exit status 1:
    
    command: /venv/bin/python3.8 -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-trwu8uwh/uwsgi/setup.py'"'"'; __file__='"'"'/tmp/pip-install-trwu8uwh/uwsgi/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' bdist_wheel -d /tmp/pip-wheel-m46bue9l
    
    cwd: /tmp/pip-install-trwu8uwh/uwsgi/
    
    Complete output (8 lines):
    
    /usr/lib/python3.8/distutils/dist.py:274: UserWarning: Unknown distribution option: 'descriptions'
    
    warnings.warn(msg)
    
    usage: setup.py [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...]
    
    or: setup.py --help [cmd1 cmd2 ...]
    
    or: setup.py --help-commands
    
    or: setup.py cmd --help
    
    
    
    error: invalid command 'bdist_wheel'
    
    ----------------------------------------
    
    ERROR: Failed building wheel for uwsgi
    

    解决方法:安装wheel

    pip3 install wheel
    

    2、使用该命令运行uwsgi --http-socket 127.0.0.1:5000 --wsgi-file /var/www/micro/micro.py出错

    出错:uwsgi: unrecognized option '--wsgi-file'
    
    getopt_long() error
    

    解决方法:使用一下语句运行,在命令行中添加--plugin python3

    uwsgi --http127.0.0.1:5000 --plugin python3 --wsgi-file /var/www/micro/micro.py
    

    3、继承问题2,使用修改后的语句运行继续出错,错误信息如下:

    出错:open("/usr/lib/uwsgi/plugins/python_plugin.so"): No such file or directory [core/utils.c line 3724]
    
    !!! UNABLE to load uWSGI plugin: /usr/lib/uwsgi/plugins/python_plugin.so: cannot open shared object file: No such file or directory !!!
    
    uwsgi: unrecognized option '--wsgi-file'
    
    getopt_long() error
    

    根据错误信息,我去/usr/lib/uwsgi/plugins文件夹下查看有没有相关信息,经查看发现该文件下确实没有python_plugin.so文件,所以,我又去下载该文件

    uwsgi-plugin-python
    

    使用该语句下载时,又出现以下问题

    出错:E: Unable to locate package uwsgi-plugin-python
    

    由于我用的是python3 所以应该使用以下语句进行下载

    uwsgi-plugin-python3
    

    至此,这个坑爬出来了,可是刚刚爬出次坑又立马进入下一个坑

    4、运行uwsgi找不到已安装的python包
    解决方法:
    在py启动文件中最开始加入

    sys.path.append('/venv/lib/python3.8/site-packages')
    

    即python包下载的位置
    5、uwsgi启动flask程序,无法开启自己写的线程
    这个坑花了很长时间,尝试了网上的很多办法都没有办法解决。
    比如:uwsgi配置文中设置enable-thread=true
    最后解决办法:
    使用flask的钩子函数,把线程启动写在钩子函数中

    @app.before_first_request
    def first_request():
          my_thread.start()
    

    before_first_request为在第一个请求之前先执行
    flask相关的钩子函数请参考以下链接:
    flask钩子函数
    至此,uwsgi的所有坑已经爬完

    nginx 配置

    server{
              listen 80;
              server_name 你的域名;
              location / {
                  include uwsgi_params;
                  uwsgi_pass 127.0.0.1:5000;
                 uwsgi_param UWSGI_CHDIR /var/www/micro;
                  uwsgi_param UWSGI_SCRIPT micro:app;
                }
    }
    

    到此时,已经可以运行你的flask程序了,使用uwsgi+nginx部署成功!!!

    相关文章

      网友评论

          本文标题:Flask+uwsgi+Nginx+Ubuntu部署爬坑过程

          本文链接:https://www.haomeiwen.com/subject/ftcpohtx.html