V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
tmackan
V2EX  ›  Python

virtualenv 部署 Django 项目

  •  
  •   tmackan ·
    tmacjx · 2016-08-19 14:46:32 +08:00 · 4500 次点击
    这是一个创建于 2778 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我最近在用 virtualenv 部署项目,遇到一些问题。

    1.我的 virtualenv 中 python 版本是 3.4, 系统自带是 2.7

    我的 uwsgi.ini 配置文件

    [uwsgi]
    socket= 127.0.0.1:8077
    home=/root/.virtualenvs/blog 
    chdir=/alidata/websites/LandsBLog
    #wsgi-file=/alidata/websites/LandsBLog/test.py
    module=/alidata/websites/LandsBLog/LandsBLog/wsgi.py
    uid=515
    gid=515
    processes=4
    threads=2
    stats=127.0.0.1:1080
    daemonize=uwsgi.log
    

    在 virtualenv 环境下,启动执行 uwsgi.ini 报错

     Python version: 2.7.5 (default, Nov 20 2015, 02:00:19)  [GCC 4.8.5 20150623 (Red Hat 4.8.5-4)]
     Set PythonHome to /root/.virtualenvs/blog
     ImportError: No module named site
    

    我 uwsgi 在系统和对应的 virtualenv 都有安装,求解。 第一次注册 V2EX 发帖。 我参考的文档:

    http://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html

    22 条回复    2016-08-23 14:09:06 +08:00
    tmackan
        1
    tmackan  
    OP
       2016-08-19 14:59:48 +08:00
    (blog) [root@iZ230ph83b2Z LandsBLog]# which python
    /root/.virtualenvs/blog/bin/python

    (blog) [root@iZ230ph83b2Z LandsBLog]# which uwsgi
    /root/.virtualenvs/blog/bin/uwsgi
    pc10201
        2
    pc10201  
       2016-08-19 15:03:50 +08:00
    我也遇到过这样的问题,死活跑不起来,后来用 gunicorn 与 meinheld 替代 uwsgi 了
    tmackan
        3
    tmackan  
    OP
       2016-08-19 15:12:56 +08:00
    @pc10201 也是奇怪了
    uniquecolesmith
        4
    uniquecolesmith  
       2016-08-19 15:31:30 +08:00
    if-env = VIRTUAL_ENV
    home = %(_)
    endif =

    mudule = YOUR_DJANGO_PROJECTNAME.wsgi:application

    确认 virtualenv 中 django 是有效的
    lrqrun
        5
    lrqrun  
       2016-08-19 15:32:05 +08:00
    启动 uwsgi 也是在 virtualenv 环境下?
    uniquecolesmith
        6
    uniquecolesmith  
       2016-08-19 15:35:13 +08:00
    对了,首先要 workon YOUR_WORK_ENV 或者 source path/to/YOUR_WORK_ENV/bin/activate
    clino
        7
    clino  
       2016-08-19 15:36:22 +08:00
    uwsgi 可以指定 env 路径的,如 --virtualenv /home/xx/xx_depl/env

    btw: 建议不要用 root 来跑应用,不好的习惯
    lxf1992521
        8
    lxf1992521  
       2016-08-19 15:56:58 +08:00
    uwsgi --chdir /home/atlantis/work/atlantis --module atlantis.wsgi --virtualenv /home/atlantis/ENV --ini uwsgi/uwsgi9090.ini
    kinghui
        9
    kinghui  
       2016-08-19 16:07:18 +08:00
    你的 virtualenv 在 /root/ 下, 而 /root 的权限是这样: 700, 你 uwsgi 又切换到普通用户运行, 你确定这个普通用户有权限读取或执行 /root/.virtualenvs/blog 下的库?
    kinghui
        10
    kinghui  
       2016-08-19 16:13:45 +08:00
    kinghui
        11
    kinghui  
       2016-08-19 16:14:16 +08:00
    ziXiong
        12
    ziXiong  
       2016-08-21 17:23:23 +08:00
    你的 virtualenv 中 python 版本是 3.4 ?? 报错显示的不是 2.7 吗? 你没有通过 source 进入 virtualenv 环境吧。
    tmackan
        13
    tmackan  
    OP
       2016-08-22 11:15:08 +08:00
    现在我以 workon blog 开启了虚拟环境,然后以 cmd 的方式开启了 uwsgi 服务
    uwsgi --http :8000 --wsgi-file test.py
    日志显示如下:

    *** Starting uWSGI 2.0.13.1 (64bit) on [Mon Aug 22 10:58:14 2016] ***
    compiled with version: 4.8.5 20150623 (Red Hat 4.8.5-4) on 19 August 2016 14:24:34
    os: Linux-3.10.0-327.el7.x86_64 #1 SMP Thu Nov 19 22:10:57 UTC 2015
    nodename: iZ230ph83b2Z
    machine: x86_64
    clock source: unix
    pcre jit disabled
    detected number of CPU cores: 1
    current working directory: /alidata/websites/LandsBLog
    detected binary path: /root/.virtualenvs/blog/bin/uwsgi
    uWSGI running as root, you can use --uid/--gid/--chroot options
    *** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
    *** WARNING: you are running uWSGI without its master process manager ***
    your processes number limit is 7283
    your memory page size is 4096 bytes
    detected max file descriptor number: 65535
    lock engine: pthread robust mutexes
    thunder lock: disabled (you can enable it with --thunder-lock)
    uWSGI http bound on :8000 fd 4
    spawned uWSGI http 1 (pid: 855)
    uwsgi socket 0 bound to TCP address 127.0.0.1:45769 (port auto-assigned) fd 3
    Python version: 3.4.1 (default, Aug 19 2016, 10:38:55) [GCC 4.8.5 20150623 (Red Hat 4.8.5-4)]
    *** Python threads support is disabled. You can enable it with --enable-threads ***
    Python main interpreter initialized at 0xc20b00
    your server socket listen backlog is limited to 100 connections
    your mercy for graceful operations on workers is 60 seconds
    mapped 72768 bytes (71 KB) for 1 cores
    *** Operational MODE: single process ***
    WSGI app 0 (mountpoint='') ready in 0 seconds on interpreter 0xc20b00 pid: 854 (default app)
    *** uWSGI is running in multiple interpreter mode ***
    spawned uWSGI worker 1 (and the only) (pid: 854, cores: 1)

    在浏览器上输入 IP : 8000 显示无法访问,求解。
    tmackan
        14
    tmackan  
    OP
       2016-08-22 11:29:25 +08:00
    @clino 我 uid 分配了一个用户, gid 也分配了一个用户组,但是还是提示我以 root 用户启动 uwsgi 服务
    tmackan
        15
    tmackan  
    OP
       2016-08-22 11:33:31 +08:00
    @tmackan

    test.py
    def application(env, start_response):
    start_response('200 OK', [('Content-Type','text/html')])
    return "Hello World"
    tmackan
        16
    tmackan  
    OP
       2016-08-22 13:16:06 +08:00
    执行
    netstat -ntulp |grep 80
    发现 uswgi 服务已经启动了
    tcp 0 0 0.0.0.0:8077 0.0.0.0:* LISTEN 980/uwsgi
    clino
        17
    clino  
       2016-08-22 15:04:23 +08:00
    @tmackan 你都创建一个用户了,为什么不在用户下面新建一个 env,还用"home=/root/.virtualenvs/blog" 这样呢?
    tmackan
        18
    tmackan  
    OP
       2016-08-22 16:35:01 +08:00
    @clino 我用 virtual wrapper 管理所有的虚拟环境
    clino
        19
    clino  
       2016-08-22 16:41:32 +08:00
    @tmackan 不明白你的意思. 另外你的所有用户都能读 /root 下的文件?
    restran
        20
    restran  
       2016-08-23 08:47:13 +08:00
    使用 gunicorn 来部署 Django ,超级简单
    tmackan
        21
    tmackan  
    OP
       2016-08-23 10:16:23 +08:00
    @restran 但是还是想折腾下
    julyclyde
        22
    julyclyde  
       2016-08-23 14:09:06 +08:00
    @restran 你这明显是不审题
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5332 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 09:15 · PVG 17:15 · LAX 02:15 · JFK 05:15
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.