virtualenv 部署 Django 项目

2016-08-19 14:46:32 +08:00
 tmackan

我最近在用 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

4538 次点击
所在节点    Python
22 条回复
tmackan
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
2016-08-19 15:03:50 +08:00
我也遇到过这样的问题,死活跑不起来,后来用 gunicorn 与 meinheld 替代 uwsgi 了
tmackan
2016-08-19 15:12:56 +08:00
@pc10201 也是奇怪了
uniquecolesmith
2016-08-19 15:31:30 +08:00
if-env = VIRTUAL_ENV
home = %(_)
endif =

mudule = YOUR_DJANGO_PROJECTNAME.wsgi:application

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

btw: 建议不要用 root 来跑应用,不好的习惯
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
2016-08-19 16:07:18 +08:00
你的 virtualenv 在 /root/ 下, 而 /root 的权限是这样: 700, 你 uwsgi 又切换到普通用户运行, 你确定这个普通用户有权限读取或执行 /root/.virtualenvs/blog 下的库?
kinghui
2016-08-19 16:13:45 +08:00
kinghui
2016-08-19 16:14:16 +08:00
ziXiong
2016-08-21 17:23:23 +08:00
你的 virtualenv 中 python 版本是 3.4 ?? 报错显示的不是 2.7 吗? 你没有通过 source 进入 virtualenv 环境吧。
tmackan
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
2016-08-22 11:29:25 +08:00
@clino 我 uid 分配了一个用户, gid 也分配了一个用户组,但是还是提示我以 root 用户启动 uwsgi 服务
tmackan
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
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
2016-08-22 15:04:23 +08:00
@tmackan 你都创建一个用户了,为什么不在用户下面新建一个 env,还用"home=/root/.virtualenvs/blog" 这样呢?
tmackan
2016-08-22 16:35:01 +08:00
@clino 我用 virtual wrapper 管理所有的虚拟环境
clino
2016-08-22 16:41:32 +08:00
@tmackan 不明白你的意思. 另外你的所有用户都能读 /root 下的文件?
restran
2016-08-23 08:47:13 +08:00
使用 gunicorn 来部署 Django ,超级简单

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/300433

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX